Run a first duct case¶
This page solves a square duct in a magnetic field, the Hartmann duct, and checks the answer against its exact profile. You do it twice, once from Python and once from a TOML file on the command line. Both take a second or two on a laptop.
From Python¶
The steps below are the script examples/hartmann_example.py, cut into pieces.
1. Set up the problem. hartmax.duct(ha, cells) is the square insulating
duct of unit half-width at Hartmann number ha, driven by a unit pressure
gradient, with the field along \(y\):
import hartmax
hartmax.configure(dtype="float64")
problem = hartmax.duct(20.0, 24)
Properties are nondimensional. Density, viscosity and conductivity are one
unless you change them. Problems, solvers and one verb lists every
field of a Problem and the other presets.
2. Solve it. One call runs the solve, and the Output writes a restart
record, a ParaView file, a summary and a log into artifacts/hartmann:
solution = hartmax.solve(problem, output=hartmax.Output("artifacts/hartmann"))
print(f"residual {float(solution.residual):.2e}, flow rate {float(solution.flow_rate):.6g}")
residual 1.25e-08, flow rate 0.156144
The printed residual comes from the certified solve. The iterative residual varies a little with the platform.
3. Check it. Compare the velocity along \(y\) at the midplane \(z=0\) with the exact Hartmann profile \(1-(\cosh(Ha\,y)-1)/(\cosh Ha-1)\):
import numpy as np
faces = np.asarray(problem.grid.faces[1])
y = 0.5 * (faces[1:] + faces[:-1]) # cell centres across the field
u = np.asarray(solution.velocity[0].data[0])
profile = u[:, 11:13].mean(axis=1) # the two cells beside z = 0
profile /= profile.max()
reference = 1.0 - (np.cosh(20.0 * y) - 1.0) / (np.cosh(20.0) - 1.0)
error = profile - reference
print(f"L2 {np.sqrt(np.mean(error**2)):.4f}, Linf {abs(error).max():.4f}")
L2 0.0064, Linf 0.0098
These are the errors that validation quotes for this case. The full script also writes them beside the solved fields:
python examples/hartmann_example.py
From a TOML file¶
The command line reads the same objects from a TOML file, in which a table is a
constructor call. examples/hartmann_case.toml is the same duct on 32 cells:
schema_version = 3
name = "hartmann_ha20_toml"
units = "nondimensional"
[problem]
preset = "duct"
ha = 20.0
cells = 32
[output]
dir = "../artifacts/examples/toml_hartmann"
Run it:
hartmax examples/hartmann_case.toml
The command writes the files of its [output] table, which are a restart
record, a ParaView file, the summary and the log. It then prints the summary and
exits nonzero if the solve did not converge. In the summary, "flow_rate" reads
0.154647 and "residual" is about 5e-09.
Override any key of the file with table.key=value, for example a stronger
field:
hartmax examples/hartmann_case.toml problem.ha=40