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 exampleversion='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. Thehost
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.
- 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”.
- connect(port, host='localhost')[source]#
Connects the client to a server.
The Comsol server must be listening at the given
port
for client connections. Thehost
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.