.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/simulator/plot_run_ravaflow3G.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_simulator_plot_run_ravaflow3G.py: RunSimulator: Ravaflow3G Mixture Model ====================================== .. GENERATED FROM PYTHON SOURCE LINES 8-11 This example shows how to run multiple Ravaflow3GMixture simulations in serial and parallelly using :class:`.RunSimulator`. .. GENERATED FROM PYTHON SOURCE LINES 14-15 First, we import the class :class:`.Ravaflow3GMixture`. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: default from psimpy.simulator import Ravaflow3GMixture .. GENERATED FROM PYTHON SOURCE LINES 20-23 To create an instance of this class, we must specify the parameter ``dir_sim``. It represents the directory in which output files generated by r.avaflow will be saved. .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: default import os # Here we create a folder called `temp1_run_Ravaflow3GMixture_example` to save # output files generated by r.avaflow cwd = os.getcwd() if not os.path.exists('temp1_run_Ravaflow3GMixture_example'): os.mkdir('temp1_run_Ravaflow3GMixture_example') dir_sim = os.path.join(cwd, 'temp1_run_Ravaflow3GMixture_example') .. GENERATED FROM PYTHON SOURCE LINES 35-38 Given ``dir_sim``, we can create an instance of :class:`.Ravaflow3GMixture`. To reduce simulation time, we set ``time_end`` to :math:`50`. Other parameters are set to their default values. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default voellmy_model = Ravaflow3GMixture(dir_sim, time_end=50) .. GENERATED FROM PYTHON SOURCE LINES 43-45 The `simulator` in this example is defined based on ``voellmy_model`` as follows. It takes required inputs and returns the overall impact area. .. GENERATED FROM PYTHON SOURCE LINES 46-59 .. code-block:: default import numpy as np def simulator(prefix, elevation, hrelease, basal_friction, turbulent_friction, EPSG): """Preprocess required inputs, run simulation, and return output as a numpy array.""" grass_location, sh_file = voellmy_model.preprocess(prefix=prefix, elevation=elevation, hrelease=hrelease, basal_friction=basal_friction, turbulent_friction=turbulent_friction, EPSG=EPSG) voellmy_model.run(grass_location, sh_file) # run this line, r.avaflow will write outputs to dir_sim impact_area = voellmy_model.extract_impact_area(prefix) return np.array([impact_area]) # define dir_out and set save_out to True, returned numpy array will be saved to dir_out .. GENERATED FROM PYTHON SOURCE LINES 60-63 To demonstrate how to run multiple simulations using :class:`.RunSimulator`, we choose ``basal_friction`` and ``turbulent_friction`` as variable input parameters. Each has two different values, leading to four simulations. .. GENERATED FROM PYTHON SOURCE LINES 64-74 .. code-block:: default import itertools var_inp_parameter = ['basal_friction', 'turbulent_friction'] basal_friction = [20, 30] turbulent_friction = [3, 4] var_samples = np.array( [x for x in itertools.product(basal_friction, turbulent_friction)]) .. GENERATED FROM PYTHON SOURCE LINES 75-78 Other parameters of the `simulator`, including ``elevation``, ``hrelease``, and ``EPSG`` are treated as fixed input. It means that their values are the same in all simulations. .. GENERATED FROM PYTHON SOURCE LINES 79-85 .. code-block:: default dir_data = os.path.abspath('../../../tests/data/') elevation = os.path.join(dir_data, 'synthetic_topo.tif') hrelease = os.path.join(dir_data, 'synthetic_rel.tif') fix_inp = {'elevation': elevation, 'hrelease': hrelease, 'EPSG': '2326'} .. GENERATED FROM PYTHON SOURCE LINES 86-91 The parameter ``prefix`` of the `simulator` is special. It is not involved in the computational model, but only used to name output files generated by r.avaflow. Such parameter is not defined in ``var_inp_parameter`` or ``fix_inp`` of :class:`.RunSimulator`. Instead, we use a seperate parameter, called ``o_parameter`` for this purpose. .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. code-block:: default o_parameter = 'prefix' .. GENERATED FROM PYTHON SOURCE LINES 97-100 We may want to save outputs returned by the `simulator` at each simulation for later inspection or processing. In that case, we need to define ``dir_out`` and set ``save_out`` as `True`. .. GENERATED FROM PYTHON SOURCE LINES 101-109 .. code-block:: default import os cwd = os.getcwd() if not os.path.exists('temp2_run_Ravaflow3GMixture_example'): os.mkdir('temp2_run_Ravaflow3GMixture_example') dir_out = os.path.join(cwd, 'temp2_run_Ravaflow3GMixture_example') .. GENERATED FROM PYTHON SOURCE LINES 110-132 .. note:: Please note that ``dir_out`` and ``dir_sim`` are two different parameters for different purposes. - ``dir_out`` is a parameter of :class:`.RunSimulator`. The `simulator` returns output of interest as a numpy array for each simulation. If we want to save this returned output of interest (here impact area) to our local machine, we need to specify the value of ``dir_out`` which represents the directory in which output of interest will be saved and set ``save_out`` to `True`. Otherwise, we do not need ``dir_out`` and leave ``save_out`` to `False`. - ``dir_sim`` is a parameter of :class:`.Ravaflow3GMixture`. :class:`.Ravaflow3GMixture` relies on the third party software r.avaflow 2.4. When we call :py:meth:`.Ravaflow3GMixture.run` in the function body of above `simulator`, r.avaflow 2.4 will be run and it generates output files. The value of ``dir_sim`` specifies the directory in which output files generated by r.avaflow 2.4 are going to be saved. The value of ``dir_out`` and ``dir_sim`` can be the same if file names have no conflict. We recommend to keep them seperate. In addition, if the function body of the `simulator` does not write files to disk, ``dir_sim`` is not required. Our :class:`.MassPointModel` is an example. Similarly, if we do not want to save returned numpy array of the `simulator`, ``dir_out`` is not needed. .. GENERATED FROM PYTHON SOURCE LINES 135-136 Now we can define an object of :class:`.RunSimulator` by .. GENERATED FROM PYTHON SOURCE LINES 137-144 .. code-block:: default from psimpy.simulator import RunSimulator run_simulator = RunSimulator(simulator, var_inp_parameter, fix_inp, o_parameter, dir_out, save_out=True) .. GENERATED FROM PYTHON SOURCE LINES 145-148 Before running simulations, we need to specify values of ``prefixes`` which will be used to name files generated by each r.avaflow simulation and each returned numpy array of `simulator`. .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: default serial_prefixes = ["serial"+str(i) for i in range(len(var_samples))] parallel_prefixes = ["parallel"+str(i) for i in range(len(var_samples))] .. GENERATED FROM PYTHON SOURCE LINES 154-157 Now we can use :py:meth:`.RunSimulator.serial_run` method or :py:meth:`.RunSimulator.parallel_run` method to run the simulations in serial or parallelly. .. GENERATED FROM PYTHON SOURCE LINES 158-181 .. code-block:: default import time start = time.time() run_simulator.serial_run(var_samples=var_samples, prefixes=serial_prefixes) serial_time = time.time() - start serial_output = run_simulator.outputs print(f"serial_output: {serial_output}") start = time.time() # append setting to True means that simulation outputs of parallel run will be # appended to above serial run outputs run_simulator.parallel_run(var_samples, prefixes=parallel_prefixes, max_workers=2, append=True) parallel_time = time.time() - start parallel_output = run_simulator.outputs[len(var_samples):] print(f"parallel_output: {parallel_output}") print("Serial run time: ", serial_time) print("Parallel run time: ", parallel_time) .. rst-class:: sphx-glr-script-out .. code-block:: none serial_output: [array([942800.]), array([1425600.]), array([640400.]), array([838400.])] parallel_output: [array([942800.]), array([1425600.]), array([640400.]), array([838400.])] Serial run time: 49.1287362575531 Parallel run time: 29.414560079574585 .. GENERATED FROM PYTHON SOURCE LINES 182-183 All output files returned by the `simulator` are .. GENERATED FROM PYTHON SOURCE LINES 184-187 .. code-block:: default os.listdir(dir_out) .. rst-class:: sphx-glr-script-out .. code-block:: none ['parallel1_output.npy', 'parallel0_output.npy', 'serial2_output.npy', 'parallel3_output.npy', 'serial3_output.npy', 'serial0_output.npy', 'parallel2_output.npy', 'serial1_output.npy'] .. GENERATED FROM PYTHON SOURCE LINES 188-190 All output files/directories generated by r.avaflow simulations (including files/directories generated during preprocessing) are .. GENERATED FROM PYTHON SOURCE LINES 191-195 .. code-block:: default os.listdir(dir_sim) .. rst-class:: sphx-glr-script-out .. code-block:: none ['parallel1_results', 'parallel3_glocation', 'parallel0_results', 'serial1_results', 'serial2_results', 'serial2_glocation', 'serial1_glocation', 'parallel0_shell.sh', 'parallel1_shell.sh', 'parallel3_shell.sh', 'serial3_shell.sh', 'parallel2_glocation', 'parallel3_results', 'serial1_shell.sh', 'serial2_shell.sh', 'parallel2_results', 'parallel1_glocation', 'serial3_results', 'serial0_results', 'parallel0_glocation', 'serial0_shell.sh', 'serial3_glocation', 'parallel2_shell.sh', 'serial0_glocation'] .. GENERATED FROM PYTHON SOURCE LINES 196-202 .. warning:: If one simulation failed due to whatever reason, the error massage will be printed to the screen but other simulations will continue. In that case, the output file of failed simulation will not be writted to ``dir_out``. Also, the element of :py:attr:`.RunSimulator.outputs` corresponding to that simulation will be a string representing the error message, instead of a numpy array. .. GENERATED FROM PYTHON SOURCE LINES 205-207 In the end, we delete the folder `temp1_run_Ravaflow3GMixture_example` and `temp2_run_Ravaflow3GMixture_example` and all files therein. .. GENERATED FROM PYTHON SOURCE LINES 208-212 .. code-block:: default import shutil shutil.rmtree(dir_sim) shutil.rmtree(dir_out) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 1 minutes 18.603 seconds) .. _sphx_glr_download_auto_examples_simulator_plot_run_ravaflow3G.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_run_ravaflow3G.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_run_ravaflow3G.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_