Skip to content

Commit 3fa2dbe

Browse files
authored
Merge pull request #162 from DaveCasson/develop
Addition of arguments to ensemble.py and simulation.py for configuration writing. Minor addition file_manager.py
2 parents ccd86aa + b626429 commit 3fa2dbe

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

pysumma/ensemble.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def _generate_coords(self):
9292
decision_dims[k] = v
9393
for k, v in conf.get('file_manager', {}).items():
9494
manager_dims[k] = v
95-
#for k, v in conf.get('parameters', {}).items():
96-
# parameter_dims[k] = v
95+
for k, v in conf.get('parameters', {}).items():
96+
parameter_dims[k] = v
9797
for k, v in conf.get('trial_parameters', {}).items():
9898
parameter_dims[k] = v
9999
return {'decisions': decision_dims,
@@ -133,13 +133,15 @@ def merge_output(self):
133133

134134
def open_output(self):
135135
"""
136-
Open all of the output datasets from the ensembe and
136+
Open all of the output datasets from the ensemble and
137137
return as a dictionary of datasets
138138
"""
139139
return {n: s.output for n, s in self.simulations.items()}
140140

141141

142-
def start(self, run_option: str='local', prerun_cmds: list=None):
142+
def start(self, run_option: str='local', run_suffix: str='',
143+
prerun_cmds: list=None, freq_restart: str=None,
144+
write_config: bool=True,write_manager: bool=False):
143145
"""
144146
Start running the ensemble members.
145147
@@ -154,9 +156,13 @@ def start(self, run_option: str='local', prerun_cmds: list=None):
154156
# Sleep calls are to ensure writeout happens
155157
config = self.configuration[n]
156158
self.submissions.append(self._client.submit(
157-
_submit, s, n, run_option, prerun_cmds, config))
159+
_submit, s, n, run_option,
160+
run_suffix, prerun_cmds, freq_restart,
161+
write_config,write_manager,config))
158162

159-
def run(self, run_option: str='local', prerun_cmds=None, monitor: bool=True):
163+
def run(self, run_option: str='local', run_suffix: str='',
164+
prerun_cmds: list=None, freq_restart: str=None, write_config: bool=True,
165+
write_manager: bool=False, monitor: bool=True):
160166
"""
161167
Run the ensemble
162168
@@ -169,7 +175,8 @@ def run(self, run_option: str='local', prerun_cmds=None, monitor: bool=True):
169175
monitor:
170176
Whether to halt operation until runs are complete
171177
"""
172-
self.start(run_option, prerun_cmds)
178+
self.start(run_option, run_suffix, prerun_cmds,
179+
freq_restart, write_config, write_manager)
173180
if monitor:
174181
return self.monitor()
175182
else:
@@ -191,11 +198,15 @@ def map(self, fun, args, include_sims=True, monitor=True):
191198

192199
def monitor(self):
193200
"""
194-
Halt computation until submitted simulations are complete
201+
Halt computation until submitted simulations are complete.
202+
Update submission name from config.
203+
195204
"""
196205
simulations = self._client.gather(self.submissions)
197-
for s in simulations:
198-
self.simulations[s.run_suffix] = s
206+
names = self.simulations.keys()
207+
208+
for s,name in zip(simulations,names):
209+
self.simulations[name] = s
199210

200211
def summary(self):
201212
"""
@@ -211,8 +222,9 @@ def summary(self):
211222
other.append(n)
212223
return {'Success': success, 'Error': error, 'Other': other}
213224

214-
def rerun_failed(self, run_option: str='local', prerun_cmds=None,
215-
monitor: bool=True):
225+
def rerun_failed(self, run_option: str='local', run_suffix: str='',
226+
prerun_cmds: list=None, freq_restart: str=None, write_config: bool=True,
227+
write_manager: bool=False, monitor: bool=True):
216228
"""
217229
Try to re-run failed simulations.
218230
@@ -232,18 +244,27 @@ def rerun_failed(self, run_option: str='local', prerun_cmds=None,
232244
s = self.simulations[n]
233245
s.reset()
234246
self.submissions.append(self._client.submit(
235-
_submit, s, n, run_option, prerun_cmds, config))
247+
_submit, s, n, run_option, run_suffix,
248+
prerun_cmds, freq_restart,write_config,
249+
write_manager, config))
250+
236251
if monitor:
237252
return self.monitor()
238253
else:
239254
return True
240255

256+
def _submit(s: Simulation, name: str, run_option: str, run_suffix: str,
257+
prerun_cmds: list, freq_restart: str,write_config: bool,
258+
write_manager: bool,config: Dict, **kwargs):
259+
"""
260+
Run simulation objects
261+
"""
241262

242-
def _submit(s: Simulation, name: str, run_option: str, prerun_cmds: List,
243-
config: Dict, **kwargs):
244263
s.initialize()
245264
s.apply_config(config)
246-
s.run(run_option, run_suffix=name, prerun_cmds=prerun_cmds, freq_restart='e')
265+
266+
s.run(run_option, run_suffix=run_suffix, freq_restart=freq_restart,
267+
write_config=write_config,write_manager=write_manager)
247268
s.process = None
248269
return s
249270

pysumma/file_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ def trial_params(self):
108108

109109
@property
110110
def initial_conditions(self):
111-
p1 = self.get_value('settingsPath')
111+
if self.get_value('statePath'):
112+
p1 = self.get_value('statePath')
113+
else:
114+
p1 = self.get_value('settingsPath')
112115
p2 = self.get_value('initConditionFile')
113116
with xr.open_dataset(p1 + p2) as self._init_cond:
114117
self._init_cond.load()

pysumma/simulation.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def apply_config(self, config: dict):
114114
"""
115115
if 'file_manager' in config:
116116
self.manager_path = Path(os.path.abspath(config['file_manager']))
117+
for k, v in config.get('manager', {}).items():
118+
self.manager.set_option(k, v)
117119
for k, v in config.get('decisions', {}).items():
118120
self.decisions.set_option(k, v)
119121
for k, v in config.get('parameters', {}).items():
@@ -302,7 +304,8 @@ def _run_docker(self, run_suffix, processes=1,
302304

303305
def start(self, run_option='local', run_suffix='pysumma_run', processes=1,
304306
prerun_cmds=[], startGRU=None, countGRU=None, iHRU=None,
305-
freq_restart=None, progress=None, **kwargs):
307+
freq_restart=None, write_config=True,write_manager=False,
308+
progress=None, **kwargs):
306309
"""
307310
Run a SUMMA simulation without halting. Most likely this should
308311
not be used. Use the ``run`` method for most common use cases.
@@ -311,7 +314,10 @@ def start(self, run_option='local', run_suffix='pysumma_run', processes=1,
311314
if not prerun_cmds:
312315
prerun_cmds = []
313316
self.run_suffix = run_suffix
314-
self._write_configuration(name=run_suffix)
317+
if write_config:
318+
self._write_configuration(name=run_suffix)
319+
if write_manager:
320+
self._write_file_manager()
315321
if run_option == 'local':
316322
self._run_local(run_suffix, processes, prerun_cmds,
317323
startGRU, countGRU, iHRU, freq_restart, progress)
@@ -324,7 +330,8 @@ def start(self, run_option='local', run_suffix='pysumma_run', processes=1,
324330

325331
def run(self, run_option='local', run_suffix='pysumma_run', processes=1,
326332
prerun_cmds=None, startGRU=None, countGRU=None, iHRU=None,
327-
freq_restart=None, progress=None, **kwargs):
333+
freq_restart=None, write_config=True,write_manager=False,
334+
progress=None, **kwargs):
328335
"""
329336
Run a SUMMA simulation and halt until completion or error.
330337
@@ -360,7 +367,8 @@ def run(self, run_option='local', run_suffix='pysumma_run', processes=1,
360367
hourly output.
361368
"""
362369
self.start(run_option, run_suffix, processes, prerun_cmds,
363-
startGRU, countGRU, iHRU, freq_restart, progress, **kwargs)
370+
startGRU, countGRU, iHRU, freq_restart,write_config,
371+
write_manager, progress, **kwargs)
364372
self.monitor()
365373

366374
def monitor(self):
@@ -452,6 +460,9 @@ def _write_configuration(self, name=''):
452460
with open(settings_path / self.manager['vegTableFile'].value, 'w+') as f:
453461
f.writelines(self.vegparm)
454462

463+
def _write_file_manager(self, name=''):
464+
self.manager.write(path=self.config_path.parent)
465+
455466
def get_forcing_data_list(self) -> List[xr.Dataset]:
456467
return self.force_file_list.open_forcing_data()
457468

0 commit comments

Comments
 (0)