OneSimulation docs Website Free books Plans on Gumroad

Docs › Build a first case in each physics package › Reusable periodic-line setup

5.1 Reusable periodic-line setup

Several reference cases use periodic endpoints to isolate wave propagation or diffusion from boundary effects. Construct the model on the same line passed to the helper. The returned fields remain on the selected numerical carrier. When comparing against a Fourier mode, use point values for nodal or sample data and cell averages for FV data. For a uniform mesh of \(N\) cells per wavelength, the cell mean of the sinusoidal part is the center value multiplied by \(\operatorname{sinc}(1/N)\) in NumPy's normalized convention.

import numpy as np
from onesim.cad.d1 import Line
from onesim.meshing import PeriodicPair, StructuredLineMesher
from onesim.numerics import Numerics
from onesim.numerics.time import ClassicalRungeKutta4
from onesim.simulation import Problem, Simulation
from onesim.simulation.initial_conditions import InitialValue
from onesim.simulation.problem_definition.boundaries import PeriodicBoundary

def run_periodic_line(line, model, initial, numerics, *, cells, end, dt):
    mesh = StructuredLineMesher(cells=cells).generate(line)
    problem = Problem(
        name=type(model).__name__, geometry=line, physics=(model,),
        initial_conditions=tuple(InitialValue(ref, value) for ref, value in initial),
        boundary_conditions=(PeriodicBoundary(
            physics=model, pair=PeriodicPair(line.boundaries.start, line.boundaries.end),
        ),),
    )
    sim = Simulation(problem=problem, mesh=mesh, numerics=numerics)
    sim.validate().require_valid()
    return sim.run(start_time=0.0, end_time=end, time_step=dt)