Model

class Model(parent)[source]

Represents a Comsol model.

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.

Example:

import mph
client = mph.start()
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')

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. Though if you wish to do that, just use the instance attribute .java to access the entire Comsol Java API from Python and refer to the Comsol Programming Manual for guidance.

The parent argument to the constructor is usually that internal Java object around which this class here is wrapped. But in order to simplify extending the class with custom functionality, the constructor also accepts instances of this class or a child class. In that case, it will preserve the original .java object throughout the class hierarchy so that you can simply “type-cast” an existing Model instance (as loaded by the client) to a derived child class.

name()[source]

Returns the model’s name.

file()[source]

Returns the absolute path to the file the model was loaded from.

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.

selections()[source]

Returns the names of all selections.

physics()[source]

Returns the names of all physics interfaces.

features(physics)[source]

Returns the names of all features in the given physics interface.

The term feature refers to the nodes defined under a physics interface. They define the differential equations, boundary conditions, initial values, etc.

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 datasets.

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, description=None, evaluate=False)[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.

Values are accepted as expressions (strings) or as numerical values (referring to default units). An optional unit may be specified, unless it is already part of the expression itself, inside square brackets.

By default, 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. If the option evaluate is set to True, the numerical value that the parameter expression evaluate to is returned.

A parameter description can be supplied and will be set regardless of a value being passed or not.

load(file, interpolation)[source]

Loads an external file and assigns its data to the named interpolation function.

toggle(physics, feature, action='flip')[source]

Enables or disables features of a physics interface.

If action is 'flip' (the default), it enables the feature if it is currently disabled or disables it if enabled. Pass 'enable' or 'on' to enable the feature regardless of its current state. Pass 'disable' or 'off' to disable it.

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 dataset 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 dataset 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 dataset. If the solution stored in the dataset 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 dataset 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.

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.