Tuesday, September 11, 2007

Using Tcl/TK to load files and generate input parameters

A question that comes up frequently in ParFlow is how to load in external parameters from a file. This could be for transient forcing for boundary conditions, for Monte Carlo simulation (in addition to changing a random seed) or even in calibration using parameters generated from another program, such as PEST. The example below was from a question recently asked by a ParFlow user and demonstrates how this can be done for a Monte Carlo simulation, though this same approach is adaptable to many other situations.

First, one would load in an array of values from a file using tcl. This is not in the ParFlow manual or addendum, but you can easily do this with native Tcl/TK commands. We use the example from the harvey_flow.tcl. Right now that script does something like this:

set fileId [open stats4.txt r 0600]
set kgu [gets $fileId]
set varu [gets $fileId]
set kgl [gets $fileId]
set varl [gets $fileId]
close $fileId

pfset Geom.upper_aquifer.Perm.GeomMean $kgu
pfset Geom.upper_aquifer.Perm.Sigma $varu
This allows us to provide parameters externally, such as from PEST for calibration (as was done in this example). To modify things, one could do something like this, say to load in file of n Kg's:

set fileId [open stats4.txt r 0600]
for {set i 1} {$i <= $n} {incr i 1} {
set kg($i) [gets $fileId]
}
close $fileId
Farther down in the harvey_flow.tcl script, where we loop over the realizations and change the random seed, we can also loop through the file of values we've read into the arrays for Kg:

#
# Loop through runs
#
for {set k 1} {$k <= $n_runs} {incr k 1} {
#
pfset Geom.upper_aquifer.Perm.Seed [ expr 33333+2*$k ]
pfset Geom.upper_aquifer.Perm.GeomMean $kg($k)
...

This approach should work for a number of circumstances as long as you can generate a text file of values outside of the ParFlow simulation script (or one could even generate some relationships within the Tcl script). This can easily be done in FORTRAN, in a spreadsheet or other mathematical program. This approach can easily be modified to load data for wells, boundary conditions or any transient condition as well.

6 comments:

Unknown said...

Thanks Reed! This is very helpful. If I have more than one parameter: say K_G and VarY, how does the structure of the input file looks like? Two columns or should I write a different file for each parameter? Thanks again.
Felipe

Reed Maxwell said...

Hi Felipe-

There are a couple different ways to do this. Probably the easiest would be to have two files and use the template provided to read in Kg, then duplicate it for VarY. You could have two loops and two files, one loop and two files or one file with Kg and varY interspersed-- it's up to you. The end result is that you want two arrays of n values (where n is the number of realizations) for each parameter. Later on in the loop where you assign the random seed and Kg, you would add the assignment of varY as well, then run ParFlow and repeat.

Hope this helps!
-Reed

Reed Maxwell said...

Here's a bit more detail:

Currently the script reads a file set up as:
kg1
kg2
kg3
...


However, if you write your text file more like the original harvey_flow.tcl script, then the variables would be interspersed:
kg1
varY1
kg2
varY2
kg3
varY3
...


Since you are doing this in tcl, and I don't think tcl has something like the FORTRAN format command, to read from two columns of data you would read the entire line into a dummy array then split it up using array commands. Probably easiest to intersperse.

Johnsaib said...

Thanks admin for sharing this information.
TCL Price in Pakistan

fa said...

I really like this blog, it is really informative, I’ll be looking forward for your next blog on bed sheet supplier in pakistan and bed sheet exporter in Pakistan

Winstore.pk said...

Thank you very much for sharing this fantastic article.

laptop on installments
tcl 50 inch led price in pakistan
riwayat jewellery
mini laptop price in Pakistan
gree Haier PEL inverter AC price in Lahore Pakistan