Coefficients
Coefficients represent fields defined on the mesh. These can be either scalar or vector-valued. They are used in various components of mufem, including reports, boundary conditions, and models.
Coefficients are registered to the Coefficient Manager either automatically by models or manually by users.
Coefficient Manager
The mufem.CoefficientFunctionManager
manages all scalar and vector coefficient functions used in
the simulation. Once registered, coefficients can be used in reports or exported using
the Field Exporter.
coefficient_manager = sim.get_coefficient_manager()
Coefficients are registered in two main ways:
Via a model:
Models automatically register coefficients when added to the simulation. As an example, the Refinement Model registers several coefficients
model = mufem.RefinementModel() sim.get_model_manager().add_model(model) print(sim.get_coefficient_manager().list_functions())
['Cell Volume', 'Element Type', 'Refinement Level', 'Global Cell Index', 'Position']
Manually by user:
Users can define and register their own coefficients, see User Coefficients, using:
cff = mufem.CffConstantScalar(1.23) sim.get_coefficient_manager().register_user("MyScalar", cff) print(sim.get_coefficient_manager().list_functions())
['MyScalar', ...]
User Coefficients
Several types of coefficients are available in mufem.
Constant Scalar Coefficient
mufem.CffConstantScalar
defines a scalar field with a constant value.
cff_constant_scalar = mufem.CffConstantScalar(value=1.23)
Sinusoidal Coefficient
mufem.CffSinusoidal
defines a time-varying sinusoidal scalar field.
cff_sin = mufem.CffSinusoidal(amplitude=2.0, frequency=50.0, phase=30.0)
Time Table Coefficient
mufem.CffTimeTable
defines a piecewise scalar function over time.
cff_time_table = mufem.CffTimeTable(time=[0.1, 0.2, 0.3], value=[1.0, 2.0, 3.0])
Expression-Based Coefficient
mufem.CffExpressionScalar
defines a scalar field via a mathematical expression.
cff_expression = mufem.CffExpressionScalar("sqrt(x()^2 + y()^2)")
Expressions support standard functions and mesh coordinates.
Constant Vector Coefficient
mufem.CffConstantVector
defines a constant 3D vector field.
cff_vector = mufem.CffConstantVector(1.0, 2.0, 3.0)
Composite Vector Coefficient
mufem.CffVectorComponent
builds a vector field from three scalar coefficients.
x = mufem.CffConstantScalar(1.0)
y = mufem.CffConstantScalar(2.0)
z = mufem.CffConstantScalar(3.0)
cff_composite = mufem.CffVectorComponent(cff_x=x, cff_y=y, cff_z=z)
Example
import mufem
sim = mufem.Simulation.New("My Case", f"data/geometry.mesh", print_only_warnings=True)
cff_manager = sim.get_coefficient_manager()
# Scalar coefficients
cff_manager.register_user("MyCffConstantScalar", mufem.CffConstantScalar(1.23))
cff_manager.register_user("MyCffSinusoidal", mufem.CffSinusoidal(amplitude=2., frequency=50., phase=30.))
cff_manager.register_user("MyCffTimeTable", mufem.CffTimeTable(time=[0.1, 0.2, 0.3], value=[1.0, 2.0, 3.0]))
cff_manager.register_user("MyCffExpression", mufem.CffExpressionScalar("sqrt(x()^2 + y()^2)"))
# Vector coefficients
cff_vector = mufem.CffConstantVector(x=1.0, y=2.0, z=3.0)
cff_manager.register_user("MyCffConstantVector", cff_vector)
x = mufem.CffConstantScalar(1.0)
y = mufem.CffConstantScalar(2.0)
z = mufem.CffConstantScalar(3.0)
composite = mufem.CffVectorComponent(cff_x=x, cff_y=y, cff_z=z)
cff_manager.register_user("MyCffCompositeVector", composite)
# List all available coefficient names
print(cff_manager.list_functions())
['MyCffExpression', 'MyCffTimeTable', 'MyCffSinusoidal', 'MyCffConstantScalar', 'MyCffCompositeVector', 'MyCffConstantVector']