.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/example_openfoam.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.py: Run an OpenFOAM simulation ========================== .. GENERATED FROM PYTHON SOURCE LINES 5-19 .. code-block:: Python from pathlib import Path import time from remote_run.run import ( ExecutionContext, SlurmSchedulingEngine, GuixRunner, GitProject, remote, is_finished, SshExecution, ) from pyopenfoam.tutorials import generate_pitz_daily_case .. GENERATED FROM PYTHON SOURCE LINES 20-23 we use pyopenfoam to call OpenFOAM from python this means adding this channel to the guix channels. This can also be read from a file directly. .. GENERATED FROM PYTHON SOURCE LINES 23-48 .. code-block:: Python channels = """(list (channel (name 'guix) (url "https://git.savannah.gnu.org/git/guix.git") (branch "master") (commit "ee8be372972bc1e84b5870df738c6e0bbdd975ff") (introduction (make-channel-introduction "9edb3f66fd807b096b48283debdcddccfea34bad" (openpgp-fingerprint "BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA")))) (channel (name 'python-pyopenfoam) (url "https://gitlab.ost.ch/sce-floss/pyopenfoam.git") (branch "main") (commit "ab8b4130bd8e3c199e72b47c6ca1809011be3b5e")) (channel (name 'guix-ost) (url "https://gitlab.ost.ch/scl/guix-ost.git") (branch "main") (commit "2a64fa887f4a2069ea9426e423f55838bc8987e9")))""" .. GENERATED FROM PYTHON SOURCE LINES 49-50 define execution variables .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python num_cpus = 10 .. GENERATED FROM PYTHON SOURCE LINES 53-55 Define an execution context with a scheduling engine. For slurm you can pass slurm parameters directly here. .. GENERATED FROM PYTHON SOURCE LINES 55-79 .. 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=channels, ), scheduling_engine=SlurmSchedulingEngine( job_name="openfoam_sim", mail_type="ALL", mail_user="reza.housseini@ost.ch", ), ) .. GENERATED FROM PYTHON SOURCE LINES 80-81 define simulation variables .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python duration = 0.1 # seconds .. GENERATED FROM PYTHON SOURCE LINES 84-85 create an OpenFOAM case locally .. GENERATED FROM PYTHON SOURCE LINES 85-90 .. code-block:: Python pitz_daily_case = generate_pitz_daily_case(duration=duration, num_cpus=num_cpus) case_path = Path("pitz_daily_case") pitz_daily_case.write(folder=case_path) .. GENERATED FROM PYTHON SOURCE LINES 91-94 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 94-109 .. code-block:: Python @remote(execution_context) def sim_job(run_path=None): # call import statements here for modules needed remotely from pyopenfoam.cli.scripts import of_run import pyvista as pv remote_case_path = run_path / case_path.name of_run(remote_case_path) # return simulation data foam_file = run_path / (case_path.name + ".foam") foam_file.touch() reader = pv.POpenFOAMReader(foam_file) return reader.read() .. GENERATED FROM PYTHON SOURCE LINES 110-116 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. Inject the identifier keyword argument, it will be replaced with the :class:`Identifier` object holding the paths. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python job_id, result_func = sim_job(identifier=None) .. GENERATED FROM PYTHON SOURCE LINES 119-121 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 121-123 .. code-block:: Python time.sleep(10) .. GENERATED FROM PYTHON SOURCE LINES 124-125 we should check if the job id has finished before retrieving the result .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python if is_finished(execution_context, job_id): result = result_func() .. _sphx_glr_download_auto_examples_example_openfoam.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.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_openfoam.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_openfoam.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_