class Client(cores=None, version=None, port=None, host='localhost')[source]#

Manages the Comsol client instance.

A client can either be a stand-alone instance or it could connect to a Comsol server started independently, possibly on a different machine on the network.

Example usage:

import mph
client = mph.Client(cores=1)
model = client.load('model.mph')

The number of cores (threads) the client instance uses can be restricted by specifying a number. Otherwise all available cores are used.

A specific Comsol version can be selected if several are installed, for example version='5.3a'. Otherwise the latest version is used.

Initializes a stand-alone Comsol session if no port number is specified. Otherwise tries to connect to the Comsol server listening at the given port for client connections. The host address defaults to 'localhost', but could be any domain name or IP address.

This class is a wrapper around the com.comsol.model.util.ModelUtil Java class, which itself is wrapped by JPype and can be accessed directly via the .java attribute. The full Comsol functionality is thus available if needed.

However, as that Comsol class is a singleton, i.e. a static class that cannot be instantiated, we can only run one client within the same Python process. Separate Python processes would have to be created and coordinated in order to work around this limitation. Within the same process, NotImplementedError is raised if a client is already running.


Comsol version (e.g., '5.3a') the client is running on.


Whether this is a stand-alone client or connected to a server.


Port number on which the client has connected to the server.


Host name or IP address of the server the client is connected to.


Java model object that this class instance is wrapped around.

property cores#

Number of processor cores (threads) the Comsol session is using.


Returns all models currently held in memory.


Returns the names of all loaded models.


Returns the file-system paths of all loaded models.


Returns the names of available licensed modules/products.


Loads a model from the given file and returns it.


Enables or disables caching of previously loaded models.

Caching means that the load() method will check if a model has been previously loaded from the same file-system path and, if so, return the in-memory model object instead of reloading it from disk. By default (at start-up) caching is disabled.

Pass True to enable caching, False to disable it. If no argument is passed, the current state is returned.


Creates a new model and returns it as a Model instance.

An optional name can be supplied. Otherwise the model will retain its automatically generated name, like “Model 1”.


Removes the given model from memory.


Removes all loaded models from memory.

connect(port, host='localhost')[source]#

Connects the client to a server.

The Comsol server must be listening at the given port for client connections. The host address defaults to 'localhost', but could be any domain name or IP address.

This will fail for stand-alone clients or if the client is already connected to a server. In the latter case, call disconnect() first.


Disconnects the client from the server.

Note that the server, unless started with the option multi set to 'on', will shut down as soon as the client disconnects.