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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a control file

Status
Not open for further replies.

archimedes80

Technical User
May 3, 2010
11
DE
Hello,

i'm interesting in read in the following "control file" for my program that solves sparse matrices, penta or hepta diagonal matrices:

SOLVER:
Iterative Method (GMRES/GAUSS-SEIDEL/JACOBI): JACOBI
Iterations : 300
Dynamic Breakcriteria (yes/no) : yes
Absolute Breakcriteria : 10e-7
PRECONDITIONER:
Preconditioning (yes/no) : yes
Method (LU/ILU) : ILU
ILU:
Factor : 0.05
END ILU
END PRECONDITIONER
END SOLVER
SIMULATION:
Simulation Type (steady/Transient) : transient
TRANSIENT:
Number of Timesteps :100
Timestep :0.001
END TRANSIENT
END SIMULATION

does someone have an idea how to do that in fortran 95 programming style (modular programming or Object Oriented)?
Thank you in advance,

archimedes80
 
Do you mean read the control file and save the options or solve the matrices?
 
ohh i'm sorry that i haven't specified it.
i would like to read the file and save for example the flag "transient" in a logical variable. It should be used like a switcher for the main program.

Furthermore, the number of iterations for example should be saved in an integer for example.

i just need an example because i'm out of fantasy!

thanky ou in advance
 
You need to write a procedure for parsing the config file. For example, for your file config_file.txt something like this does the job:
config_file.f95
Code:
[COLOR=#a020f0]program[/color] config_file
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

[COLOR=#2e8b57][b]integer[/b][/color] stat
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]80[/color]) :: line
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]10[/color]) :: iter_method
[COLOR=#2e8b57][b]integer[/b][/color] :: nr_iters
[COLOR=#2e8b57][b]real[/b][/color] :: time_step

[COLOR=#0000ff]! open file[/color]
[COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]1[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'config_file.txt'[/color], [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
[COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'File cannot be opened !'[/color]
  [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(80A)'[/color]) [COLOR=#ff00ff]'*******************************************************'[/color]
[COLOR=#0000ff]! process file[/color]
[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] ([COLOR=#ff00ff].true.[/color]) 
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) line 
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(80A)'[/color]) line [COLOR=#0000ff]! write on screen  [/color]
  [COLOR=#a020f0]call[/color] process_line([COLOR=#008080]adjustl[/color]([COLOR=#008080]trim[/color](line)), iter_method, nr_iters, time_step)
[COLOR=#804040][b]enddo[/b][/color]

[COLOR=#0000ff]! close file[/color]
[COLOR=#6a5acd]99[/color] [COLOR=#804040][b]continue[/b][/color]
[COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(80A)'[/color]) [COLOR=#ff00ff]'*******************************************************'[/color]
[COLOR=#0000ff]! Print the parameters found in config file[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(80A)'[/color]) [COLOR=#ff00ff]'Config parameters found:'[/color] 
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Iterative Method     = '[/color], iter_method
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Number of Iterations = '[/color], nr_iters
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Time Step            = '[/color], time_step

[COLOR=#a020f0]end program[/color] config_file

[COLOR=#a020f0]subroutine[/color] process_line(line, iter_method, nr_iters, time_step)
  [COLOR=#0000ff]! parse settings from config line[/color]
  
  [COLOR=#0000ff]! line  = input line[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: line
  [COLOR=#0000ff]! output parameters[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]10[/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]out[/b][/color]) :: iter_method
  [COLOR=#2e8b57][b]integer[/b][/color] :: nr_iters
[COLOR=#2e8b57][b]  real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]out[/b][/color]) :: time_step
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: cfg_param
  
  [COLOR=#0000ff]! parameter part of config line[/color]
  cfg_param [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]adjustl[/color](line([COLOR=#008080]index[/color](line,[COLOR=#ff00ff]':'[/color])[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]:))
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]index[/color](line,[COLOR=#ff00ff]'Iterative Method'[/color]) [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    iter_method [COLOR=#804040][b]=[/b][/color] cfg_param
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]index[/color](line,[COLOR=#ff00ff]'Iterations'[/color]) [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color])  [COLOR=#804040][b]then[/b][/color]
    [COLOR=#0000ff]! convert substring into integer number[/color]
    [COLOR=#804040][b]read[/b][/color] (cfg_param,[COLOR=#ff00ff]'(I3)'[/color]) nr_iters    
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]index[/color](line,[COLOR=#ff00ff]'Timestep'[/color]) [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color])  [COLOR=#804040][b]then[/b][/color]
    [COLOR=#0000ff]! convert substring into real number[/color]
    [COLOR=#804040][b]read[/b][/color] (cfg_param,[COLOR=#ff00ff]'(F5.3)'[/color]) time_step      
  [COLOR=#804040][b]end if[/b][/color]
[COLOR=#a020f0]end subroutine[/color] process_line
Output:
Code:
$ g95 config_file.f95 -o config_file

$ config_file
*******************************************************
SOLVER:                                                                         
   Iterative Method (GMRES/GAUSS-SEIDEL/JACOBI): JACOBI                         
   Iterations                                  : 300                            
   Dynamic Breakcriteria (yes/no)              : yes                            
   Absolute Breakcriteria                      : 10e-7                          
   PRECONDITIONER:                                                              
      Preconditioning (yes/no)                 : yes                            
      Method (LU/ILU)                          : ILU                            
      ILU:                                                                      
         Factor                                : 0.05                           
      END ILU                                                                   
   END PRECONDITIONER                                                           
END SOLVER                                                                      
SIMULATION:                                                                     
   Simulation Type (steady/Transient)    : transient                            
   TRANSIENT:                                                                   
      Number of Timesteps                 :100                                  
      Timestep                            :0.001                                
   END TRANSIENT                                                                
END SIMULATION                                                                  
*******************************************************

Config parameters found:
 Iterative Method     = JACOBI    
 Number of Iterations =  300
 Time Step            =  0.001
I parsed from the config file only 3 of your settings of several datatypes: character, integer and real.
Enhance the procedure to parse all you need.
 
thank you very much! it's a good idea and i learned thinks i didn't know like "adjustl".
i will modify it and i'll tell you about it in the future!

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top