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

Represents the (single) 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.


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

Due to limitations of the Java bridge, provided by the JPype library, only one client can be instantiated at a time. This is because JPype cannot manage more than one Java virtual machine within the same Python session. Separate Python processes would have to be run to work around this limitation.

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.

Raises RuntimeError if another client is already running.

Internally, the client is a wrapper around the ModelUtil object provided by Comsol’s Java API, which can be accessed directly via the .java instance attribute.


Returns the model loaded from the given file.


Creates and returns a new, empty model with the given name.

This is not particularly useful unless you are prepared to drop down to the Java layer and add model features on your own. It may help to call the returned (Python) model object something like pymodel and assign the name model to Then you can just copy-and-paste Java or Matlab code from the Comsol programming manual or as exported from the Comsol front-end. Python will gracefully overlook gratuitous semicolons at the end of statements, so this approach would even work for entire blocks of code.


Returns all model objects currently held in memory.


Names all models that are currently held in memory.


Removes the given model from memory.


Removes all loaded models from memory.


Disconnects the client from the server.