How to Manually Work with Entity Numbers in Selections
Lars Gregersen June 4, 2014
In Part 1 of this blog series, I introduced how you can export a model M-file from COMSOL Multiphysics® simulation software to learn about the structure of the COMSOL Application Programming Interface (API). One important part of a model M-file is the selections that are made in order to set up properties for the domain, boundaries, etc. These selections are identified using numbers. Here, we explain how you can automate the handling of the entity numbers using LiveLink™ for MATLAB®.
Handling Selections When Changing the Geometry
When large changes are introduced to a model’s geometry, keeping track of the numbers that domains, boundaries, edges, or vertices are assigned is a challenge. These numbers are used to specify where certain settings should be applied:
The upper part of the above figure shows a heat sink with only one fin above the heat sink base. This is not a very efficient design, so I’ll add further fins in order to calculate the heat sink’s performance for different designs.
As you can see in the lower part of the figure, the numbering of the boundaries changes when a second fin is introduced on top of the heat sink base. Naturally, the boundaries that are a part of the newly introduced fin have to get numbers that are different from the previous design, since these boundaries have not been a part of the model so far. Introducing the new boundaries also means that some of the old numbers will change.
Note that the numbering is usually very difficult to predict, even for a simple 2D model like this. For a more complicated model in 2D and 3D, it becomes even more complicated. The examples and code used here could just as easily have been made with a 3D model, but I’ve chosen to go with a 2D model, as it becomes much easier to follow what’s going on.
When applying model settings to boundaries, the numbering becomes important. The figure below shows what the boundary condition for the cool side of the heat sink looks like:
The boundaries are easily identified in the Graphics window on the right. The corresponding numbers are seen in the Heat Flux settings window in the middle (circled in pink).
Selections in the Model M-file Code
Let’s have a look at what the corresponding selections look like in the model M-file code:
The first set is for the upper part, where the cooling takes place, and the second set represents the hot part of the heat sink. When the second fin (or more, depending on your modeling scenario) is added to the model, we normally have little idea of which selection numbers to use.
There are two distinct ways of keeping track of the entity numbers:
- Using LiveLink™ for MATLAB® functionality to track geometric entities based on their coordinates
- Working with selections from geometric operations to track geometric entities
Method 1 is usually the easiest when you are introducing very large changes (such as new objects, for instance, like we will do here) into a geometry and the number of geometric entities change. This method makes it very easy to work with a model on the command line when changing the model bit by bit.
Method 2 usually requires more steps to set up, but has the added benefit that once the model has been set up, you can avoid the use of entity numbers altogether. The model can be used either via LiveLink™ for MATLAB® or directly in the COMSOL Multiphysics® user interface (UI), with ease.
Note that if you have a model where you don’t add or remove objects and you do not change its topology, then the numbering of the selections in the model will automatically be updated when you change model settings, and, for example, move objects from one location to another.
Coordinate Based Selections Using LiveLink™ for MATLAB® Functionality
The model is available as both an M-file and an MPH-file. Typically, the best solution is to use the MPH-file as a base for the changes to the model. Loading an MPH-file is usually faster than running the corresponding M-file. The MPH-file can, of course, contain meshes and solutions that can’t be saved as part of the M-file, which makes plotting model results a lot easier.
We load the model using this command:
model = mphload('heat_param1')
A new fin is introduced into the model by these simple commands:
The design is verified by using this command:
which plots the geometry in a MATLAB® figure.
This produces the lower part of the heat sink figure . In this figure, the numbers can be identified visually, but we would like to automate the process such that we can vary both the number of fins and their design. Getting the entity numbers of the boundaries that we need is easy if we use the
mphselectbox command. This command selects entities using a box for which you have to specify the coordinates of two opposite corners. There is a corresponding function called
mphselectcoords that selects entities inside a circle (or a sphere in 3D), but we will not use that function in this example.
Below, are the
mphselectbox commands that we have to use for this model. For the cooling fins, we supply one set of coordinates that cover the entire upper part of the heat sink. This means that when I add more fins to the heat sink, these new fins will be covered by the rectangle and I can use the same code to get the selection numbers at that time. For the hot, lower part of the heat sink, I have to use two calls to mphselectbox and take the union of the selection numbers. “Union” is a standard MATLAB® function that takes the union of a set. It is also possible to use the MATLAB® functions, “setdiff” and “intersect”, in order to play around with the selection numbers to get the result you wish.
The output looks like this:
Now it is easy to update the model settings with the correct numbers:
Once the selections have been set up, it is easy to adjust the number of fins. The following script shows how to set up a simple loop that will add four fins (one by one) to a model and solve. The goal is to find the effect of adding more fins to the heat sink.
The first line is used to load the model from an MPH-file. This way, we don’t have to run an M-file, which usually takes longer time than loading an MPH-file.
In the for loop, some more fins are added to the model with a proper size and location. The selection numbers are retrieved using mphselectbox and the model is solved. We generate a plot of each result to be studied when the analysis is completed.
The method “uniquetag” is used to get a tag for the new fin (Rectangle). When we use uniquetag, we don’t have to guess what tags are already taken and which might be available. This use of uniquetag with an ‘r’ as argument will return a tag that consists of ‘r’ and a number that is available.
What We Have Learned
When we change the geometry in a model (especially when we introduce new topology), it is important to keep track of the entity numbers for specifying model settings. An efficient way of doing this in a model M-file, or at the MATLAB® command line, is by using the wrapper functions,
mphselectcoords, that return entity numbers based on entity coordinates.
The next blog post in this Working with M-files series will discuss how we can set up a model using selections from geometric operations. This leads to a model that is easy to handle both with LiveLink™ for MATLAB® and from within the COMSOL Multiphysics® user interface. Using this method means we are working directly with the selection objects and, in many cases, can avoid the use of entity numbers altogether.
MATLAB is a registered trademark of The MathWorks, Inc.