Skip to content

Commit 23cb4f4

Browse files
authored
get rid of command_line_params (#257)
we don't need a separate RuntimeParameters method to parse a string, instead we can just do this in pyro_sim.py and then use the existing set_val method from a dict. This also allows us to simplify the confusing Pyro() interface.
1 parent 9c65825 commit 23cb4f4

File tree

6 files changed

+184
-120
lines changed

6 files changed

+184
-120
lines changed

presentations/pyro_intro.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@
354354
"from pyro import Pyro\n",
355355
"pyro_sim = Pyro(\"advection\")\n",
356356
"pyro_sim.initialize_problem(\"tophat\",\n",
357-
" other_commands=[\"mesh.nx=8\", \"mesh.ny=8\",\n",
358-
" \"vis.dovis=0\"])\n",
357+
" inputs_dict={\"mesh.nx\": 8,\n",
358+
" \"mesh.ny\": 8})\n",
359359
"pyro_sim.run_sim()"
360360
]
361361
},

pyro/pyro_sim.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import pyro.util.io_pyro as io
1111
import pyro.util.profile_pyro as profile
12-
from pyro.util import compare, msg, runparams
12+
from pyro.util import compare, msg
13+
from pyro.util.runparams import RuntimeParameters, _get_val
1314

1415
valid_solvers = ["advection",
1516
"advection_nonuniform",
@@ -71,16 +72,15 @@ def __init__(self, solver_name, *, from_commandline=False):
7172
# -------------------------------------------------------------------------
7273

7374
# parameter defaults
74-
self.rp = runparams.RuntimeParameters()
75+
self.rp = RuntimeParameters()
7576
self.rp.load_params(self.pyro_home + "_defaults")
7677
self.rp.load_params(self.pyro_home + self.solver_name + "/_defaults")
7778

7879
self.tc = profile.TimerCollection()
7980

8081
self.is_initialized = False
8182

82-
def initialize_problem(self, problem_name, *, inputs_file=None, inputs_dict=None,
83-
other_commands=None):
83+
def initialize_problem(self, problem_name, *, inputs_file=None, inputs_dict=None):
8484
"""
8585
Initialize the specific problem
8686
@@ -92,8 +92,6 @@ def initialize_problem(self, problem_name, *, inputs_file=None, inputs_dict=None
9292
Filename containing problem's runtime parameters
9393
inputs_dict : dict
9494
Dictionary containing extra runtime parameters
95-
other_commands : str
96-
Other command line parameter options
9795
"""
9896
# pylint: disable=attribute-defined-outside-init
9997

@@ -126,10 +124,6 @@ def initialize_problem(self, problem_name, *, inputs_file=None, inputs_dict=None
126124
for k, v in inputs_dict.items():
127125
self.rp.set_param(k, v)
128126

129-
# and any commandline overrides
130-
if other_commands is not None:
131-
self.rp.command_line_params(other_commands)
132-
133127
# write out the inputs.auto
134128
self.rp.print_paramfile()
135129

@@ -399,9 +393,15 @@ def main():
399393
else:
400394
pyro = Pyro(args.solver[0], from_commandline=True)
401395

396+
other = {}
397+
for param_string in args.other:
398+
k, v = param_string.split("=")
399+
other[k] = _get_val(v)
400+
401+
print(other)
402402
pyro.initialize_problem(problem_name=args.problem[0],
403403
inputs_file=args.param[0],
404-
other_commands=args.other)
404+
inputs_dict=other)
405405
pyro.run_sim()
406406

407407

pyro/solver-test.ipynb

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
"solver = \"advection\"\n",
3535
"problem_name = \"smooth\"\n",
3636
"param_file = \"inputs.smooth\"\n",
37-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\"]"
37+
"params = {\"driver.max_steps\":1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
3838
]
3939
},
4040
{
4141
"cell_type": "code",
42-
"execution_count": 3,
42+
"execution_count": 4,
4343
"metadata": {
4444
"tags": [
4545
"nbval-ignore-output"
@@ -68,14 +68,14 @@
6868
],
6969
"source": [
7070
"pyro_sim = Pyro(solver)\n",
71-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
71+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
7272
"pyro_sim.run_sim()\n",
7373
"pyro_sim.sim.dovis()"
7474
]
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 4,
78+
"execution_count": 5,
7979
"metadata": {
8080
"scrolled": true
8181
},
@@ -121,7 +121,7 @@
121121
"solver = \"advection_nonuniform\"\n",
122122
"problem_name = \"slotted\"\n",
123123
"param_file = \"inputs.slotted\"\n",
124-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\"]"
124+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
125125
]
126126
},
127127
{
@@ -162,7 +162,7 @@
162162
],
163163
"source": [
164164
"pyro_sim = Pyro(solver)\n",
165-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
165+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
166166
"pyro_sim.run_sim()\n",
167167
"pyro_sim.sim.dovis()"
168168
]
@@ -213,7 +213,7 @@
213213
"solver = \"advection_fv4\"\n",
214214
"problem_name = \"smooth\"\n",
215215
"param_file = \"inputs.smooth\"\n",
216-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\"]"
216+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
217217
]
218218
},
219219
{
@@ -247,7 +247,7 @@
247247
],
248248
"source": [
249249
"pyro_sim = Pyro(solver)\n",
250-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
250+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
251251
"pyro_sim.run_sim()\n",
252252
"pyro_sim.sim.dovis()"
253253
]
@@ -298,7 +298,7 @@
298298
"solver = \"advection_rk\"\n",
299299
"problem_name = \"tophat\"\n",
300300
"param_file = \"inputs.tophat\"\n",
301-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\"]"
301+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
302302
]
303303
},
304304
{
@@ -332,7 +332,7 @@
332332
],
333333
"source": [
334334
"pyro_sim = Pyro(solver)\n",
335-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
335+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
336336
"pyro_sim.run_sim()\n",
337337
"pyro_sim.sim.dovis()"
338338
]
@@ -376,19 +376,19 @@
376376
},
377377
{
378378
"cell_type": "code",
379-
"execution_count": 14,
379+
"execution_count": 16,
380380
"metadata": {},
381381
"outputs": [],
382382
"source": [
383383
"solver = \"compressible\"\n",
384384
"problem_name = \"rt\"\n",
385385
"param_file = \"inputs.rt\"\n",
386-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=24\", \"driver.verbose=0\", \"compressible.riemann=CGF\"]"
386+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 24, \"compressible.riemann\": \"CGF\"}"
387387
]
388388
},
389389
{
390390
"cell_type": "code",
391-
"execution_count": 15,
391+
"execution_count": 17,
392392
"metadata": {
393393
"tags": [
394394
"nbval-ignore-output"
@@ -425,14 +425,14 @@
425425
],
426426
"source": [
427427
"pyro_sim = Pyro(solver)\n",
428-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
428+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
429429
"pyro_sim.run_sim()\n",
430430
"pyro_sim.sim.dovis()"
431431
]
432432
},
433433
{
434434
"cell_type": "code",
435-
"execution_count": 16,
435+
"execution_count": 18,
436436
"metadata": {},
437437
"outputs": [
438438
{
@@ -485,19 +485,19 @@
485485
},
486486
{
487487
"cell_type": "code",
488-
"execution_count": 17,
488+
"execution_count": 20,
489489
"metadata": {},
490490
"outputs": [],
491491
"source": [
492492
"solver = \"compressible_fv4\"\n",
493493
"problem_name = \"kh\"\n",
494494
"param_file = \"inputs.kh\"\n",
495-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\", \"driver.verbose=0\"]"
495+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
496496
]
497497
},
498498
{
499499
"cell_type": "code",
500-
"execution_count": 18,
500+
"execution_count": 21,
501501
"metadata": {
502502
"tags": [
503503
"nbval-ignore-output"
@@ -526,14 +526,14 @@
526526
],
527527
"source": [
528528
"pyro_sim = Pyro(solver)\n",
529-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
529+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
530530
"pyro_sim.run_sim()\n",
531531
"pyro_sim.sim.dovis()"
532532
]
533533
},
534534
{
535535
"cell_type": "code",
536-
"execution_count": 19,
536+
"execution_count": 22,
537537
"metadata": {},
538538
"outputs": [
539539
{
@@ -570,14 +570,14 @@
570570
},
571571
{
572572
"cell_type": "code",
573-
"execution_count": 20,
573+
"execution_count": 23,
574574
"metadata": {},
575575
"outputs": [],
576576
"source": [
577577
"solver = \"compressible_rk\"\n",
578578
"problem_name = \"quad\"\n",
579579
"param_file = \"inputs.quad\"\n",
580-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=16\", \"mesh.ny=16\", \"driver.verbose=0\"]"
580+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 16, \"mesh.ny\": 16}"
581581
]
582582
},
583583
{
@@ -611,7 +611,7 @@
611611
],
612612
"source": [
613613
"pyro_sim = Pyro(solver)\n",
614-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
614+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
615615
"pyro_sim.run_sim()\n",
616616
"pyro_sim.sim.dovis()"
617617
]
@@ -670,7 +670,7 @@
670670
"solver = \"compressible_sdc\"\n",
671671
"problem_name = \"sod\"\n",
672672
"param_file = \"inputs.sod.y\"\n",
673-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=4\", \"mesh.ny=16\", \"driver.verbose=0\"]"
673+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 4, \"mesh.ny\": 16}"
674674
]
675675
},
676676
{
@@ -715,7 +715,7 @@
715715
],
716716
"source": [
717717
"pyro_sim = Pyro(solver)\n",
718-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
718+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
719719
"pyro_sim.run_sim()\n",
720720
"pyro_sim.sim.dovis()"
721721
]
@@ -774,7 +774,7 @@
774774
"solver = \"diffusion\"\n",
775775
"problem_name = \"gaussian\"\n",
776776
"param_file = \"inputs.gaussian\"\n",
777-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=16\", \"mesh.ny=16\", \"driver.verbose=0\"]"
777+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 16, \"mesh.ny\": 16}"
778778
]
779779
},
780780
{
@@ -818,7 +818,7 @@
818818
],
819819
"source": [
820820
"pyro_sim = Pyro(solver)\n",
821-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
821+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
822822
"pyro_sim.run_sim()\n",
823823
"pyro_sim.sim.dovis()"
824824
]
@@ -877,7 +877,7 @@
877877
"solver = \"incompressible\"\n",
878878
"problem_name = \"shear\"\n",
879879
"param_file = \"inputs.shear\"\n",
880-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=8\", \"mesh.ny=8\", \"driver.verbose=0\"]"
880+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 8, \"mesh.ny\": 8}"
881881
]
882882
},
883883
{
@@ -921,7 +921,7 @@
921921
],
922922
"source": [
923923
"pyro_sim = Pyro(solver)\n",
924-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
924+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
925925
"pyro_sim.run_sim()\n",
926926
"pyro_sim.sim.dovis()"
927927
]
@@ -972,7 +972,7 @@
972972
"solver = \"lm_atm\"\n",
973973
"problem_name = \"bubble\"\n",
974974
"param_file = \"inputs.bubble\"\n",
975-
"other_commands = [\"driver.max_steps=1\", \"mesh.nx=16\", \"mesh.ny=16\", \"driver.verbose=0\"]"
975+
"params = {\"driver.max_steps\": 1, \"mesh.nx\": 16, \"mesh.ny\": 16}"
976976
]
977977
},
978978
{
@@ -1006,7 +1006,7 @@
10061006
],
10071007
"source": [
10081008
"pyro_sim = Pyro(solver)\n",
1009-
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, other_commands=other_commands)\n",
1009+
"pyro_sim.initialize_problem(problem_name, inputs_file=param_file, inputs_dict=params)\n",
10101010
"pyro_sim.run_sim()\n",
10111011
"pyro_sim.sim.dovis()"
10121012
]

pyro/solver_test_swe.ipynb

Lines changed: 136 additions & 43 deletions
Large diffs are not rendered by default.

pyro/util/runparams.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,36 +163,6 @@ def load_params(self, pfile, *, no_new=False):
163163

164164
self.param_comments[key] = comment.strip()
165165

166-
def command_line_params(self, cmd_strings):
167-
"""
168-
finds dictionary pairs from a string that came from the
169-
commandline. Stores the parameters in only if they
170-
already exist.
171-
172-
we expect things in the string in the form:
173-
["sec.opt=value", "sec.opt=value"]
174-
with each opt an element in the list
175-
176-
Parameters
177-
----------
178-
cmd_strings : list
179-
The list of strings containing runtime parameter pairs
180-
181-
"""
182-
183-
for item in cmd_strings:
184-
185-
# break it apart
186-
key, value = item.split("=")
187-
188-
# we only want to override existing keys/values
189-
if key not in self.params:
190-
msg.warning("warning, key: %s not defined" % (key))
191-
continue
192-
193-
# check in turn whether this is an integer, float, or string
194-
self.params[key] = _get_val(value)
195-
196166
def get_param(self, key):
197167
"""
198168
returns the value of the runtime parameter corresponding to the

pyro/util/tests/test_runparams.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ def test_get_param(self):
5050
assert self.rp.get_param("test3.param") == "this is a test"
5151
assert self.rp.get_param("test3.i1") == 1
5252

53-
def test_command_line_params(self):
53+
def test_dict(self):
5454

55-
param_string = "test.p1=q test3.i1=2"
55+
params = {"test.p1": "q", "test3.i1": 2}
5656

57-
self.rp.command_line_params(param_string.split())
57+
for k, v in params.items():
58+
self.rp.set_param(k, v, no_new=False)
5859

5960
assert self.rp.get_param("test.p1") == "q"
6061
assert self.rp.get_param("test3.i1") == 2

0 commit comments

Comments
 (0)