Docs › Worked numerical comparisons › Coupled execution lab
9.9 Coupled execution lab
9.9.1 Compare monolithic and partitioned thermal exchange
Two transient heat capacities send their temperatures to one another through typed input ports. The monolithic method conserves their capacity-weighted sum to roundoff. The partitioned trajectory has a small time-discretization balance error, which is shown separately.
import numpy as np
import matplotlib.pyplot as plt
import onesimulation_plot_theme as theme
from onesim.cad.geometry import LumpedDomain
from onesim.meshing import LumpedMesh
from onesim.numerics import Numerics
from onesim.numerics.coupled_systems import MonolithicCoupling, PartitionedCoupling
from onesim.numerics.discretization import LumpedParameters
from onesim.numerics.field_transfer import GlobalValueTransfer
from onesim.numerics.nonlinear import FixedPoint
from onesim.numerics.time import ClassicalRungeKutta4
from onesim.physics.systems import ThermalCapacitance
from onesim.simulation import Problem, Simulation
from onesim.simulation.initial_conditions import InitialValue
from onesim.simulation.field_connections import FieldCoupling
class CoupledStep:
def maximum_step(self, **kwargs):
return 0.02
domain = LumpedDomain()
a = ThermalCapacitance(
domain=domain.system, heat_capacity=2, conductance=1, ambient_temperature=None
)
b = ThermalCapacitance(
domain=domain.system, heat_capacity=3, conductance=1, ambient_temperature=None
)
models = (a, b)
problem = Problem(
name="Closed exchange",
geometry=domain,
physics=models,
initial_conditions=(
InitialValue(a.temperature, 320.0),
InitialValue(b.temperature, 300.0),
),
couplings=tuple(
FieldCoupling(
source=s.temperature, target=d.ambient, transfer=GlobalValueTransfer()
)
for s, d in ((a, b), (b, a))
),
)
mesh = LumpedMesh(domain)
method = ClassicalRungeKutta4()
options = {
"Monolithic": (
MonolithicCoupling(
transient_systems=models, time_integrator=method, stability=CoupledStep()
),
Numerics(spatial=LumpedParameters()),
),
"Partitioned": (
PartitionedCoupling(
transient_systems=models,
iteration=FixedPoint(
relative_tolerance=1e-12,
absolute_tolerance=1e-12,
maximum_iterations=80,
),
),
Numerics(spatial=LumpedParameters(), time_integrator=method),
),
}
theme.use_theme()
fig, (ax, balance) = plt.subplots(
2, 1, sharex=True, figsize=(6.8, 4.4), layout="constrained"
)
for label, (strategy, numerical) in options.items():
sim = Simulation(
problem=problem,
mesh={m: mesh for m in models},
numerics={m: numerical for m in models},
coupling_solver=strategy,
)
result = sim.run(start_time=0, end_time=0.6, time_step=0.01)
t = np.asarray(result.times)
Ta = np.array([result.field(a.temperature).at(float(x)).values.item() for x in t])
Tb = np.array([result.field(b.temperature).at(float(x)).values.item() for x in t])
drift = 2 * Ta + 3 * Tb - 1540.0
assert np.max(np.abs(drift)) < (1e-9 if label == "Monolithic" else 0.01)
ax.plot(t, Ta - Tb, label=label)
balance.plot(t, drift, label=label)
ax.set_ylabel("Temperature difference [K]")
balance.set(xlabel="Time [s]", ylabel="Energy balance error [J]")
ax.legend(frameon=False)
plt.show()
