Select Type: Can not call subroutine defined only in derived type?
I recently started to dive into OOP with Fortran. I have a type hierarchy where I have an abstract base type genericProblem and a derived type specificProblem. specificProblem has a subroutine that is not defined in genericProblem, called "InitializeWith" in my example.
It seems that I misunderstood the select type construct. I thought it was made specifically for such cases where I know I can expect the variable problem to be of a certain (derived) type of genericProblem and that the compiler should know that "I'm apparently of type specificProblem now function calls specific to that type are ok".
I am however getting a This is not a field name that is defined in the encompassing structure. error with the example below, so I am probably missing out something. Could someone point me in the right direction?
subroutine AssignSomething(problem,rhsTarget)
class(genericProblemT), intent(in out) :: problem
class(genericProblemT), target, intent(in) :: rhsTarget
select type (lhsProblem => problem)
type is (specificProblemT)
! Try setting up the rhsProblem:
call lhsProblem%InitializeWith(rhsTarget) ! Gives an error
class default
! give error for unexpected/unsupported type
throw some error
end select
end subroutine AssignSomething
See also questions close to this topic

Cascading generic inheritance  "generic tree"?
I hope this is the right site to ask.
I am trying to model this:
 a Map can have multiple child of type Biomes and no parent
 a Biome can have multiple child of type Landforms and a Map as its parent
 a Landform can have multiple child of type Tiles and a Biome as its parent
 a Tile has no child and a Landform as its parent
I want it to be generic so I can easily add new links to the chain (like adding a new kind of section between Biome and Landform for example). Here is the less ugly solution I have for now :
public class RootSection<T, TChild> : Section<T> where T : Section<T> where TChild : Section<TChild> { public List<TChild> ChildSection { get; } // duplicate } public class MiddleSection<T, TChild, TParent> : Section<T> where T : Section<T> where TChild : Section<TChild> where TParent : Section<TParent> { public List<TChild> ChildSection { get; } // duplicate public TParent Parent { get; } // duplicate } public class BottomSection<T, TParent> : Section<T> where T : Section<T> where TParent : Section<TParent> { public TParent Parent { get; } // duplicate } public class Section<T> where T : Section<T> { List<T> AdjacentSections { get; } } public class Map : RootSection<Map, Biome> { } // (T, TChild) public class Biome : MiddleSection<Biome, Landform, Map> { } // (T, TChild, TParent) public class Landform : MiddleSection<Landform, Tile, Biome> { } // (T, TChild, TParent) public class Tile : BottomSection<Tile, Landform> { } // (T, TParent)
As you can see, there is already duplicate code and I can't think of a solution to get rid of this issue. I feel like I am either missing something obvious or overcomplexifying the problem. I also feel like this is close to a classic data structure which I ignore the name preventing me from searching for inspiration on the net.
How can I rewrite this code to look cleaner ? Am I right to think it's close to a well known data structure ?
Thank you!

Use a class object inside of another class? or just use fields?
I am practicing OOP Inheritance and came across a question, which I have a few ideas on an answer to, but I am not sure what is the "correct" way of thinking.
Say you have a class for a Person. That person has a name. Would you create a class for Name, then make an object of it inside of Person? Or would you just use fields for the first and last name?
Personally, I think that if it doesn't manipulate the data or isn't a lot of data  then I may as well just create 2 fields for first and last name at the class at the top of the inheritance tree so it will be inherited by all the sub classes.
Am I thinking about that correctly?
Thanks :D
Bryan

PHP class manipulating
class example{ static function toArray($notFormattedArray){ return ["ID" => $notFormattedArray>ID] } }
I want to manipulate array before toArray method with abstract class or whatever but user mustn't see this function in example class.
Example Order
 example>toArray($notFormattedArray) (Defining) >
 Manipulating Array from user so $example>toArray() function >
 Manipulating Array from other method or abstract class >
 return

Why midpoint rule turns out more accurate than Simpson's rule when doing riemann sum approximation on Fortran
everyone.
I am just playing with the calculation of integral of x^2 from [1, 2] using both midpoint rule and Simpson's rule. And I find it out that with the same number of subintervals midpoint rule approximation seems more accurate than Simpson's rule approximation, which is really weird.
The source code of midpoint rule approximation is :
program midpoint implicit none ! Turn off implicit typing Integer, parameter :: n=100 ! Number of subintervals integer :: i ! Loop index real :: xlow=1.0, xhi=2.0 ! Bounds of integral real :: dx ! Variable to hold width of subinterval real :: sum ! Variable to hold sum real :: xi ! Variable to hold location of ith subinterval real :: fi ! Variable to value of function at ith subinterval dx = (xhixlow)/(1.0*n) ! Calculate with of subinterval sum = 0.0 ! Initialize sum xi = xlow+0.5*dx ! Initialize value of xi do i = 1,n,1 ! Initiate loop ! xi = xlow+(0.5+1.0*i)*dx write(*,*) "i,xi ",i,xi ! Print intermidiate result fi = xi**2 ! Evaluate function at ith point sum = sum+fi*dx ! Accumulate sum xi = xi+dx ! Increment location of ith point end do ! Terminate loop write(*,*) "sum =",sum stop ! Stop execution of the program end program midpoint
the according execution is:
...... ..... .................. i,xi 100 1.99499905 sum = 2.33332348
The source code of Simpson's rule approximation is:
program simpson implicit none ! Turn off implicit typing integer, parameter :: n=100 ! Number of subintervals integer :: i=0 ! Loop index real :: xlow=1.0, xhi=2.0 ! Bounds of integral real :: h ! Variable to hold width of subinterval real :: sum ! Variable to hold sum real :: xi ! Variable to hold location of ith subinterval real :: fi ! Variable to value of function at ith subinterval real :: Psimp ! Variable of simpson polynomial of xi interval h = (xhixlow)/(1.0*n) ! Calculate width of subinterval sum = 0.0 ! Initialize sum do while (xi<=xhih) ! Initiate loop xi = xlow+i*2.0*h ! Increment of xi i=i+1 write(*,*) "i,xi ",i,xi ! Print intermidiate result Psimp=xi**2+4.0*(xi+h)**2+(xi+2.0*h)**2 ! Evaluate function at ith point sum = sum+(h/3.0)*Psimp ! Accumulate sum end do ! Terminate loop write(*,*) "sum =",sum end program simpson
the according execution is:
........ ...... ................... i,xi 101 2.00000000 sum = 2.37353396
To get the same precision of digits as midpoint result, I have to set the number of subintervals in Simpson's program to 100000, which is 1000 times more than the midpoint program (I initially set both of the number subintervals to 100)
I check the codes in Simpson's program and can't find whats wrong.
Simpson's rule should converge more rapid than midpoint rule if I remembered it correct.

How to pass function name in subroutine/function
My problem is to pass the names of a series of functions contained in a module to a subroutine in a do loop.
I post part of my code. The modules are in two separate files compared to the main.
%%% FILE testKer_mod.f90
module kernel implicit none contains ! POLYNOMIAL function poly(x, ts, ndim, a, param1, param2) integer, intent (in) :: ndim real*8, dimension(ndim), intent(in) :: x real*8, dimension(ndim), intent(in) :: ts real*8, intent(in) :: a, param1, param2 real*8 :: r real*8 :: poly r = (x(1:ndim)  ts(1:ndim)) poly = r**(.5*a) end function poly ! GAUSSIAN function gauss(x, ts, ndim, a, gamma, param2) integer, intent (in) :: ndim real*8, dimension(ndim), intent(in) :: x real*8, dimension(ndim), intent(in) :: ts real*8, intent(in) :: a, param2, gamma real*8 :: r real*8 :: gauss r = (x(1:ndim)  ts(1:ndim)) gauss = exp((gamma*r)**a) end function gauss end module kernel
%%%
%%% FILE testSRBF_mod.f90
module srbf implicit none contains subroutine weigth(nx, x, nts, ts, ndim, s, kernel, StocPar, param, coe, mat) integer :: i,j,k,l,m,n integer :: info integer :: nx, nts, ndim integer, dimension(nts) :: ipiv real*8, dimension(nts) :: w real*8, dimension(nts) :: s real*8, dimension(2) :: param real*8, dimension(nx,ndim) :: x real*8, dimension(nts,ndim) :: ts real*8, dimension(nx,nts) :: phi, mat real*8, dimension(nts) :: coe real*8 :: stocPar interface real*8 function kernel(x1, x2, n3, stov, p1, p2) integer, intent (in) :: n3 real*8, dimension(n3), intent(in) :: x1 real*8, dimension(n3), intent(in) :: x2 real*8, intent(in) :: stov, p1, p2 end function kernel end interface do i = 1, nx do j = 1, nts phi(i,j) = kernel(x(i,1:ndim), ts(j,1:ndim), ndim, stocPar, param(1), param(2)) end do end do w = s mat = phi call DGESV(nts,1,mat,nts,ipiv,w,nts,info) coe = w end subroutine weigth end module srbf
%%%
%%% MAIN PROGRAM test.f90
program MKRBFuse kernel use srbf implicit none !real*8 :: SelKer integer :: i,j,k integer, parameter :: n = 3 integer, parameter :: nKer = 2 real*8, dimension(2,2) :: ParBound, auxpar real*8, dimension(2) :: Bound real*8, dimension(n) :: Var, func real*8, dimension(n,nKer) :: coe real*8, dimension(n,n) :: mat !external SelKer interface real*8 function SelKer(ind) integer, intent (in) :: ind end function SelKer end interface Bound(1) = 0 Bound(2) = 5 ParBound(1,1) = 1 ParBound(1,2) = 5 ParBound(2,1) = 1 ParBound(2,2) = 5 auxpar(1,1) = 0 auxpar(1,2) = 0 auxpar(2,1) = 1 auxpar(2,2) = 1 var(:) = (/ 0., 2.5, 5. /) do i = 1, n func(i) = cos(3*Var(i)) * exp(.25*Var(i)); end do do i = 1, nKer call weigth(n,Var,n,Var,1,func,SelKer(i),2.0D0,auxpar,coe,mat) end do end program MKRBF function SelKer(indx) integer, intent(in) :: indx real*8 :: SelKer select case (indx) case (1) SelKer = poly case (2) SelKer = gauss end select return end function SelKer
%%%
I tried both with interface and with external but the program gives me the same error:
gfortran testKer_mod.f90 testSRBF_mod.f90 test.f90 llapack o test test.f90:46:38: call weigth(n,Var,n,Var,1,func,SelKer(i),2.0D0,auxpar,coe,mat) 1 Error: Expected a procedure for argument 'kernel' at (1)
How can I fix it?

How to create a network (graph) with the given degree distribution?
What I'm going to try to describe now actually comes from a different field of science, but I'll try to explain it using graph theory as analogue. So let's say I have some degree distribution (probability density). Now, my question is, if I want to create a network which would have the same degree distribution, what would be the fastest/easiest approach? And my problem is the code I'm working on is written in Fortran, so using NetworkX is not an option. The other problem (that comes from my field) is the limits due to maximum distance between nodes (they're already generated), but I think for now I can skip that.

In recursive Fortran subroutines, what is local to the individual call of the subroutine, and what is "global"?
I have the following simple code (which is an example of something I am trying to do in a much more complicated code):
module linkmod type link character(len=1) :: name type(link), pointer :: next => null() end type link contains recursive subroutine tell_name(a) type(link), target :: a type(link), pointer :: b !b is useless, except to illustrate my point integer :: num_outputs = 0 b => a if (associated(b%next)) call tell_name(b%next) print *, b%name num_outputs = num_outputs + 1 print *, num_outputs end subroutine tell_name end module linkmod program driver use linkmod type(link) :: a type(link), pointer :: b a%name = 'a' allocate(a%next) b => a%next b%name='b' allocate(b%next) b => b%next b%name = 'c' allocate(b%next) b => b%next b%name = 'd' call tell_name(a) end program
I compile and run and get the following output:
d 1 c 2 b 3 a 4
Now, I get lucky here because this is the behavior I need, namely, when a call to
tell_name()
is exited,b
in the calling instance has retained the value it had before the calling instance made the call, butnum_outputs
has been set to what it became in the called instance. (Apologies if that was hard to read. I tried my best there.)The thing is, I don't really understand why things work this way. Why do not both
b
andnum_outputs
retain the values they were assigned in the called instance? Or, alternatively, why do they not both reacquire the values they had in the calling instance, previous to the call?What is the general rule here? On the other hand, what if I wanted
num_outputs
in a calling instance to be unaffected by what happens in any called instance. How would I arrange that?(BTW, I wrote up an alternative version of the code where I replaced the
recursive subroutine
with a derived type procedure, called ascall b%next%tell_name()
I get the exact same behavior with it.)

passing a portion of an array of a derived type that contains an allocatable component
I'm having trouble when I pass a portion of an array of a derived type which has an allocatable component. A (probable) memory leaks seems to occur (unfortunatly valgrind is not yet available on mac 10.14/mojave, so I simply used a long do loop and looked the memory used with top).
Consider the following test program which reproduces my problem in a simpler context:
program test_leak implicit none type :: vint_t integer, allocatable :: col(:) end type vint_t type(vint_t) :: row(4) integer :: i, Ind(2)=[2,3] do i = 1, 4 allocate(row(i)%col(i)) end do row(1)%col(1) = 11 row(2)%col(1) = 21 ; row(2)%col(2) = 22 row(3)%col(1) = 31 ; row(3)%col(2) = 32 ; row(3)%col(3) = 33 row(4)%col(1) = 41 ; row(4)%col(2) = 42 ; row(4)%col(3) = 43 ; row(4)%col(4) = 44 do i = 1, 1000000 call do_nothing ( row(Ind) ) ! (version #A) with this version and with gfortran: memory grows with iter !call do_nothing ( row(2:3) ) ! (version #B) but not with this version if (mod(i,10000) == 0) then print*,i ; read* end if end do contains subroutine do_nothing ( a ) type(vint_t), intent(in) :: a(:) end subroutine do_nothing end program test_leak
 The problem occurs with gfortran (8.2) and not with ifort (19) nor with nagfor (6.2) (with nagfor and ifort the used memory is stable during the iterations and ca 400 Kb, while it reaches ca 30 Mb with gfortran!).
 A final procedure doesn't solve the problem.
 The problem disapears if the allocatable member "col" is replaced by an explicit array (say col(4)) or if the version #B is used.
 The problem also occurs with an allocatable character component.
 If I print "a" in the subroutine "do_nothing" both versions give the correct result.
Does anyone have some insight on this issue? Of course I can design the called subroutines by passing the array Ind (do_nothing(row, Ind)) but that would be somewhat a pity.