Skip to content

Argantonio65/pyWEST

Repository files navigation

pyWEST

Python scripts for water quality simulators in WEST (MIKE DHI).

  • Sensitivity analysis
  • Model Inference/Calibration
  • Automated simulation with control over inputs and model parameters

Dependencies

  • Ironpython
  • Python 2.7
  • TornadoNet

Set-up

  • Install Ironpython and set environment variable (ipy).

  • Install python 2.7 with the following libraries: numpy, pandas, os, time, timeit, json, shutil and sys

  • Download Parallel_WESTRunningPyWrapper.py and RunModel.py and set them in a project folder under \AutomatedCall\

Description

  • RunModel.py Is an IronPython script which interfaces with Tornado.net in order to call a WEST experiment.

  • Parallel_WESTRunningPyWrapper.py Python class which calls RunModel.py in parallel. Management of model resources. - Atributes:

              - self.InstanceNames = List the Model names
              - self.OccupationList = boolean list, 1 for occupied node, 0 for free
              - self.NumberofInstances = Number of total Model sessions (should not be higher than the number of local licenses or computer cores)
              - self.Paths = Project directory Path
              - self.threads_active = Number of threads initiated
    

Operation Automated_Call.py

  • Instanciate n models with:

    ModelInstance = ModelInstances(['Model_1', 'Model_2', 'Model_n'], modelPath = "<ProjectFolder>", modelInternalName = 'Model_dummy_1.Dynamic.ObjEval.Exp')

    You can also run one model in parallel at different processors:

    ModelInstances(['Model_dummy_1'], modelPath = "<ProjectFolder>", modelInternalName = 'Model_dummy_1.Dynamic.ObjEval.Exp')
  • Set Parameter values: e.g

    Timelist = {'StartTime':0,'StopTime':15}
    Parameterlist = {'n':0.07, 'KBODs':0.55, 'KLmin':0.1, 'KO2':1.6, 'SOD':1} 

    A Parameterlist is formed by a dictionary of parameter values. An entry of the form:

    • 'param' :value will set all parameters sharing the same name (even if it has been declared as a Top-level parameter) to the value specified.
    • 'param@submodel' :value will set the parameter 'param' only in the block model 'submodel'. This allows for local specifications.
    • 'param@Manipulated':value sets a manipulated variable value which doesn't carry any declared input. There is the option 'param@Manipulated#submodel':value to make the change only at submodel level.
    • 'param@Multiplier':value works by ofsetting its current saved value by a multiplyier value.
    • 'param@Multiplier':value works by ofsetting its current saved value by a multiplyier value.
    • 'inputname@Intput': Declare change of input file. Inputs should be added as: InputName@Input_DirectoryOfInputSamples Inputs in the directory should be labelled as Inputname_id (and all should be as txt)
  • Run Model:

    ModelInstance.model_run(Parameterlist, Timelist)

    Note: each call to model_run will use the next available Model instance in an availability basis. This will open a call in the background. The execution of the program will be halted until all simulations are finished.

  • Read outputs:

    output = ReadOutput('Model_dummy_1.Dynamic_water_quality.Simul.1.out.txt', ModelInstance.InstanceNames[0], TimeWindowStart = '01-01-2012 00:00:00', variable = '.River_5.DO')

    Note: Simulations produced by calling .model_run(Parameterlist, Timelist) store all output files in a new folder \SimulationOutput\ModelName\Sim_i. Each Sim_i contains: All output files from the WEST experiment which has .out. in their names. A txt file containing all iternal parameter values in the model <Internal_parameters.out.txt> and a file containing all changed parameter values from the Parameterlist in <External_parameters.txt>.

Operation Uncertainty_propagation2.py

This script automatizes a parameter sampling scheme provided by:

SamplingLayout = pd.read_csv('\pyWEST\500SamplesUP.txt', index_col = [0])

An example for the format is found at pyWEST\500SamplesUP.txt

The script performs a simulation for each row in the layout. It will call in parallel as many simulations as resources allocated through the parameter TreadingPool at:

ModelInstance.model_run(Parameterlist, Timelist, ThreadingPool = 4, ParamDatabase = os.path.join(repPath, '500SamplesUPReduced.txt'))

The function Read_SA_outputVariable loads the selected variable from the output folder.

Extra:

Notebooks

Included two jupyter notebooks containing:

  • Bayesian_Inference_Example.ipynb : Examples and tutorial to perform bayesian parameter inference to a West model.
  • Calibration_Example.ipynb : A tutorial for perform model calibration with scipy.minimize

About

Python scripts for water quality simulators WEST (MIKE DHI). Sensitivity analysis, inference and automated runs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published