1

I am new to fortran and I am having issues trying to pass in a argument via command line. For instance my working code has the following lines:

!experimental parameters
real (kind=8), parameter :: rhot                =1.2456
!density of top fluid
real (kind=8), parameter :: rhob                = 1.3432
!density of bottom fluid
real (kind=8), parameter :: rhof                = (rhot-rhob)
!generic function of rhot rhob

And I would like to hand them in via:

./fulltime_2009_05_15_Fortran9 [Value rhot] [Value rhob]

using something like:

call get_command_argument(1,input1)
call get_command_argument(2,input2)
read(input1,*) rhot
read(input2,*) rhob

The issue is, I declare parameters like rhof that depend on the inputted values. So I would like to have the user inputted values applied immediately so that all dependent parameters can use those values. However, if modify my code to be:

real (kind=8) :: rhot,  rhoB
call get_command_argument(1,input1)
call get_command_argument(2,input2)
read(input1,*) rhot
read(input2,*) rhob
real (kind=8), parameter :: rhof    = (rhot-rhob)

I get the error: A specification statement cannot appear in the executable section.

Any thoughts or suggestions for how I could address this issue?

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
FluidMan
  • 25
  • 4
  • If you are new to Fortran, do not learn `kind=8`, see https://stackoverflow.com/questions/838310/fortran-90-kind-parameter – Vladimir F Героям слава Apr 19 '21 at 15:21
  • Why do you need `rhof` to be a `parameter`? – veryreverie Apr 19 '21 at 15:27
  • @VladimirF I am modifying a large code that I didn't create. So the original creator was using that and it pops up in numerous places. Sounds like it could cause issues, but I am afraid to remove it from the 20+ cases it pops up. Do you recommend I should? – FluidMan Apr 19 '21 at 15:47
  • @veryreverie Not sure. I honestly don't understand why I would ever specify it as a parameter as opposed to a assignable variable (not sure if I am saying that right). For instance, I am not sure why I don't always just write: real (kind=8) :: rhof = (rhot-rhob) ? – FluidMan Apr 19 '21 at 15:49
  • @FluidMan the `parameter` keyword means that a variable is constant and known at compile time. See e.g. [this documentation](https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnaj/index.html). If you want to set `rhof` at runtime, then you don't want this. – veryreverie Apr 19 '21 at 15:51
  • Pi would be an example of a parameter. And the A=Pi*R^2 would have R as an input variable and A as a variable. – Holmz Apr 20 '21 at 21:44

1 Answers1

4

A compile time constant (parameter) cannot be changed by command line arguments. It is fixed at compile time.

This code:

real (kind=8) :: rhot,  rhoB
real (kind=8) :: rhof
call get_command_argument(1,input1)
call get_command_argument(2,input2)
read(input1,*) rhot
read(input2,*) rhob
rhof    = (rhot-rhob)

would compile fine. But you cannot have a variable declaration after a normal statement.

You could have

real (kind=8) :: rhot,  rhoB
call get_command_argument(1,input1)
call get_command_argument(2,input2)
read(input1,*) rhot
read(input2,*) rhob

block
  real (kind=8) :: rhof    = (rhot-rhob)

in Fortran 2008, but you cannot define a compile-time constant using a non-constant expression.

Some programming langauges do allow a type of constants that are set during the configuration of the run and are fixed thereafter. Fortran is not one of them.