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.

name()[source]

Returns the model’s name.

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.

functions()[source]

Returns the names of all globally defined functions.

components()[source]

Returns the names of all model components.

geometries()[source]

Returns the names of all geometry sequences.

physics()[source]

Returns the names of all physics interfaces.

materials()[source]

Returns the names of all materials.

meshes()[source]

Returns the names of all mesh sequences.

studies()[source]

Returns the names of all studies.

solutions()[source]

Returns the names of all solutions.

datasets()[source]

Returns the names of all data-sets.

plots()[source]

Returns the names of all plots.

exports()[source]

Returns the names of all exports.

rename(name)[source]

Assigns a new name to the model.

parameter(name, value=None, unit=None)[source]

Returns or sets the parameter of the given name.

If no value is given (the default None 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 named interpolation function.

build(geometry=None)[source]

Builds the named geometry, or all of them if none given.

mesh(mesh=None)[source]

Runs the named mesh sequence, or all of them if none given.

solve(study=None)[source]

Solves the named study, or all of them if none given.

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 optional unit 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 several inner 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, the outer 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.

clear()[source]

Clears stored solution, mesh, and plot data.

reset()[source]

Resets the modeling history.

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. If path contains no directory information, the current folder (working directory) is used. If path points to a directory, the model name is used to name the file inside that directory.

Overwrites existing files. Imposes a .mph file ending.