diff --git a/examples/particles_above_salt/particles_above_salt_bc_3d.py b/examples/particles_above_salt/particles_above_salt_bc_3d.py index 9b678f1a64289948a09230c0d945b5bd6cd55689..09383880d777e6361a45d5c75d50b568cfcae588 100644 --- a/examples/particles_above_salt/particles_above_salt_bc_3d.py +++ b/examples/particles_above_salt/particles_above_salt_bc_3d.py @@ -71,6 +71,7 @@ def compute(args): ## IO paths spectral_path = IO.default_path() + '/spectral' + dump_energy_ioparams=IOParams(filepath=spectral_path, filename='E_{fname}', frequency=args.dump_freq) # Constants l0 = 1.5 #initial thickness of the profile @@ -79,7 +80,7 @@ def compute(args): (Sc, tau, Vp, Rs, Xo, Xn, N) = (0.70, 25, 0.04, 2.0, (-600,0), (600,750), (1537, 512)) elif (dim==3): (Sc, tau, Vp, Rs, Xo, Xn, N) = (7.00, 25, 0.04, 2.0, (-110,0,0), (65,100,100), (3073, 1024, 1024)) - n = 32 + n = 128 N = (3*n+1, n, n) else: raise NotImplementedError @@ -197,10 +198,9 @@ def compute(args): Fin = S, variables = {S: npts}, dt=dt, - plot_input_energy=IOParams(filepath=spectral_path, - filename='E_Sin_{ite}', frequency=args.dump_freq), - plot_output_energy=IOParams(filepath=spectral_path, - filename='E_Sout_{ite}', frequency=args.dump_freq), + dump_energy=dump_energy_ioparams, + plot_inout_energy=IOParams(filepath=spectral_path, + filename='E_S_diffusion_{ite}', frequency=args.dump_freq), **extra_op_kwds) diffuse_C = Diffusion(implementation=impl, enforce_implementation=enforce_implementation, @@ -210,10 +210,9 @@ def compute(args): Fin = C, variables = {C: npts}, dt=dt, - plot_input_energy=IOParams(filepath=spectral_path, - filename='E_Cin_{ite}', frequency=args.dump_freq), - plot_output_energy=IOParams(filepath=spectral_path, - filename='E_Cout_{ite}', frequency=args.dump_freq), + dump_energy=dump_energy_ioparams, + plot_inout_energy=IOParams(filepath=spectral_path, + filename='E_C_diffusion_{ite}', frequency=args.dump_freq), **extra_op_kwds) #> External force rot(-rho*g) = rot(Rs*S + C) @@ -243,9 +242,10 @@ def compute(args): diffusion=nu_W, dt=dt, implementation=impl, enforce_implementation=enforce_implementation, + dump_energy=dump_energy_ioparams, plot_velocity_energy=IOParams(filepath=spectral_path, filename='E_velocity_{ite}', frequency=args.dump_freq), - plot_vorticity_energy=IOParams(filepath=spectral_path, + plot_inout_vorticity_energy=IOParams(filepath=spectral_path, filename='E_vorticity_{ite}', frequency=args.dump_freq), **extra_op_kwds) diff --git a/hysop/operator/base/poisson.py b/hysop/operator/base/poisson.py index b53431f56901ab6db9a984707b40a83d05a21e30..f88d987ff070761e59f4ed4b46e3db6f75fed0d7 100644 --- a/hysop/operator/base/poisson.py +++ b/hysop/operator/base/poisson.py @@ -4,7 +4,7 @@ from hysop.tools.types import check_instance, first_not_None from hysop.tools.decorators import debug from hysop.fields.continuous_field import Field from hysop.topology.cartesian_descriptor import CartesianTopologyDescriptors -from hysop.operator.base.spectral_operator import SpectralOperatorBase, EnergyPlotter +from hysop.operator.base.spectral_operator import SpectralOperatorBase, EnergyPlotter, EnergyDumper from hysop.symbolic.relational import Assignment from hysop.symbolic.field import laplacian @@ -16,7 +16,8 @@ class PoissonOperatorBase(SpectralOperatorBase): @debug def __init__(self, Fin, Fout, variables, name=None, pretty_name=None, - plot_input_energy=None, plot_output_energy=None, plot_inout_energy=None, + dump_energy=None, dump_input_energy=None, dump_output_energy=None, + plot_energy=None, plot_input_energy=None, plot_output_energy=None, plot_inout_energy=None, **kwds): """ Initialize a n-dimensional Poisson operator base (using spectral methods). @@ -32,6 +33,14 @@ class PoissonOperatorBase(SpectralOperatorBase): Output continuous field (lhs), possibly inplace, same number of components as Fin. variables: dict Dictionary of fields as keys and topology descriptors as values. + dump_energy: IOParams, optional, defaults to None + Will set the default io parameter for all energy plotters. + dump_input_energy: IOParams, optional, defaults to None + Dump input field energy to a custom file. Defaults to no dump. + dump_output_energy: IOParams, optional, defaults to None + Dump output field energy to a custom file. Defaults to no dump. + plot_energy: IOParams, optional, defaults to None + Will set the default io parameter for all energy plotters. plot_input_energy: IOParams, optional, defaults to None Plot input field energy in a custom file. Defaults to no plot. plot_output_energy: IOParams, optional, defaults to None @@ -45,9 +54,13 @@ class PoissonOperatorBase(SpectralOperatorBase): check_instance(Fin, Field) check_instance(Fout, Field) check_instance(variables, dict, keys=Field, values=CartesianTopologyDescriptors) - check_instance(plot_input_energy, IOParams, allow_none=True) - check_instance(plot_output_energy, IOParams, allow_none=True) - check_instance(plot_inout_energy, IOParams, allow_none=True) + check_instance(dump_energy, (IOParams,int), allow_none=True) + check_instance(dump_input_energy, (IOParams,int), allow_none=True) + check_instance(dump_output_energy, (IOParams,int), allow_none=True) + check_instance(plot_energy, (IOParams,int), allow_none=True) + check_instance(plot_input_energy, (IOParams,int), allow_none=True) + check_instance(plot_output_energy, (IOParams,int), allow_none=True) + check_instance(plot_inout_energy, (IOParams,int), allow_none=True) assert Fin.nb_components == Fout.nb_components input_fields = { Fin: variables[Fin] } @@ -58,18 +71,15 @@ class PoissonOperatorBase(SpectralOperatorBase): name = first_not_None(name, default_name) pretty_name = first_not_None(name, default_pretty_name) - do_plot_inout_energy = (plot_inout_energy is not None) - compute_input_energy = None - compute_output_energy = None - if do_plot_inout_energy: - if (plot_input_energy is None): - compute_input_energy = plot_inout_energy.frequency - else: - assert compute_input_energy.frequency == plot_input_energy.frequency - if (plot_output_energy is None): - compute_output_energy = plot_inout_energy.frequency - else: - assert compute_output_energy.frequency == plot_output_energy.frequency + dump_input_E = first_not_None(dump_input_energy, dump_energy) + dump_output_E = first_not_None(dump_output_energy, dump_energy) + plot_input_E = first_not_None(plot_input_energy, plot_energy) + plot_output_E = first_not_None(plot_output_energy, plot_energy) + plot_inout_E = first_not_None(plot_inout_energy, plot_energy) + + do_plot_inout_E = isinstance(plot_inout_E, IOParams) and (plot_inout_E.frequency>=0) + _, compute_input_E_freqs = EnergyDumper.do_compute_energy(dump_input_E, plot_input_E, plot_inout_E) + _, compute_output_E_freqs = EnergyDumper.do_compute_energy(dump_output_E, plot_output_E, plot_inout_E) super(PoissonOperatorBase, self).__init__(name=name, pretty_name=pretty_name, input_fields=input_fields, output_fields=output_fields, **kwds) @@ -81,9 +91,11 @@ class PoissonOperatorBase(SpectralOperatorBase): tg = self.new_transform_group() for (Fi,Fo) in zip(Fin.fields, Fout.fields): Ft = tg.require_forward_transform(Fi, custom_output_buffer='auto', - compute_energy=compute_input_energy, plot_energy=plot_input_energy) + dump_energy=dump_input_E, plot_energy=plot_input_E, + compute_energy_frequencies=compute_input_E_freqs) Bt = tg.require_backward_transform(Fo, custom_input_buffer='auto', matching_forward_transform=Ft, - compute_energy=compute_output_energy, plot_energy=plot_output_energy) + dump_energy=dump_output_E, plot_energy=plot_output_E, + compute_energy_frequencies=compute_output_E_freqs) assert (Ft.output_dtype == Bt.input_dtype) expr = Assignment(Bt.s, laplacian(Ft.s, Ft.s.frame)) kds = tg.push_expressions(expr) @@ -99,8 +111,8 @@ class PoissonOperatorBase(SpectralOperatorBase): self.backward_transforms = backward_transforms self.wave_numbers = wave_numbers - self.do_plot_inout_energy = do_plot_inout_energy - self.plot_inout_energy_ioparams = plot_inout_energy + self.do_plot_inout_energy = do_plot_inout_E + self.plot_inout_energy_ioparams = plot_inout_E self.input_energy_params = tuple(Ft._energy_parameter for Ft in self.forward_transforms) self.output_energy_params = tuple(Bt._energy_parameter for Bt in self.backward_transforms) @@ -126,24 +138,16 @@ class PoissonOperatorBase(SpectralOperatorBase): for (dFin, dFout, Pin, Pout) in zip( self.dFin.dfields, self.dFout.dfields, self.input_energy_params, self.output_energy_params): + fname = '{}_{}'.format(dFin.name, dFout.name) iname = u'{}.input.{}'.format(type(self).__name__, dFin.pretty_name.decode('utf-8')) oname = u'{}.output.{}'.format(type(self).__name__, dFout.pretty_name.decode('utf-8')) - plt = EnergyPlotter(energy_parameters={iname: Pin, oname: Pout}, save_txt=False) + plt = EnergyPlotter(energy_parameters={iname: Pin, oname: Pout}, fname=fname, + io_params=self.plot_inout_energy_ioparams) inout_energy_plotters += (plt,) self.inout_energy_plotters = inout_energy_plotters def plot(self, simulation): if not self.do_plot_inout_energy: return - iop = self.plot_inout_energy_ioparams - if simulation.should_dump(frequency=iop.frequency, with_last=True): - for (plt, Fin, Fout) in zip(self.inout_energy_plotters, self.Fin.fields, self.Fout.fields): - filename = iop.filename - filename = filename.replace('{fname}', '{}_{}'.format(Fin.name, Fout.name) - if (Fin.name!=Fout.name) else Fin.name) - filename = filename.replace('{finname}', Fin.name) - filename = filename.replace('{foutname}', Fout.name) - filename = filename.replace('{ite}', '{:05}'.format(simulation.current_iteration)) - plt.update(iteration=simulation.current_iteration, time=simulation.t()) - plt.save(filename) - + for plt in self.inout_energy_plotters: + plt.update(simulation=simulation, wait_for=None) diff --git a/hysop/tools/spectral_utils.py b/hysop/tools/spectral_utils.py index 3f06e1b0e86fc1cad56d805e7f1104982a2f33eb..46bfb0e3a5baaadc5f222394a8e4891b214f379e 100644 --- a/hysop/tools/spectral_utils.py +++ b/hysop/tools/spectral_utils.py @@ -718,9 +718,9 @@ class EnergyDumper(object): check_instance(io_params.io_leader, int) filename = io_params.filename filename = filename.replace('{fname}', fname) - filename = filename.replace('{it}', '') + filename = filename.replace('{ite}', '') assert '{fname}' not in filename - assert '{it}' not in filename + assert '{ite}' not in filename assert io_params.frequency>=0 check_instance(energy_parameter, BufferParameter) diff --git a/test_examples.sh b/test_examples.sh index 28144bfb13eda18b2ee30532abae2e33651b6185..acf7047f33a116bf167fa8780bb6fa4ccc69081a 100755 --- a/test_examples.sh +++ b/test_examples.sh @@ -11,6 +11,7 @@ export HYSOP_VERBOSE=1 export HYSOP_DEBUG=0 export HYSOP_PROFILE=0 export HYSOP_KERNEL_DEBUG=0 +export MPLBACKEND='cairo' python -c 'import hysop; print hysop' EXAMPLE_DIR="$HYSOP_ROOT/examples"