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
versioncan be selected if several are installed, for exampleversion='6.0'. Otherwise the latest version is used.Initializes a stand-alone Comsol session if no
portnumber is specified. Otherwise tries to connect to the Comsol server listening at the given port for client connections.The
hostaddress defaults to'localhost', but could be any domain name or IP address. Ifhost=Noneis passed, the client will remain in a disconnected state untilconnect()is called. This is sometimes useful to catch runtime errors at start-up.This class is a wrapper around the
com.comsol.model.util.ModelUtilJava class, which itself is wrapped by JPype and can be accessed directly via the.javaattribute. 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,
NotImplementedErroris raised if a client is already running.- version¶
Comsol version (e.g.,
'6.0') 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
loadmethod 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
Trueto enable caching,Falseto 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
Modelinstance.An optional
namecan 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
portfor client connections. Thehostaddress 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.