From 1fdc2b5bfe00d79b962a0d95554b0f3a0d577aaa Mon Sep 17 00:00:00 2001 From: Connor Jordan Date: Fri, 7 Jun 2024 14:08:39 +0100 Subject: [PATCH 1/2] Update channel inversion example - Correct file paths in example for plotting - Correct VTKFile paths for import - Load times for the optimisation fields (one timestep less than existing files) - Import from collections.abc (deprecation update) --- examples/channel_inversion/Makefile | 5 ++++- examples/channel_inversion/inverse_problem.py | 1 + examples/channel_inversion/plot_elevation_progress.py | 7 ++++--- thetis/inversion_tools.py | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/channel_inversion/Makefile b/examples/channel_inversion/Makefile index 7398d197d..950df21d1 100644 --- a/examples/channel_inversion/Makefile +++ b/examples/channel_inversion/Makefile @@ -1,6 +1,7 @@ all: invert plot CONTROLS = Bathymetry Manning InitialElev +STATIONS = stationA stationB stationC stationD stationE invert: for controls in $(CONTROLS); do \ @@ -9,7 +10,9 @@ invert: plot: for controls in $(CONTROLS); do \ - python3 plot_elevation_progress.py -c $$controls + for station in $(STATIONS); do \ + python3 plot_elevation_progress.py -c $$controls -s $$station; \ + done; \ done clean: diff --git a/examples/channel_inversion/inverse_problem.py b/examples/channel_inversion/inverse_problem.py index 8a636d614..d7700c75e 100644 --- a/examples/channel_inversion/inverse_problem.py +++ b/examples/channel_inversion/inverse_problem.py @@ -1,5 +1,6 @@ from thetis import * from firedrake.adjoint import * +from firedrake.output.vtk_output import VTKFile import numpy import thetis.inversion_tools as inversion_tools from model_config import construct_solver diff --git a/examples/channel_inversion/plot_elevation_progress.py b/examples/channel_inversion/plot_elevation_progress.py index 05a0f8bce..a7345e72c 100644 --- a/examples/channel_inversion/plot_elevation_progress.py +++ b/examples/channel_inversion/plot_elevation_progress.py @@ -2,7 +2,7 @@ import matplotlib import matplotlib.pyplot as plt import argparse -from collections import Iterable +from collections.abc import Iterable matplotlib.rc('font', size=10) @@ -26,7 +26,7 @@ ctrl_str = '-'.join(controls) station_str = '-'.join(station_names) -inv_dir = f'outputs_{ctrl_str}-opt' +inv_dir = f'outputs_new_{ctrl_str}-opt' nplots = len(station_names) @@ -45,6 +45,7 @@ g = f'{inv_dir}/diagnostic_timeseries_progress_{sta}_elev.hdf5' with h5py.File(g, 'r') as h5file: + iter_times = h5file['time'][:].flatten() iter_vals = h5file['elev'][:] niter = iter_vals.shape[0] - 1 @@ -52,7 +53,7 @@ ax = next(ax_iter) ax.plot(time, vals, 'k:', zorder=3, label='observation', lw=1.3) for i, v in enumerate(iter_vals): - ax.plot(time, v, label=f'iteration {i}', lw=0.5) + ax.plot(iter_times, v, label=f'iteration {i}', lw=0.5) ax.set_title(f'{sta}', size='small') ax.set_ylabel('Elevation (m)') ax.grid(True) diff --git a/thetis/inversion_tools.py b/thetis/inversion_tools.py index 36d762d0a..9c695ca10 100644 --- a/thetis/inversion_tools.py +++ b/thetis/inversion_tools.py @@ -80,9 +80,9 @@ def initialize(self): create_directory(self.output_dir + '/hdf5') for i in range(len(self.control_coeff_list)): self.outfiles_m.append( - fd.VTKFile(f'{self.output_dir}/control_progress_{i:02d}.pvd')) + fd.output.vtk_output.VTKFile(f'{self.output_dir}/control_progress_{i:02d}.pvd')) self.outfiles_dJdm.append( - fd.VTKFile(f'{self.output_dir}/gradient_progress_{i:02d}.pvd')) + fd.output.vtk_output.VTKFile(f'{self.output_dir}/gradient_progress_{i:02d}.pvd')) self.initialized = True def add_control(self, f): From 576f2a0673be45a1b0d351b03ff9a3b4756115e1 Mon Sep 17 00:00:00 2001 From: Connor Jordan Date: Fri, 7 Jun 2024 16:46:37 +0100 Subject: [PATCH 2/2] Import VTKFile at top level --- examples/channel_inversion/inverse_problem.py | 4 ++-- thetis/inversion_tools.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/channel_inversion/inverse_problem.py b/examples/channel_inversion/inverse_problem.py index d7700c75e..5778b39de 100644 --- a/examples/channel_inversion/inverse_problem.py +++ b/examples/channel_inversion/inverse_problem.py @@ -1,6 +1,6 @@ from thetis import * +import firedrake as fd from firedrake.adjoint import * -from firedrake.output.vtk_output import VTKFile import numpy import thetis.inversion_tools as inversion_tools from model_config import construct_solver @@ -134,4 +134,4 @@ oc.rename(name) print_function_value_range(oc, prefix='Optimal') if not no_exports: - VTKFile(f'{options.output_directory}/{name}_optimised.pvd').write(oc) + fd.VTKFile(f'{options.output_directory}/{name}_optimised.pvd').write(oc) diff --git a/thetis/inversion_tools.py b/thetis/inversion_tools.py index 9c695ca10..36d762d0a 100644 --- a/thetis/inversion_tools.py +++ b/thetis/inversion_tools.py @@ -80,9 +80,9 @@ def initialize(self): create_directory(self.output_dir + '/hdf5') for i in range(len(self.control_coeff_list)): self.outfiles_m.append( - fd.output.vtk_output.VTKFile(f'{self.output_dir}/control_progress_{i:02d}.pvd')) + fd.VTKFile(f'{self.output_dir}/control_progress_{i:02d}.pvd')) self.outfiles_dJdm.append( - fd.output.vtk_output.VTKFile(f'{self.output_dir}/gradient_progress_{i:02d}.pvd')) + fd.VTKFile(f'{self.output_dir}/gradient_progress_{i:02d}.pvd')) self.initialized = True def add_control(self, f):