Docs › Worked physics and service recipes › Shared cell for one-dimensional profiles
6.2 Shared cell for one-dimensional profiles
Use this cell for the periodic and explicitly bounded line experiments. For nonperiodic examples pass the physical boundary tuple. The display helper expects one real scalar per native coordinate. Complex and vector recipes select a physical component or magnitude explicitly.
import numpy as np
import matplotlib.pyplot as plt
import onesimulation_plot_theme as theme
from onesim.cad.d1 import Line
from onesim.meshing import PeriodicPair, StructuredLineMesher
from onesim.numerics import Numerics
from onesim.numerics.fdm import FiniteDifference, CentralDifference, CentralLaplacian
from onesim.numerics.fvm import FiniteVolume, Rusanov, MUSCL, TwoPointDiffusion
from onesim.numerics.linear import SparseDirect
from onesim.numerics.nonlinear import Newton
from onesim.numerics.time import ClassicalRungeKutta4, BackwardEuler
from onesim.simulation import Problem, Simulation
from onesim.simulation.initial_conditions import InitialValue
from onesim.simulation.problem_definition.boundaries import PeriodicBoundary
def line_experiment(
line,
model,
initial,
spatial,
*,
cells,
end,
dt,
conditions=None,
implicit=False,
linear_solver=None
):
if conditions is None:
pair = PeriodicPair(line.boundaries.start, line.boundaries.end)
conditions = (PeriodicBoundary(physics=model, pair=pair),)
problem = Problem(
name=type(model).__name__,
geometry=line,
physics=(model,),
initial_conditions=tuple(
InitialValue(model.field(name), value) for name, value in initial.items()
),
boundary_conditions=conditions,
)
numerics = Numerics(
spatial=spatial,
time_integrator=BackwardEuler() if implicit else ClassicalRungeKutta4(),
linear_solver=linear_solver,
nonlinear_solver=(
Newton(
linear_solver=SparseDirect(),
jacobian="colored_finite_difference",
absolute_tolerance=1e-12,
relative_tolerance=1e-10,
)
if implicit
else None
),
)
sim = Simulation(
problem=problem,
mesh=StructuredLineMesher(cells=cells).generate(line),
numerics=numerics,
)
sim.validate().require_valid()
return sim.run(start_time=0, end_time=end, time_step=dt)
def profile_figure(title, result, model, names, *, reference=None):
theme.use_theme()
fig, axes = plt.subplots(
len(names),
1,
sharex=True,
squeeze=False,
figsize=(7.2, 1.25 * len(names) + 0.85),
layout="constrained",
)
for ax, name in zip(axes[:, 0], names):
ref = model.field(name)
field = result.field(ref)
x = field.space.dof_coordinates[:, 0]
y = np.asarray(field.values).reshape(-1)
assert x.shape == y.shape and np.isfinite(y).all()
order = np.argsort(x)
ax.plot(x[order], y[order], ".-", markersize=2, label="Computed")
if reference is not None and name in reference:
ax.plot(x[order], reference[name](x[order]), "--", label="Reference")
ax.legend(frameon=False)
ax.set_title(name.replace("_", " ").capitalize(), loc="left", fontsize=9)
ax.set_ylabel(ref.unit)
ax.grid(alpha=0.25)
axes[-1, 0].set_xlabel("World x [m]")
fig.suptitle(title, fontweight="bold")
return fig