.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/example_openfoam_parameter_study.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_example_openfoam_parameter_study.py: Run an OpenFOAM parameter study =============================== .. GENERATED FROM PYTHON SOURCE LINES 5-20 .. code-block:: Python from pathlib import Path import time from remote_run.run import ( ExecutionContext, SlurmSchedulingEngine, GitProject, GuixRunner, remote, is_finished, SshExecution, ) from pyopenfoam.type_conversion import OFMultiValue from pyopenfoam.tutorials import generate_pitz_daily_case .. GENERATED FROM PYTHON SOURCE LINES 21-22 define execution variables .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python num_cpus = 5 .. GENERATED FROM PYTHON SOURCE LINES 25-27 create an OpenFOAM case study locally we are going to do a parameter study of the inlet velocity .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python study_path = Path("study_pitz_daily") velocities_x = range(8, 12) array_task_ids = range(len(velocities_x)) .. GENERATED FROM PYTHON SOURCE LINES 32-33 define a case path for each array task id .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python case_paths_lookup = { array_task_id: study_path / str(array_task_id) for array_task_id in array_task_ids } .. GENERATED FROM PYTHON SOURCE LINES 38-40 Define an execution context with a scheduling engine. For slurm you can pass slurm parameters directly here. .. GENERATED FROM PYTHON SOURCE LINES 40-65 .. code-block:: Python execution_context = ExecutionContext( execution=SshExecution( machine="shpc0003.ost.ch", working_directory=Path("/cluster/raid/home/reza.housseini"), ), num_cpus=num_cpus, project=GitProject(), runner=GuixRunner( dependencies=[ "python-pyvista", "python-pint", "python-pyopenfoam", "openfoam-org", "openmpi", ], channels=Path("channels.scm").read_text(), ), scheduling_engine=SlurmSchedulingEngine( job_name="openfoam_sim", mail_type="ALL", mail_user="reza.housseini@ost.ch", array=array_task_ids, ), ) .. GENERATED FROM PYTHON SOURCE LINES 66-67 define simulation variables .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python duration = 0.05 # seconds .. GENERATED FROM PYTHON SOURCE LINES 71-72 then adjust the inlet velocity and generate the cases .. GENERATED FROM PYTHON SOURCE LINES 72-103 .. code-block:: Python for velocity_x, array_task_id in zip(velocities_x, array_task_ids): pitz_daily_case = generate_pitz_daily_case(duration=duration, num_cpus=num_cpus) pitz_daily_case.files["U"].contents.update( { "boundaryField": { "inlet": { "type": "mappedInternalValue", "interpolationScheme": "cell", "average": [velocity_x, 0, 0], "value": OFMultiValue(("uniform", [velocity_x, 0, 0])), }, "outlet": { "type": "inletOutlet", "inletValue": OFMultiValue(("uniform", [0, 0, 0])), "value": OFMultiValue(("uniform", [0, 0, 0])), }, "upperWall": { "type": "fixedValue", "value": OFMultiValue(("uniform", [0, 0, 0])), }, "lowerWall": { "type": "fixedValue", "value": OFMultiValue(("uniform", [0, 0, 0])), }, "frontAndBack": {"type": "empty"}, } } ) pitz_daily_case.write(folder=case_paths_lookup[array_task_id]) .. GENERATED FROM PYTHON SOURCE LINES 104-107 decorate your functions you want to run in the specified execution context, in this case we want to execute our OpenFOAM case in parallel and return the field "alpha.liquid" .. GENERATED FROM PYTHON SOURCE LINES 107-122 .. code-block:: Python @remote(execution_context) def sim_job(run_path=None): # call import statements here for modules needed remotely from pyopenfoam.cli.commands import run_parallel import os array_task_id = int(os.getenv("SLURM_ARRAY_TASK_ID")) remote_case_path = run_path / case_paths_lookup[array_task_id] run_parallel( remote_case_path, num_cpus=num_cpus, reconstruct_fields=["U", "p"], ) .. GENERATED FROM PYTHON SOURCE LINES 123-127 this call will run on the remote machine specified in execution_context but due to the asynchronous nature of scheduling engines this will not return the result, instead you get the job id and a function to retrieve the result later. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python job_id, result_func = sim_job(remote=None) .. GENERATED FROM PYTHON SOURCE LINES 130-132 now we wait for the remote execution to finish before retrieving the result normally this step is decoupled when using a scheduler. .. GENERATED FROM PYTHON SOURCE LINES 132-134 .. code-block:: Python time.sleep(10) .. GENERATED FROM PYTHON SOURCE LINES 135-136 we should check if the job id has finished before retrieving the result .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python if is_finished(execution_context, job_id): result = result_func() .. _sphx_glr_download_auto_examples_example_openfoam_parameter_study.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_openfoam_parameter_study.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_openfoam_parameter_study.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_openfoam_parameter_study.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_