Skip to content

Commit 9c65825

Browse files
authored
remove _*.defaults for problems and use a dict instead (#255)
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.
1 parent 633f394 commit 9c65825

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+356
-332
lines changed

docs/source/problems.rst

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,100 @@
11
Adding a problem
22
================
33

4-
The easiest way to add a problem is to copy an existing problem setup
5-
in the solver you wish to use (in its problems/ sub-directory). Three
6-
different files will need to be copied (created):
7-
8-
* ``problem.py``: this is the main initialization routine. The
9-
function ``init_data()`` is called at runtime by the ``Simulation``
10-
object's ``initialize()`` method. Two arguments are passed in, the
11-
simulation's ``CellCenterData2d`` object and the
12-
``RuntimeParameters`` object. The job of ``init_data()`` is to fill
13-
all of the variables defined in the ``CellCenterData2d`` object.
14-
15-
* ``_problem.defaults``: this contains the runtime parameters and
16-
their defaults for your problem. They should be placed in a block
17-
with the heading ``[problem]`` (where ``problem`` is your problem's
18-
name). Anything listed here will be available through the
19-
``RuntimeParameters`` object at runtime.
20-
21-
* ``inputs.problem``: this is the inputs file that is used at runtime
22-
to set the parameters for your problem. Any of the general
23-
parameters (like the grid size, boundary conditions, etc.) as well
24-
as the problem-specific parameters can be set here. Once the
25-
problem is defined, you need to add the problem name to the
26-
``__all__`` list in the ``__init__.py`` file in the ``problems/``
27-
sub-directory. This lets python know about the problem.
4+
Problem setups are defined in a ``problems/`` subdirectory under each
5+
solver. For example, the problem setups for ``compressible`` are here:
6+
https://github.com/python-hydro/pyro2/tree/main/pyro/compressible/problems
7+
8+
When you install pyro via ``pip``, these problem setups become available.
9+
At the moment, the way to add a new problem is to directly put files
10+
into these directories.
11+
12+
.. tip::
13+
14+
If you are working on adding problems, it is
15+
recommended that you install pyro from source and do it as an
16+
`editable install
17+
<https://setuptools.pypa.io/en/latest/userguide/development_mode.html>`_
18+
as:
19+
20+
.. prompt:: bash
21+
22+
pip install -e .
23+
24+
Every problem needs a python module of the form *problem_name.py*.
25+
This will define the runtime parameters that the problem expects
26+
and do the initialization of the state variables.
27+
28+
Many problems will also provide an *inputs* file that overrides
29+
some of the runtime parameter defaults (like the domain size and BCs)
30+
to be appropriate for this problem.
31+
32+
"`problem.py`"
33+
--------------
34+
35+
A python module named after the problem (we'll call it ``problem.py`` here)
36+
provides the following:
37+
38+
* ``PROBLEM_PARAMS`` : this is a dictionary (it can be empty, ``{}``)
39+
that defines the runtime parameters that are needed to this problem
40+
setup. For instance, for the ``compressible`` ``sod`` problem:
41+
42+
.. code:: python
43+
44+
PROBLEM_PARAMS = {"sod.direction": "x",
45+
"sod.dens_left": 1.0,
46+
"sod.dens_right": 0.125,
47+
"sod.u_left": 0.0,
48+
"sod.u_right": 0.0,
49+
"sod.p_left": 1.0,
50+
"sod.p_right": 0.1}
51+
52+
Each key in the dictionary should be of the form
53+
*problem-name.parameter*, and the values are the default value of
54+
the parameter.
55+
56+
Any of these runtime parameters can be overridden in an inputs file
57+
or on the commandline (when running via ``pyro_sim.py``) or via the
58+
``inputs_dict`` keyword argument (when running via the
59+
:func:`Pyro <pyro.pyro_sim.Pyro>` class).
60+
61+
* ``DEFAULT_INPUTS`` : this is the name of an inputs file to be
62+
read in by default when using the ``Pyro`` class interface. It
63+
can be ``None``.
64+
65+
This is not used when running via ``pyro_sim.py``.
66+
67+
* ``init_data()`` : this is the main initialization routine. It has
68+
the signature:
69+
70+
.. code:: python
71+
72+
def init_data(my_data, rp)
73+
74+
where
75+
76+
* ``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
77+
as needed.
78+
79+
* ``rp`` is a :func:`RuntimeParameters <pyro.util.runparams.RuntimeParameters>` object.
80+
Any of the runtime parameters (including the problem-specific ones
81+
defined via ``PROBLEM_PARAMS``) can be accessed via this.
82+
83+
.. note::
84+
85+
The interface for ``init_data`` is the same for all solvers.
86+
87+
The job of ``init_data`` is to initialize the state data that is
88+
managed by the ``my_data`` object passed in. Exactly which variables
89+
are included there will depend on the solver.
90+
91+
* ``finalize()`` : this is called at the very end of evolution. It
92+
is meant to output instructions to the user on how the can analyze the
93+
data. It takes no arguments.
94+
95+
.. important::
96+
97+
Once the problem is defined, you need to add the problem name to
98+
the ``__all__`` list in the ``__init__.py`` file in the
99+
``problems/`` sub-directory. This lets python know about the
100+
problem.

pyro/advection/problems/_smooth.defaults

Lines changed: 0 additions & 2 deletions
This file was deleted.

pyro/advection/problems/_tophat.defaults

Lines changed: 0 additions & 2 deletions
This file was deleted.

pyro/advection/problems/smooth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
DEFAULT_INPUTS = "inputs.smooth"
99

10+
PROBLEM_PARAMS = {}
11+
1012

1113
def init_data(my_data, rp):
1214
""" initialize the smooth advection problem """

pyro/advection/problems/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
DEFAULT_INPUTS = None
66

7+
PROBLEM_PARAMS = {}
8+
79

810
def init_data(my_data, rp):
911
""" an init routine for unit testing """

pyro/advection/problems/tophat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
DEFAULT_INPUTS = "inputs.tophat"
77

8+
PROBLEM_PARAMS = {}
9+
810

911
def init_data(myd, rp):
1012
""" initialize the tophat advection problem """

pyro/advection_nonuniform/problems/_slotted.defaults

Lines changed: 0 additions & 3 deletions
This file was deleted.

pyro/advection_nonuniform/problems/slotted.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
DEFAULT_INPUTS = "inputs.slotted"
77

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

912
def init_data(my_data, rp):
1013
""" initialize the slotted advection problem """

pyro/advection_nonuniform/problems/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
DEFAULT_INPUTS = None
55

6+
PROBLEM_PARAMS = {}
7+
68

79
def init_data(my_data, rp):
810
""" an init routine for unit testing """

pyro/burgers/problems/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
DEFAULT_INPUTS = "inputs.test"
77

8+
PROBLEM_PARAMS = {}
9+
810

911
def init_data(myd, rp):
1012
""" initialize the burgers test problem """

0 commit comments

Comments
 (0)