Skip to content

Commit 3a27486

Browse files
authored
Merge pull request #1615 from knutfrode/dev
[run-ex] Further cleaning in main loop
2 parents 074da9a + bda018d commit 3a27486

File tree

2 files changed

+38
-58
lines changed

2 files changed

+38
-58
lines changed

opendrift/models/basemodel/__init__.py

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,29 +2103,22 @@ def run(self,
21032103
logger.debug('%s elements scheduled.' %
21042104
self.num_elements_scheduled())
21052105
logger.debug('===================================' * 2)
2106-
if len(self.elements.lon) > 0:
2107-
lonmin = self.elements.lon.min()
2108-
lonmax = self.elements.lon.max()
2109-
latmin = self.elements.lat.min()
2110-
latmax = self.elements.lat.max()
2111-
zmin = self.elements.z.min()
2112-
zmax = self.elements.z.max()
2113-
if latmin == latmax:
2114-
logger.debug('\t\tlatitude = %s' % (latmin))
2106+
2107+
# Display element locations to terminal
2108+
for n in ['lat', 'lon', 'z']:
2109+
d = getattr(self.elements, n)
2110+
dmin = d.min();
2111+
dmax = d.max()
2112+
n = n.replace('lat', 'latitude').replace('lon', 'longitude')
2113+
if dmin == dmax:
2114+
logger.debug(f'\t\t{n} = {dmin}')
21152115
else:
2116-
logger.debug('\t\t%s <- latitude -> %s' %
2117-
(latmin, latmax))
2118-
if lonmin == lonmax:
2119-
logger.debug('\t\tlongitude = %s' % (lonmin))
2120-
else:
2121-
logger.debug('\t\t%s <- longitude -> %s' %
2122-
(lonmin, lonmax))
2123-
if zmin == zmax:
2124-
logger.debug('\t\tz = %s' % (zmin))
2125-
else:
2126-
logger.debug('\t\t%s <- z -> %s' % (zmin, zmax))
2127-
logger.debug('---------------------------------')
2116+
logger.debug(f'\t\t{dmin} <- {n} -> {dmax}')
2117+
logger.debug('---------------------------------')
21282118

2119+
###############################################
2120+
# Get environment data for all active elements
2121+
###############################################
21292122
self.environment, self.environment_profiles, missing = \
21302123
self.env.get_environment(list(self.required_variables),
21312124
self.time,
@@ -2137,57 +2130,40 @@ def run(self,
21372130

21382131
self.calculate_missing_environment_variables()
21392132

2140-
if any(missing):
2141-
self.report_missing_variables()
2142-
2143-
self.store_previous_variables()
2133+
self.report_missing_variables(missing)
21442134

21452135
self.interact_with_coastline()
21462136

21472137
self.interact_with_seafloor()
21482138

2149-
self.deactivate_elements(missing, reason='missing_data')
2150-
21512139
self.state_to_buffer() # Append status to history array
2152-
2140+
21532141
self.increase_age_and_retire()
21542142

21552143
self.remove_deactivated_elements()
21562144

2157-
# Propagate one timestep forwards
2158-
self.steps_calculation += 1
2159-
2160-
if self.num_elements_active(
2161-
) == 0 and self.num_elements_scheduled() == 0:
2162-
raise ValueError(
2163-
'No more active or scheduled elements, quitting.')
2164-
21652145
# Store location, in case elements shall be moved back
21662146
self.store_present_positions()
2167-
2168-
#####################################################
2147+
21692148
if self.num_elements_active() > 0:
2149+
########################################
2150+
# Calling module specific update method
2151+
########################################
21702152
logger.debug('Calling %s.update()' % type(self).__name__)
21712153
self.timer_start('main loop:updating elements')
21722154
self.update()
21732155
self.timer_end('main loop:updating elements')
21742156
else:
2175-
logger.info('No active elements, skipping update() method')
2176-
#####################################################
2157+
if self.num_elements_scheduled() == 0:
2158+
raise ValueError('No more active or scheduled elements, quitting.')
2159+
else:
2160+
logger.info('No active elements, skipping update() method')
21772161

21782162
self.horizontal_diffusion()
21792163

2180-
if self.num_elements_active(
2181-
) == 0 and self.num_elements_scheduled() == 0:
2182-
raise ValueError(
2183-
'No active or scheduled elements, quitting simulation')
2184-
2185-
logger.debug('%s active elements (%s deactivated)' %
2186-
(self.num_elements_active(),
2187-
self.num_elements_deactivated()))
21882164
# Updating time
2189-
if self.time is not None:
2190-
self.time = self.time + self.time_step
2165+
self.time = self.time + self.time_step
2166+
self.steps_calculation += 1
21912167

21922168
except Exception as e:
21932169
message = ('The simulation stopped before requested '
@@ -2218,7 +2194,7 @@ def run(self,
22182194
self.timer_end('total time')
22192195
self.state_to_buffer(final=True) # Append final status to buffer
22202196

2221-
## Add any other data to the result here.
2197+
# Module specific post processing.
22222198
self.post_run()
22232199

22242200
if outfile is not None:
@@ -2390,18 +2366,22 @@ def state_to_buffer(self, final=False):
23902366
self.result.coords['time'] = newtime
23912367
logger.debug(f'Reset self.result, size {self.result.sizes}')
23922368

2393-
def report_missing_variables(self):
2394-
"""Issue warning if some environment variables missing."""
2369+
def report_missing_variables(self, missing):
2370+
"""Deactivate elements whose environment variables are missing."""
2371+
2372+
if not any(missing):
2373+
return
23952374

2396-
missing_variables = []
2397-
for var in self.required_variables:
2398-
if np.isnan(getattr(self.environment, var).min()):
2399-
missing_variables.append(var)
2375+
missing_variables = [v for v in self.required_variables
2376+
if np.any(np.isnan(getattr(self.environment, v)))]
24002377

24012378
if len(missing_variables) > 0:
24022379
logger.warning('Missing variables: ' + str(missing_variables))
24032380
self.store_message('Missing variables: ' + str(missing_variables))
24042381

2382+
# TODO: missing should probably be updated after calculating derived variables
2383+
self.deactivate_elements(missing, reason='missing_data')
2384+
24052385
def index_of_first_and_last(self):
24062386
"""Return the indices when elements were seeded and deactivated."""
24072387

tests/models/test_basemodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def test_logging(tmpdir, capsys):
1111
# Accepting small variations in log output,
1212
# depending on machine, and from which folder test is run
13-
accepted = (288, 291, 316)
13+
accepted = (285, 288, 313)
1414

1515
# Logging to console
1616
logfile = None

0 commit comments

Comments
 (0)