Scripting a sequence of related simulations

140 views
Skip to first unread message

Owen Mays

unread,
Aug 31, 2020, 1:41:22 PM8/31/20
to gprMax-users
Dear gprMax community,

I'm running pairs of simulations: a forward model and a background model. The forward model includes a metal target in the domain, and the background does not include the target. Currently I have two separate .in files, one for the forward, one for the background. The only difference between them is whether the target is added.

Is there any way to have a single input file run two slightly different scenarios in sequence? One problem I see is that the outputs are named based on the input file, so I suspect the answer may be "no."
Alternatively, is there a way to have an input file "inherit" properties from a master file and then just add one item (the target)?


There are two drawbacks to my current approach of having two completely independent input files:
1) If I change something other than the target (grid size, number or location of antennas, waveform parameters, etc), I have to manually make those same changes in both files. Keeping the two files in sync is inefficient and error prone.
2) I have to manually run the two simulations in sequence. This is less of an issue; I could write a bash or python script to do that.

Any suggestions would be much appreciated!

-Owen

Andres Altieri

unread,
Aug 31, 2020, 3:47:55 PM8/31/20
to gprMax-users
Hi Owen,
I can tell you two options:
1) you can run invoke gprMax with the opcion "-n X" where X is the number of runs. Then in your code you can access the run number from the variable "current_model_run" and use and IF to make changes. Gprmax will output a .out for each run.
For example:
Invoke gprMax as "python -m gprMax owenscode.in -n 2"

Inside the code:
#python:

if current_model_run==1:
    changes without target
elif current_model_run==2:
    code withoutarget


2) For more complex setups you can generate a file with configurations and use "current_model_run" to select the setup.

Best regards,
Andrés

Owen Mays

unread,
Sep 4, 2020, 1:23:48 PM9/4/20
to gprMax-users
Hello Andrés,

Thank you for the suggestions. Using if statements in conjunction with current_model_run worked well.

Can you explain what you mean by your option two? How is this different from option 1? Is it possible to have sub-input files? (An input file that's loaded by another input file?) 

Best regards,
Owen

Andres Altieri

unread,
Sep 4, 2020, 1:53:03 PM9/4/20
to gprMax-users
Hi Owen,
glad to help.
What I mean by the second option is that if you have a model in which you need to sweep several parameters, for example, depth, width, dielectric constant or even the shape of the target, you can generate a file which contains this information and then read from the script this file and configure the simulation according to your needs.

For example, if you have three parameters, (width, height, depth) , and you want to use two configurations you could write a simple text file
with following content (what's between the line segments)
--------
width height depth
0.1 0.3 0.2
0.3 0.5 0.6
--------

Then from the script you can read the file using the np.genfromtxt
#python:
sim_table = np.genfromtxt('textfile.txt', skip_header=1)
width  = sim_table[current_model_run-1, 0]
height = sim_table[current_model_run-1, 1] 
depth= sim_table[current_model_run-1, 2]

#end_python:

Then  you can run the script with the option "-n 2" to run both combinations, one for each row of the txt file. This is better than option 1) because you don't have to change the .in file if you need to change the parameters, you just have to change the txt file. And the Txt file also serves as a log for the setup which may help.

---------
A more advanced alternative would be, instead storing the configuration in a .txt file, to use a more complex data structure, for example, you could use the pickle module in python. Then you can read from the pickle file the configurations and cycle through them with the current_model_run variable.


Best regards,
Andrés
Reply all
Reply to author
Forward
0 new messages