Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with namelist and dynamic allocatable arrays

Status
Not open for further replies.

grad2009

Technical User
May 27, 2009
1
US
Hi
I wanted to use namelist to group my variables that are users-specified inputs to run a simulation, but I later found out that the variables in namelist can not be dynamic allocatable arrays.
I really like the idea of "namelist". Does any fortran expert here know how to deal with these dynamic allocatable arrays using idea of "namelist"?
 
If I need to have more variables under one name available, I define my own data type - a data structure.
There is no problem with allocatable arrays - see this example:

dtypes_example.f90
Code:
[COLOR=#a020f0]module[/color] dtypes
  [COLOR=#2e8b57][b]type[/b][/color] dstruct01
    [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]25[/color]         :: string25
    [COLOR=#2e8b57][b]integer[/b][/color]              :: int_num
[COLOR=#2e8b57][b]    real[/b][/color]                 :: real_num    
    [COLOR=#2e8b57][b]integer[/b][/color]              :: int_vector([COLOR=#ff00ff]3[/color])
[COLOR=#2e8b57][b]    real[/b][/color]                 :: real_vector([COLOR=#ff00ff]3[/color])    
  [COLOR=#2e8b57][b]end type[/b][/color]

  [COLOR=#2e8b57][b]type[/b][/color] dstruct02
    [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]25[/color]         :: string25
    [COLOR=#2e8b57][b]integer[/b][/color]              :: int_num
[COLOR=#2e8b57][b]    real[/b][/color]                 :: real_num    
    [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color] :: int_vector(:)
[COLOR=#2e8b57][b]    real[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color]    :: real_vector(:)
  [COLOR=#2e8b57][b]end type[/b][/color]
[COLOR=#a020f0]end module[/color] dtypes

[COLOR=#a020f0]program[/color] dtypes_example
  [COLOR=#a020f0]use[/color] dtypes

  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#2e8b57][b]type[/b][/color](dstruct01) :: ds01
  [COLOR=#2e8b57][b]type[/b][/color](dstruct02) :: ds02
  [COLOR=#2e8b57][b]integer[/b][/color] :: j, int_vector_dim [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]5[/color], real_vector_dim [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3[/color]

  [COLOR=#0000ff]! Data Structure 01[/color]
  ds01%string25 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'Data Structure 01:'[/color]
  ds01%int_num  [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]100[/color]
  ds01%real_num [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]123.456[/color]
  ds01%int_vector [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]10[/color], [COLOR=#ff00ff]20[/color], [COLOR=#ff00ff]30[/color][COLOR=#804040][b]/[/b][/color])
  ds01%real_vector [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1.41[/color], [COLOR=#ff00ff]2.71[/color], [COLOR=#ff00ff]3.14[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds01 
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

  [COLOR=#0000ff]! Data Structure 02[/color]
  ds02%string25 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'Data Structure 02:'[/color]
  ds02%int_num  [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]100[/color]
  ds02%real_num [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]123.456[/color]
  [COLOR=#804040][b]allocate[/b][/color](ds02%int_vector(int_vector_dim))
  ds02%int_vector [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]10[/color], [COLOR=#ff00ff]20[/color], [COLOR=#ff00ff]30[/color], [COLOR=#ff00ff]40[/color], [COLOR=#ff00ff]50[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]allocate[/b][/color](ds02%real_vector(real_vector_dim))
  ds02%real_vector [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1.414[/color], [COLOR=#ff00ff]2.718[/color], [COLOR=#ff00ff]3.141[/color][COLOR=#804040][b]/[/b][/color])
  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds02%string25 
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds02%int_num
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds02%real_num
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], int_vector_dim
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds02%int_vector(j)
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], real_vector_dim
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) ds02%real_vector(j)
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] dtypes_example
Output:
Code:
$ g95 dtypes_example.f90 -o dtypes_example

$ dtypes_example
 Data Structure 01:        100 123.456 10 20 30 1.41 2.71 3.14

 Data Structure 02:       
 100
 123.456
 10
 20
 30
 40
 50
 1.414
 2.718
 3.141
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top