Skip to content

Commit

Permalink
remove _*.defaults for problems and use a dict instead (#255)
Browse files Browse the repository at this point in the history
Now the problem parameters are specified in a dict in the problem's python module, instead of separately in a file that needs to be parsed. This will make it easier to define a new problem completely in Jupyter, since we can just create the dict on our own.
  • Loading branch information
zingale authored Sep 6, 2024
1 parent 633f394 commit 9c65825
Show file tree
Hide file tree
Showing 75 changed files with 356 additions and 332 deletions.
121 changes: 97 additions & 24 deletions docs/source/problems.rst
Original file line number Diff line number Diff line change
@@ -1,27 +1,100 @@
Adding a problem
================

The easiest way to add a problem is to copy an existing problem setup
in the solver you wish to use (in its problems/ sub-directory). Three
different files will need to be copied (created):

* ``problem.py``: this is the main initialization routine. The
function ``init_data()`` is called at runtime by the ``Simulation``
object's ``initialize()`` method. Two arguments are passed in, the
simulation's ``CellCenterData2d`` object and the
``RuntimeParameters`` object. The job of ``init_data()`` is to fill
all of the variables defined in the ``CellCenterData2d`` object.

* ``_problem.defaults``: this contains the runtime parameters and
their defaults for your problem. They should be placed in a block
with the heading ``[problem]`` (where ``problem`` is your problem's
name). Anything listed here will be available through the
``RuntimeParameters`` object at runtime.

* ``inputs.problem``: this is the inputs file that is used at runtime
to set the parameters for your problem. Any of the general
parameters (like the grid size, boundary conditions, etc.) as well
as the problem-specific parameters can be set here. Once the
problem is defined, you need to add the problem name to the
``__all__`` list in the ``__init__.py`` file in the ``problems/``
sub-directory. This lets python know about the problem.
Problem setups are defined in a ``problems/`` subdirectory under each
solver. For example, the problem setups for ``compressible`` are here:
https://github.com/python-hydro/pyro2/tree/main/pyro/compressible/problems

When you install pyro via ``pip``, these problem setups become available.
At the moment, the way to add a new problem is to directly put files
into these directories.

.. tip::

If you are working on adding problems, it is
recommended that you install pyro from source and do it as an
`editable install
<https://setuptools.pypa.io/en/latest/userguide/development_mode.html>`_
as:

.. prompt:: bash

pip install -e .

Every problem needs a python module of the form *problem_name.py*.
This will define the runtime parameters that the problem expects
and do the initialization of the state variables.

Many problems will also provide an *inputs* file that overrides
some of the runtime parameter defaults (like the domain size and BCs)
to be appropriate for this problem.

"`problem.py`"
--------------

A python module named after the problem (we'll call it ``problem.py`` here)
provides the following:

* ``PROBLEM_PARAMS`` : this is a dictionary (it can be empty, ``{}``)
that defines the runtime parameters that are needed to this problem
setup. For instance, for the ``compressible`` ``sod`` problem:

.. code:: python
PROBLEM_PARAMS = {"sod.direction": "x",
"sod.dens_left": 1.0,
"sod.dens_right": 0.125,
"sod.u_left": 0.0,
"sod.u_right": 0.0,
"sod.p_left": 1.0,
"sod.p_right": 0.1}
Each key in the dictionary should be of the form
*problem-name.parameter*, and the values are the default value of
the parameter.

Any of these runtime parameters can be overridden in an inputs file
or on the commandline (when running via ``pyro_sim.py``) or via the
``inputs_dict`` keyword argument (when running via the
:func:`Pyro <pyro.pyro_sim.Pyro>` class).

* ``DEFAULT_INPUTS`` : this is the name of an inputs file to be
read in by default when using the ``Pyro`` class interface. It
can be ``None``.

This is not used when running via ``pyro_sim.py``.

* ``init_data()`` : this is the main initialization routine. It has
the signature:

.. code:: python
def init_data(my_data, rp)
where

* ``my_data`` is a :func:`CellCenterData2d <pyro.mesh.patch.CellCenterData2d>` or :func:`FV2d <pyro.mesh.fv.FV2d>` object. The ``Grid`` object can be obtained from this
as needed.

* ``rp`` is a :func:`RuntimeParameters <pyro.util.runparams.RuntimeParameters>` object.
Any of the runtime parameters (including the problem-specific ones
defined via ``PROBLEM_PARAMS``) can be accessed via this.

.. note::

The interface for ``init_data`` is the same for all solvers.

The job of ``init_data`` is to initialize the state data that is
managed by the ``my_data`` object passed in. Exactly which variables
are included there will depend on the solver.

* ``finalize()`` : this is called at the very end of evolution. It
is meant to output instructions to the user on how the can analyze the
data. It takes no arguments.

.. important::

Once the problem is defined, you need to add the problem name to
the ``__all__`` list in the ``__init__.py`` file in the
``problems/`` sub-directory. This lets python know about the
problem.
2 changes: 0 additions & 2 deletions pyro/advection/problems/_smooth.defaults

This file was deleted.

2 changes: 0 additions & 2 deletions pyro/advection/problems/_tophat.defaults

This file was deleted.

2 changes: 2 additions & 0 deletions pyro/advection/problems/smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

DEFAULT_INPUTS = "inputs.smooth"

PROBLEM_PARAMS = {}


def init_data(my_data, rp):
""" initialize the smooth advection problem """
Expand Down
2 changes: 2 additions & 0 deletions pyro/advection/problems/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

DEFAULT_INPUTS = None

PROBLEM_PARAMS = {}


def init_data(my_data, rp):
""" an init routine for unit testing """
Expand Down
2 changes: 2 additions & 0 deletions pyro/advection/problems/tophat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

DEFAULT_INPUTS = "inputs.tophat"

PROBLEM_PARAMS = {}


def init_data(myd, rp):
""" initialize the tophat advection problem """
Expand Down
3 changes: 0 additions & 3 deletions pyro/advection_nonuniform/problems/_slotted.defaults

This file was deleted.

3 changes: 3 additions & 0 deletions pyro/advection_nonuniform/problems/slotted.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

DEFAULT_INPUTS = "inputs.slotted"

PROBLEM_PARAMS = {"slotted.omega": 0.5, # angular velocity
"slotted.offset": 0.25} # offset of the slot center from domain center


def init_data(my_data, rp):
""" initialize the slotted advection problem """
Expand Down
2 changes: 2 additions & 0 deletions pyro/advection_nonuniform/problems/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

DEFAULT_INPUTS = None

PROBLEM_PARAMS = {}


def init_data(my_data, rp):
""" an init routine for unit testing """
Expand Down
2 changes: 2 additions & 0 deletions pyro/burgers/problems/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

DEFAULT_INPUTS = "inputs.test"

PROBLEM_PARAMS = {}


def init_data(myd, rp):
""" initialize the burgers test problem """
Expand Down
5 changes: 0 additions & 5 deletions pyro/compressible/problems/_acoustic_pulse.defaults

This file was deleted.

3 changes: 0 additions & 3 deletions pyro/compressible/problems/_advect.defaults

This file was deleted.

11 changes: 0 additions & 11 deletions pyro/compressible/problems/_bubble.defaults

This file was deleted.

6 changes: 0 additions & 6 deletions pyro/compressible/problems/_gresho.defaults

This file was deleted.

4 changes: 0 additions & 4 deletions pyro/compressible/problems/_hse.defaults

This file was deleted.

10 changes: 0 additions & 10 deletions pyro/compressible/problems/_kh.defaults

This file was deleted.

1 change: 0 additions & 1 deletion pyro/compressible/problems/_logo.defaults

This file was deleted.

33 changes: 0 additions & 33 deletions pyro/compressible/problems/_quad.defaults

This file was deleted.

19 changes: 0 additions & 19 deletions pyro/compressible/problems/_ramp.defaults

This file was deleted.

10 changes: 0 additions & 10 deletions pyro/compressible/problems/_rt.defaults

This file was deleted.

10 changes: 0 additions & 10 deletions pyro/compressible/problems/_rt2.defaults

This file was deleted.

4 changes: 0 additions & 4 deletions pyro/compressible/problems/_sedov.defaults

This file was deleted.

13 changes: 0 additions & 13 deletions pyro/compressible/problems/_sod.defaults

This file was deleted.

3 changes: 3 additions & 0 deletions pyro/compressible/problems/acoustic_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

DEFAULT_INPUTS = "inputs.acoustic_pulse"

PROBLEM_PARAMS = {"acoustic_pulse.rho0": 1.4,
"acoustic_pulse.drho0": 0.14}


def init_data(myd, rp):
"""initialize the acoustic_pulse problem. This comes from
Expand Down
2 changes: 2 additions & 0 deletions pyro/compressible/problems/advect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

DEFAULT_INPUTS = "inputs.advect.64"

PROBLEM_PARAMS = {}


def init_data(my_data, rp):
""" initialize a smooth advection problem for testing convergence """
Expand Down
8 changes: 8 additions & 0 deletions pyro/compressible/problems/bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

DEFAULT_INPUTS = "inputs.bubble"

PROBLEM_PARAMS = {"bubble.dens_base": 10.0, # density at the base of the atmosphere
"bubble.scale_height": 2.0, # scale height of the isothermal atmosphere
"bubble.x_pert": 2.0,
"bubble.y_pert": 2.0,
"bubble.r_pert": 0.25,
"bubble.pert_amplitude_factor": 5.0,
"bubble.dens_cutoff": 0.01}


def init_data(my_data, rp):
""" initialize the bubble problem """
Expand Down
5 changes: 5 additions & 0 deletions pyro/compressible/problems/gresho.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

DEFAULT_INPUTS = "inputs.gresho"

PROBLEM_PARAMS = {"gresho.rho0": 1.0, # density in the domain
"gresho.r": 0.2, # radial location of peak velocity
"gresho.p0": 59.5, # ambient pressure in the domain
"gresho.t_r": 1.0} # reference time (used for setting peak velocity)


def init_data(my_data, rp):
""" initialize the Gresho vortex problem """
Expand Down
3 changes: 3 additions & 0 deletions pyro/compressible/problems/hse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

DEFAULT_INPUTS = "inputs.hse"

PROBLEM_PARAMS = {"hse.dens0": 1.0,
"hse.h": 1.0}


def init_data(my_data, rp):
""" initialize the HSE problem """
Expand Down
Loading

0 comments on commit 9c65825

Please sign in to comment.