Docs › Worked numerical comparisons › Nonlinear solver lab
9.6 Nonlinear solver lab
9.6.1 Solve the same nonlinear implicit cooling stage
Select the nonlinear method separately from backward Euler. Compare the final temperature to the Newton route and keep the time grid fixed. The difference isolates stage convergence, while the bias relative to the analytic cooling curve also includes time error.
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.discretization import LumpedParameters
from onesim.numerics.linear import GMRES, SparseDirect
from onesim.numerics.nonlinear import (
Newton,
DampedNewton,
TrustRegionNewton,
QuasiNewton,
JFNK,
FixedPoint,
)
from onesim.numerics.time import BackwardEuler
from onesim.physics.heat_transfer import (
RadiationCooling,
ThermalGreyDiffuseRadiationBoundary,
)
from onesim.simulation import Problem, Simulation
from onesim.simulation.initial_conditions import InitialValue
domain = LumpedDomain()
model = RadiationCooling(
domain=domain.system,
heat_capacity=2,
radiating_area=0.3,
radiation=ThermalGreyDiffuseRadiationBoundary(
condition_id="radiation",
emissivity=0.5,
ambient_temperature=1,
stefan_boltzmann=0.02,
),
)
problem = Problem(
name="Nonlinear cooling stage",
geometry=domain,
physics=(model,),
initial_conditions=(InitialValue(model.temperature, 2.0),),
)
settings = dict(relative_tolerance=1e-12, absolute_tolerance=1e-13)
solvers = {
s.__name__: s(linear_solver=SparseDirect(), jacobian="analytic", **settings)
for s in (Newton, DampedNewton, TrustRegionNewton, QuasiNewton)
}
solvers["JFNK"] = JFNK(linear_solver=GMRES(), jacobian="analytic", **settings)
solvers["FixedPoint"] = FixedPoint(**settings)
values = {}
for label, solver in solvers.items():
sim = Simulation(
problem=problem,
mesh=LumpedMesh(domain),
numerics=Numerics(
spatial=LumpedParameters(),
time_integrator=BackwardEuler(),
nonlinear_solver=solver,
),
)
result = sim.run(start_time=0, end_time=1, time_step=0.05)
values[label] = np.asarray(result.field(model.temperature).values).item()
assert max(abs(y - values["Newton"]) for y in values.values()) < 1e-9
theme.use_theme()
fig, ax = plt.subplots(figsize=(6.8, 3.3), layout="constrained")
ax.barh(list(values), [(2 - y) * 1e3 for y in values.values()])
ax.set_xlabel("Temperature drop after 1 s [mK]")
plt.show()
