Working with Model Objects
When using the Model Builder in the COMSOL Desktop interface, an embedded model with variable name model is automatically created. The embedded model has a special status. For example, the automatic code generation tools only consider the embedded model. In addition, when you save to or load from an MPH file, only the embedded model is saved or loaded. General tools include the Save Application As command in the Application Builder and File>Save As, from the File menu of the COMSOL Desktop environment.
However, in an application you are allowed to create and edit multiple models. Saving and loading such models is done by using the built-in methods saveModel and loadModel. An MPH file can only contain a single model object.
If you need to create model objects, in addition to the embedded model, use the built-in method createModel.
To create a new model you use:
Model extmodel = createModel();
A unique model tag is created automatically and assigned to the model. If you want to explicitly control the model tag, use:
Model extmodel = createModel("My_model_1");
where My_model_1 is a unique tag. It is recommended that you do not use the names Model1, Model2, Model3, and so on, since these names are used by the mechanism that automatically generates model tags for the embedded model when loading and saving MPH files.
The following example retrieves the model tag of the embedded model:
String my_modeltag = model.tag();
however, you rarely need to use the model tag of the embedded model object.
Instead of creating and building up the contents of a model from scratch, you can load an existing model and edit it.
For example in the Windows operating system, load a model my_model.mph from the folder C:\COMSOL_Work, by using the built-in method loadModel:
Model extmodel = loadModel("C:\\COMSOL_Work\\my_model.mph");
A unique model tag is created automatically and is assigned to the model upon load. Note the double-backslash syntax in the filename. Backslash (\) is a special character in Java and the double backslash is needed in this case.
To make your application portable, you can use the file scheme syntax available in the Application Builder. Assuming you stored the MPH file in the common folder, the call to loadModel should be:
Model extmodel = loadModel("My_Model_1", "common:///my_model.mph");
In this example, the tag My_Model_1 is important since it is used to retrieve the model from other methods. Once loaded, the model extmodel exists in the work space of the current COMSOL Multiphysics or COMSOL Server session. Note that an MPH file can only contain one model object, so there is no ambiguity on which model you refer to when loading an MPH file.
Assume that you, in one method, have loaded the model extmodel with the tag My_Model_1, such as in the example above. The model variable extmodel is not available in other methods. In order to retrieve the model from another method use:
Model mymodel = getModel("MyModel_1");
The contents of mymodel and extmodel are the same, but these variables exist in the variable space of two different methods.
The tag My_Model_1 uniquely identified and retrieved the model object from the current COMSOL Multiphysics or COMSOL Server session.
To clear the contents of a model object, use the built-in method clearModel.
For a list of model utility methods, see “Model Utility Methods” on page 98.
Turning Off and Resetting The Model History
When running method code in applications or otherwise in order to automate modeling tasks, the stored model history may become excessively large. The model history is used, for example, when saving to a Model M-file or Model Java-file. Because of this, depending on the repetitive nature of your code, you may need to temporarily turn off model history recording as illustrated by the following example:
model.hist().disable();
// some code
model.hist().enable();
If you want to reset the model history to an almost minimal sequence of commands that creates the current state of the model object you can use:
model.resetHist();
In the File menu, this functionality is referred to as Compact History.
Limitations with Loading and Saving Models
If you use the loadModel method to load another model into your application, then the usual functionality for displaying the geometry, mesh, and results, for the loaded model, is not directly available in the application since that functionality is reserved for the embedded model. However, you can use the API to call geometry, mesh, study, and results functionality and extract numerical results from the loaded model. For example, you can change the value of parameters or variables of the loaded model, run a study, and extract numerical results.
In an application, you can display plots generated from models other than the embedded model by writing specific method code. First, you will need to use the loadModel method to load the desired model. Following this, the useGraphics method can be used to display a particular plot group from the loaded model. Here is a simplified example to illustrate this process:
Model m = loadModel(...);
useGraphics(m.result("pg1"), "/form1/graphics1");
In this example, loadModel is used to load a model, and useGraphics is used to display a plot group "pg1" from the loaded model in the graphics component "/form1/graphics1".
Note that useGraphics exclusively supports the display of plot groups. This method cannot be used for displaying other graphical elements like geometry or mesh. Furthermore, the toolbar accompanying these plots is limited compared to the one available for plots from the embedded model. This limitation exists as certain actions associated with the full toolbar are not supported in this scenario.
Note that the loadModel and saveModel methods are not supported in standalone applications that have been compiled with COMSOL Compiler.