Model¶
-
class
Model
(java)[source]¶ Wraps a Comsol model object.
This is a Python wrapper for the Comsol model’s Java API. The class is not intended to be instantiated directly. Rather, the model would be loaded from a file by the client.
The focus of the functionality exposed by this wrapper is to inspect an existing model, possibly change parameters, solve the model, then evaluate the results. The intention is not to create the model from scratch or to extensively modify its structure.
Example:
import mph client = mph.Client() model = client.load('capacitor.mph') model.parameter('U', '1', 'V') model.parameter('d', '1', 'mm') model.solve() C = model.evaluate('2*es.intWe/U^2', 'pF') print(f'capacitance C = {C:.3f} pF')
While only a minimal subset of the functionality is exposed by the class directly, the entire Comsol Java API can be accessed via the instance attribute
.java
.-
parameters
()[source]¶ Returns the global model parameters.
The parameters are returned as a list of tuples holding name, value, and description for each of them.
-
parameter
(name, value=None, unit=None)[source]¶ Returns or sets the parameter of the given
name
.If no
value
is given (the defaultNone
is passed), returns the value of the named parameter. Otherwise sets it.Numerical values are accepted, but will be converted to strings. An optional
unit
may be specified, unless it is already part of the value string itself, inside square brackets.Values are always returned as strings, i.e. the expression as entered in the user interface. That expression may include the unit, again inside brackets.
-
load
(file, interpolation)[source]¶ Loads an external
file
and assigns its data to the namedinterpolation
function.
-
inner
(dataset=None)[source]¶ Returns the indices and values of inner solutions.
These are the solution indices and time values in time-dependent studies, returned as a tuple of an integer array and a floating-point array. A
dataset
name may be specified. Otherwise the default data-set is used.
-
outer
(dataset=None)[source]¶ Returns the indices and values of outer solutions.
These are the solution indices and values in parametric sweeps, returned as a tuple of an integer array and a floating-point array. A
dataset
name may be specified. Otherwise the default data-set is used.
-
evaluate
(expression, unit=None, dataset=None, inner=None, outer=None)[source]¶ Evaluates an expression and returns the numerical results.
The
expression
may be a string, denoting a single expression, or a sequence of strings, denoting multiple. The optionalunit
must be given correspondingly. If omitted, default units are used.A
dataset
may be specified. Otherwise the expression will be evaluated on the default data-set. If the solution stored in the data-set is time-dependent, one or severalinner
solution(s) can be preselected, either by an index number, a sequence of indices, or by passing “first”/”last” to select the very first/last index. If the data-set represents a parameter sweep, theouter
solution(s) can be selected by index or sequence of indices.Results are returned as NumPy arrays of whichever dimensionality they may have. The expression may be a global one, or a scalar field, or particle data.
ValueError
exceptions are raised if anything goes wrong, such as the solution not having been computed.
-
export
(node, file=None)[source]¶ Runs the named export
node
.A
file
name can be specified. Otherwise the file name defined in the export node itself will be used.
-
save
(path=None)[source]¶ Saves the model at the given file-system
path
.If
path
is not given, the original file name is used, i.e. the one from which the model was loaded to begin with. Ifpath
contains no directory information, the current folder (working directory) is used. Ifpath
points to a directory, the model name is used to name the file inside that directory.Overwrites existing files. Imposes a
.mph
file ending.
-