Core interface

This part of the documentation covers cmphy’s core interface to the COMSOL Multiphysics API.

Configuration

This module holds the available configuration options for the package.

cmphy.config.DEBUG = False

Wether to send log messages at the DEBUG level or higher to the console.

cmphy.config.TIMEOUT = 60

Timeout in seconds after that a connection attempt to COMSOL is aborted.

cmphy.config.VERSION = '5.3'

Default COMSOL version.

cmphy.config.configure_logging(config=None)

Configure logging for cmphy using the given dictionary.

Parameters:config (dict, optional) – The logging configuration to use. Use a default configuration if config is None.

Exceptions

This module contains the COMSOL exception classes.

exception cmphy.error.COMSOLError

Bases: Exception

Base class for all COMSOL exceptions.

exception cmphy.error.VersionError

Bases: cmphy.error.COMSOLError

Raised when the requested COMSOL version was not found on the system.

exception cmphy.error.ConnectionError

Bases: cmphy.error.COMSOLError

Raised when a connection attempt to COMSOL failed.

exception cmphy.error.TimeoutError

Bases: cmphy.error.ConnectionError

Raised when a connection attempt to COMSOL was aborted due to timeout.

exception cmphy.error.AlreadyConnectedError

Bases: cmphy.error.ConnectionError

Raised when the Python client is already connected to the server.

exception cmphy.error.APIError(e)

Bases: cmphy.error.COMSOLError

Raised when a COMSOL API function raises a com_error exception.

Parameters:com_error (pywintypes.com_error) – A COM exception raised by the ComsolCom interface.

Session

This module provides connection capabilities to COMSOL.

class cmphy.session.Session(version='5.3', timeout=60, rebuild=False)

Run a COMSOL Multiphysics session in client/server mode.

Start a COMSOL Multiphysics server and connect the Python client to the running server. The mu attribute provides a handle to the ModelUtil object.

Exactly one single Session instance can be active at the same time. An AlreadyConnectedError is raised if multiple client connections are requested (e.g. a second Session is instantiated while the first one is still active).

Parameters:
  • version (str, optional) – The COMSOL version to be started.
  • timeout (int, optional) – The time limit in seconds after which the connection attempt to COMSOL is aborted.
  • rebuild (bool, optional) – Wether to rebuild the Python source code for the ComsolCom interface.
Raises:

AlreadyConnectedError – If the Python client is already connected to the COMSOL server.

Examples

Start a new COMSOL Multiphysics session and create an empty model on the server:

>>> ses = Session()
>>> ses.port
2036
>>> mu = ses.mu  # Get a handle to the ModelUtil object.
>>> model = mu.create(tag="Model1")
>>> mu.tags()
('Model1',)

Open a COMSOL Multiphysics file in the desktop and connect the desktop to the running server:

>>> model = ses.launch("tests\models\session-1.mph")
>>> model.getFilePath()
'E:\Repos\cmphy\tests\models\session-1.mph'

Shutdown the COMSOL session when done:

>>> ses.shutdown()
launch(filepath=None)

Launch a COMSOL desktop that is connected to the running server.

Open a COMSOL Multiphysics model in ‘interactive’ mode. This enables simultaneous access to the model from both clients (the desktop and the Python client).

If another COMSOL desktop is already connected to the server, terminate that process, without saving, and remove the corresponding model from the server.

Parameters:filepath (str, optional) – Open the given MPH-file in the COMSOL desktop. Start an empty desktop if filepath is None.
Returns:The launched COMSOL Multiphysics model.
Return type:Model
mu

IModelUtil – A handle to the ModelUtil object.

port

int – The port number of the COMSOL server.

shutdown()

Shutdown the COMSOL Multiphysics session.