1

Is it possible with fortran to have a subroutine that accepts shape-varying input parameters ? I could not find a way...However I found a workaround which is to reshape the input array such as

      SUBROUTINE func(R1_TBV,...)

      DOUBLE PRECISION, dimension(:), allocatable :: R1_TBV ! type of boundary value 
      DOUBLE PRECISION R2_TBV(3,2), R3_TBV(3,2,3)

      SELECT CASE(trim(name))
      CASE('flux')
         allocate(r1_tbv(18))
         r3_tbv = reshape(r1_tbv,(/3,2,3/))
      CASE('volumic')
         allocate(r1_tbv(6))
         r2_tbv = reshape(r1_tbv,(/3,2/))
      END SELECT

but:

  1. I find strange not to have other choice to define all possible arrays because reshape can't do the work (i.e. tbv = reshape(tbv,(/3,2,3/) does not work...)
  2. when calling this function, my input parameter R1_TBV actually has either the size (3,2,3) or (3,2) so I also need beforehand to reshape it to call the function...
Romain
  • 133
  • 7
  • 3
    I'm not fully certain about what you are asking, but possible [one](https://stackoverflow.com/q/38058080/3157076) or [other](https://stackoverflow.com/q/39749056/3157076) [question](https://stackoverflow.com/q/16786965/3157076) helps you clarify? – francescalus Jul 10 '20 at 10:41
  • Yes, the first link helps, thanks ! So If I understand clearly, I only have to define an array TBV as "double precision TBV(..)" and then do a select case on the rank of TBV, is that right ? – Romain Jul 10 '20 at 10:49
  • 1
    To have assumed-rank, yes you'd have `double precision tbv(..)` and then use a `select rank` (rather than _case_). Otherwise, the other links cover generics (with different specific for each rank) and assumed size (`double precision tbv(*)`). – francescalus Jul 10 '20 at 11:27
  • Oh I did not realize there was a third link on "question" specifically. Thanks. – Romain Jul 10 '20 at 12:20
  • When I read the question I immediately thought about subroutine overloading (an interface of procedures with the same name, distinguishable from the shape of input array). The most elegant solution is probably an elemental function, if the constraints of such a pure function can be satisfied. `select rank` or `select case(rank())` should also work, although it is probably the last solution I would pick. – Pap Jul 12 '20 at 07:15

0 Answers0