ModelUtil
Model object utility methods such as methods to create and remove model objects, showing progress information, and listing and saving preferences. See also model.
Syntax
This section describes general methods that handle the environment for the model object. It also describes methods for the client/server machinery.
import com.comsol.model.*;
import com.comsol.model.util.*;
 
The import statements above make all model and model utility methods available.
ModelUtil.create(<tag>): The create method creates a model with tag <tag>. Returns a reference to the model. If there is already a model with this tag the previous model is removed.
ModelUtil.remove(<tag>): The remove method removes the model tagged <tag>.
ModelUtil.clear(): The clear method removes all models.
ModelUtil.tags(): The tags method obtains the current list of model tags.
ModelUtil.model(<tag>): The model method returns a reference to the model tagged <tag>.
ModelUtil.closeWindow(<windowtag>): The closeWindow method closes the window tagged <windowtag>.
ModelUtil.closeWindows(): The closeWindows method closes all windows on the server.
ModelUtil.createApplication(<tag>,<appname>): The createApplication method creates a new application model with the tag <tag> and the name <appname> using the given application file reference.
ModelUtil.createUnique(<prefix>): The createUnique method creates a model with a unique tag that begins with the prefix <prefix>.
ModelUtil.getComsolVersion(): The getComsolVersion method returns the current COMSOL Multiphysics version as a string.
ModelUtil.getOpenGeometryKernel(): The getOpenGeometryKernel method returns the geometry kernel to use when opening models. Valid values are model for the geometry kernel used by the model file, and comsol to convert the geometry to the COMSOL kernel.
ModelUtil.getDefaultGeometryKernel(): The getDefaultGeometryKernel method returns the default geometry kernel in new models. Valid values are comsol for the COMSOL kernel and cadps for the CAD kernel (Parasolid kernel). The CAD kernel requires the CAD Import Module.
ModelUtil.setOpenGeometryKernel(<openkernel>): The setOpenGeometryKernel method specifies the geometry kernel to use when opening models. Valid values are model for the geometry kernel used by the model file, and comsol to convert the geometry to the COMSOL kernel.
ModelUtil.setDefaultGeometryKernel(<defaultkernel>): The setDefaultGeometryKernel method specifies the default geometry kernel to use in new models with the string <defaultkernel>. Valid values are comsol for the COMSOL kernel and cadps for the CAD kernel (Parasolid kernel). The CAD kernel requires the CAD Import Module.
ModelUtil.getEntityPath(<entity>,<divider>): The getEntityPath method creates the path from the root of the model object for the given entity <entity>. <divider> specifies a divider to use between entities in path.
ModelUtil.load(<tag>,<filename>): The load method loads a model from a file <filename> in the client’s file system and names it <tag>. Loading a file from a directory sets the model directory. The model directory is used for saving files if you do not provide an absolute path to the file. The model directory is the directory where the model is saved. If the model has not been saved there is no model directory. You can get the model directory from a saved model using model.getFilePath.
ModelUtil.loadCopy(<tag>,<filename>): The loadCopy method loads a copy of a model from a file <filename> in the client’s file system and names it <tag>. The loadCopy method is the same as load except that the loaded model is not associated with the file, so model.save() does not work. You have to specify the filename the first time you save it again.
ModelUtil.loadOnServer(<tag>,<filename>): The loadOnServer method works like ModelUtil.load except that the filename is a path on the server computer. The client does not have to have access to the file.
ModelUtil.loadProtected(<tag>,<filename>,<password>): The loadProtected method works like ModelUtil.loadOnServer but with password protection.
ModelUtil.loadProtectedOnServer(<tag>,<filename>,<password>): The loadProtectedOnServer method works like ModelUtil.loadOnServer but with password protection.
ModelUtil.loadRecovery(<tag>,<foldername>): The loadRecovery method loads a model from a recovery directory or folder structure in the client’s file system and names it <tag>.
ModelUtil.showPlots(bool): The showPlots method applies when connected to a graphics server, and ModelUtil.showPlots(false) will disable plotting. It will not close any existing plot windows. Use ModelUtil.closeWindow(<tag>) or ModelUtil.closeWindows() to do that.
ModelUtil.showProgress(bool): The showProgress method with a Boolean input turns on or off showing of progress in a window or on a file when running lengthy tasks when connected to a server. The return value is a Boolean value that is true if showing progress is possible.
ModelUtil.showProgress(<filename>): The showProgress method with a filename input turns on logging of progress to the file <filename> in the client’s file system. If <filename> is null progress is logged to the standard output.
ModelUtil.initStandalone(bool): The initStandalone method initializes the environment for using the COMSOL API from a standalone Java® application. You should not use this command from the LiveLink™ for MATLAB®. Set the argument to true if support for plotting in a GUI using Java Swing widgets should be available.
ModelUtil.initStandalone(bool,<guiToolkit>) allows to specify that support for using a given Java GUI toolkit should be available. The optional <guiToolkit> parameter can have the values "swing" or "swt" telling that Swing widgets or widgets from the Standard Widget Toolkit (SWT) can be used.
ModelUtil.getPreference(<prefsName>): The getPreference method returns the value of a preference.
ModelUtil.setPreference(<prefsName>, <value>): The setPreference method sets the value of a preference.
ModelUtil.listPreferences(): The listPreferences method returns a string with a listing of the preferences names and their descriptions.
ModelUtil.loadPreferences(): The loadPreferences method loads the preferences from file. Use this is standalone Java application which do not load the preferences at launch time.
ModelUtil.savePreferences(): The savePreferences method saves the preferences to file.
The preferences are also saved when you close the Preferences dialog box and when you exit COMSOL Desktop. When you run a thin client, preferences are saved when you call ModelUtil.disconnect() (see Client-Server Commands below).
ModelUtil.uniquetag(): The uniquetag method returns a unique model tag that is not in use.
ModelUtil.modelsUsedByOtherClients(): The modelsUsedByOtherClients method returns the tags of models used by other clients.
License Commands
ModelUtil provides functionality to check availability for and control the checkout of COMSOL product licenses.
ModelUtil.hasProduct(String... product): The hasProduct method checks if the current license allows to run the specified COMSOL products given as the input (as an array of strings).
ModelUtil.hasProductForFile(String file): The hasProductForFile method checks if the current license allows the specified COMSOL products needed to use that COMSOL MPH file.
ModelUtil.hasProductForFileonServer(String file): The hasProductForFileonServer method is similar to hasProductForFile but checks if the license allows the specified COMSOL products needed for a file on the server.
ModelUtil.checkoutLicense(String... product): The checkoutLicense method checks out licenses for the COMSOL products given as the input (as an array of strings).
ModelUtil.checkoutLicenseForFile(String file): The checkoutLicenseForFile method checks out the licenses needed to use that COMSOL MPH file.
ModelUtil.checkoutLicenseForFileonServer(String file): The checkoutLicenseForFileonServer method is similar to checkoutLicenseForFile but checks out the licenses needed to use that COMSOL MPH file on the server.
The following table lists the available products for which licenses can be checked for availability and checked out using the names in the Name column:
CADIMPORT, CADREADER
LiveLink™ for AutoCAD®
LiveLink™ for PTC® Creo® Parametric
LiveLink™ for Excel®
LiveLink™ for Inventor®
LiveLink™ for MATLAB®
LiveLink™ for Revit®
LiveLink™ for PTC® Pro/ENGINEER®
LiveLink™ for Solid Edge®
LiveLink™ for SOLIDWORKS®
Client-Server Commands
ModelUtil provides functionality to control COMSOL client-server options. You can connect/disconnect and control connections from multiple clients to a server using, for example, the connect and disconnect methods.
ModelUtil.connect() connects to a COMSOL server (COMSOL Multiphysics server or COMSOL Server™). The COMSOL command arguments -Dcs.host=<host> and -Dcs.port=<port> can provide the hostname and port number. In case those are not provided, and the both client and server access the same file system, the host and port can be automatically transferred.
ModelUtil.connect(<host>,<port>) connects to a COMSOL server. The arguments <host> and <port> provide the hostname (a string) and port number (an integer) for the COMSOL server.
ModelUtil.connect(<host>,<port>,<user>,<password>) connects to a COMSOL server. The arguments <host>, <port>, <user> and <password> provide the hostname (a string), port number (an integer), user (a string), and password (a string) for the COMSOL server.
ModelUtil.connect(<host>,<port>,<encryption>) and ModelUtil.connect(<host>,<port>,<encryption>,<user>,<password>) connects to a COMSOL server using encryption. With the Boolean <encryption> set to false, it uses the ws WebSocket URI scheme; when set to true, it uses the secure wss (SSL) WebSocket URI scheme.
ModelUtil.disconnect() disconnects from a COMSOL server.
ModelUtil.setServerBusyHandler(<ServerBusyHandler>): Use the setServerBusyHandler method to register a ServerBusyHandler. Several clients can be connected to the same server, but only one client at a time can ask the server to perform an operation. The default behavior when attempting to call the server when it is busy is to issue an exception. By registering a ServerBusyHandler it is possible to make the client wait until the server is free again, and to set a time-out for how long to wait.
ServerBusyHandler is a class which controls how long a client waits for a busy server to become free again. It also has hooks to perform any action on the client side before starting to wait and just after stopping to wait. Implement a subclass in Java to ServerBusyHandler to change the default behavior.
ServerBusyHandler() creates a server-busy handler that waits for the server to be free without any time-out.
ServerBusyHandler(<timeOut>) creates a server-busy handler that waits for the server to be free. The time to wait, <timeOut>, is given in milliseconds.
postWaitForServer(<boolean>) is a hook to perform any action right after waiting for the server to become free.
preWaitForServer(<host>, <port>) is a hook to perform any action before beginning to wait for the server to become free.
ModelUtil.setModelChangedHandler(<ModelChangedHandler>): The setModelChangedHandler method registers a handler of changes to models made by other clients. If any other client changes any model in use by this client, the model change handler is notified.
ModelChangedHandler is an interface for handling updates of the client when another client has modified any models in use by this client. Implement the interface in Java to change the default behavior.
handleModelChangeOnServer(<modelChangeInfo>) is called when another client has changed any models in use by this client.
ModelChangeInfo is a class with information about changes to models by other clients.
getModelTags() returns the tags of the models that have changed.
The scanModel Method
The scanModel method is an advanced tool that scans the raw data of a model file for a certain node type and collects the tag and label of all nodes that it finds. See Scanning Models for more information.