Skip to content

Commit

Permalink
Merge pull request #1212 from knutfrode/dev
Browse files Browse the repository at this point in the history
[run-ex] OpenOil.plot_oil_budget() show shows watercontent and viscos…
  • Loading branch information
knutfrode authored Jan 10, 2024
2 parents 516c0b3 + 5a19754 commit f486f2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/example_oil_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
# Running model
o.run(duration=timedelta(hours=24), time_step=1800)

o.plot_oil_budget(show_density_viscosity=True, show_wind_and_current=True)
o.plot_oil_budget(show_watercontent_and_viscosity=True, show_wind_and_current=True)

o.animation(color='viscosity')

Expand Down
38 changes: 20 additions & 18 deletions opendrift/models/openoil/openoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ def get_oil_budget(self):
def plot_oil_budget(self,
filename=None,
ax=None,
show_density_viscosity=True,
show_watercontent_and_viscosity=True,
show_wind_and_current=True):

if self.time_step.days < 0: # Backwards simulation
Expand Down Expand Up @@ -1305,7 +1305,7 @@ def plot_oil_budget(self,
if ax is None:
# Left axis showing oil mass
nrows = 1
if show_density_viscosity is True:
if show_watercontent_and_viscosity is True:
nrows = nrows + 1
if show_wind_and_current is True:
nrows = nrows + 1
Expand All @@ -1317,8 +1317,8 @@ def plot_oil_budget(self,
ax1 = axs
elif nrows >= 2:
ax1 = axs[0]
if show_density_viscosity is True:
self.plot_oil_density_and_viscosity(ax=axs[1], show=False)
if show_watercontent_and_viscosity is True:
self.plot_oil_watercontent_and_viscosity(ax=axs[1], show=False)
if show_wind_and_current is True:
self.plot_environment(ax=axs[nrows - 1], show=False)
else:
Expand Down Expand Up @@ -1397,12 +1397,12 @@ def plot_oil_budget(self,
self.start_time.strftime('%Y-%m-%d %H:%M'),
self.time.strftime('%Y-%m-%d %H:%M')))
# Shrink current axis's height by 10% on the bottom
box = ax1.get_position()
ax1.set_position(
[box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])
ax2.set_position(
[box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])
ax1.legend(bbox_to_anchor=(0., -0.10, 1., -0.03),
#box = ax1.get_position()
#ax1.set_position(
# [box.x0, box.y0 + box.height * 0.15, box.width, box.height * 0.85])
#ax2.set_position(
# [box.x0, box.y0 + box.height * 0.5, box.width, box.height * 0.6])
ax1.legend(bbox_to_anchor=(0., -0.1, 1., -0.04),
loc=1,
ncol=6,
mode="expand",
Expand Down Expand Up @@ -1434,7 +1434,7 @@ def cumulative_oil_entrainment_fraction(self):
cumulative_fraction_entrained = np.sum(z, 1) / z.shape[1]
return cumulative_fraction_entrained

def plot_oil_density_and_viscosity(self, ax=None, show=True):
def plot_oil_watercontent_and_viscosity(self, ax=None, show=True):
if ax is None:
fig, ax = plt.subplots()
import matplotlib.dates as mdates
Expand All @@ -1447,6 +1447,8 @@ def plot_oil_density_and_viscosity(self, ax=None, show=True):
dyn_viscosity_std = dyn_viscosity.std(axis=0)
density = self.history['density'].mean(axis=0)
density_std = self.history['density'].std(axis=0)
watercontent = self.history['water_fraction'].mean(axis=0)*100
watercontent_std = self.history['water_fraction'].std(axis=0)*100

ax.plot(time,
dyn_viscosity_mean,
Expand All @@ -1463,19 +1465,19 @@ def plot_oil_density_and_viscosity(self, ax=None, show=True):
ax.tick_params(axis='y', colors='g')

axb = ax.twinx()
axb.plot(time, density, 'b', lw=2, label='Density')
axb.plot(time, watercontent, 'b', lw=2, label='Water content')
axb.fill_between(time,
density - density_std,
density + density_std,
color='b',
alpha=0.5)
watercontent - watercontent_std,
watercontent + watercontent_std,
color='b', alpha=0.5)
ax.set_xlim([0, time.max()])
ax.set_xlabel('Time [hours]')
axb.set_ylabel(r'Density [kg/m3]', color='b')
axb.set_ylim([0, 100])
axb.set_ylabel(r'Water content [%]', color='b')
axb.tick_params(axis='y', colors='b')

ax.legend(loc='upper left')
axb.legend(loc='lower right')
axb.legend(loc='upper center')
if show is True:
plt.show()

Expand Down

0 comments on commit f486f2d

Please sign in to comment.