Client#

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')
model.solve()
model.save()
client.remove(model)

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.

version#

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

standalone#

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

port#

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

host#

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

java#

Java model object that this class instance is wrapped around.

property cores#

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

models()[source]#

Returns all models currently held in memory.

names()[source]#

Returns the names of all loaded models.

files()[source]#

Returns the file-system paths of all loaded models.

modules()[source]#

Returns the names of available licensed modules/products.

load(file)[source]#

Loads a model from the given file and returns it.

caching(state=None)[source]#

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.

create(name=None)[source]#

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

remove(model)[source]#

Removes the given model from memory.

clear()[source]#

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.

disconnect()[source]#

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.