How to Export Images Automatically After Solving Your Model

July 11, 2017

In a previous blog post, we looked at setting up a programmatic sequence of operations under the Study node for solving, saving a model to file, and exporting data to file. Today, we are building on this knowledge to show how an entire sequence of images can be automatically exported after solving a model in the COMSOL Multiphysics® software.

Example: Micromixer Model

To demonstrate this functionality, just like in the previous blog post, we will first load the Micromixer tutorial model from the Application Libraries. This model is available in the folder COMSOL Multiphysics > Fluid Dynamics and illustrates fluid flow and mass transport in a laminar static mixer.

A screenshot of the Application Libraries with the Micromixer tutorial model highlighted.

The model performs a fluid flow simulation using a Laminar Flow interface. In the next step, it shows how to calculate the mixing efficiency by means of a Transport of Diluted Species interface, using the results from the fluid flow simulation as input. The species will be transported downstream based on the fluid velocity.

The computation time for this model is a few minutes. In the previous blog post, we made the computations go faster by not solving for the Transport of Diluted Species part. However, this time around, we will need the concentration profile throughout the mixer. To run the computation quicker in this case, you can set the Predefined Element Size to Extremely coarse.

A screenshot of the element size settings for the micromixer model, which can speed up computation time.

For this example, the step of coarsening the mesh is optional and everything that follows will work even without this change.

Let’s now see how to use a parameterized slice plot together with an animation to export an entire series of images, where each image corresponds to a single slice.

Using Parameters to Position a Slice Plot

This is the default plot for the concentration at 5 different yz-plane slices in the x direction in a solved version of the library model:

An image of the solved micromixer model in the COMSOL Multiphysics® GUI.

You can get a slightly improved visualization by setting the Quality Resolution setting to Extra fine, like so:

A screenshot of the Quality Resolution settings for the micromixer model, which can improve the visualization.

Instead of the default 5 evenly spaced slices in the Slice plot for the Concentration, you can change the Plane Data entry method to Coordinates. For example, you can generate a single slice at 0.5 mm, as follows.

A screenshot showing how to generate a single slice of plane data in COMSOL Multiphysics®.

The resulting plot is shown in the figure below:

An image showing a single slice of plane data in the Graphics window.

We can parameterize the location of the slices by means of a parameter. Right-click the Results node and select Parameters.

A screenshot of the Parameters option under the Results node.

Define a parameter xcut with the value -3.5[mm]. (The microchannel ranges from -3.5 mm to 8 mm in the x direction.)

A screenshot showing how to define a parameter in COMSOL Multiphysics®.

For the Slice plot, in the section for Plane Data, type xcut in the edit field for X-coordinates.

A screen capture showing how to add a parameter to the plane data feature.

The corresponding slice plot is shown here:

A graphic of a Slice plot that was positioned using a set parameter.

Using an Animation to Export a Sequence of Images

What if you would now like to export a series of images corresponding to different values of the slice position? For this purpose, you can use a file-export-based animation.

To generate an animation, select File from the Animation menu in the ribbon toolbar.

A screenshot showing how to generate an animation from simulation results.

Alternatively, you can right-click the Export node under Results and select Animation > File.

A screenshot showing how to export an animation generated from simulation results.

In the Settings window of the Animation node in the model tree, select Image sequence as the Output type.

A screenshot of the Image Sequence option, selected for the output type for the simulation animation.

For the Filename, type, for example, C:\COMSOL\my_image.png. This assumes that you have a folder C:\COMSOL in your system, but you can write to any folder where you have write permission.

A screenshot showing how to set the Filename type for an image sequence.

To link the exported file to the parameter xcut, change the Sequence type to Result parameter. This setting is available in the Animation Editing section.

A screenshot showing how to link the exported image sequence file to a defined parameter.

Choose xcut as the Parameter, with Start set to -3.5, Stop set to 8, and Unit set to mm.

A screenshot showing the options for setting the parameter, start and stop values, and unit for an animation.

At the top of the Settings window for Animation, click Export to start the generation of images. The images will get a suffix corresponding to the number in the sequence. The number of frames, or images, is set in the Frames section.

A screenshot of the Frames section in the Animation Settings window.

A series of images is generated with names: my_image01.png, my_image02.png, …, my_image25.png, as shown in the screenshot below.

A screenshot of a file folder showing a series of images generated from the animation.

Automatic Export After Solving the Model

Let’s now see how the generation of images can be made automatically after solving a model in COMSOL Multiphysics.

To be able to define a sequence of operations under the Study node, we enable Advanced Study Options. This is an available menu option under the Model Builder toolbar. Click the “eye” symbol to see the menu.

A screenshot showing how to enable the Advanced Study Options setting from the Model Builder toolbar.

Under the Job Configuration node that is now visible, select Sequence. This procedure was described in the previous blog post on how to use job sequences.

A screen capture showing the Sequence option selected under the Job Configuration node.

In the Settings window for Solution, select All. This ensures that all study steps are run.

A screenshot of the settings used in order to run all of the study steps in the job sequence.

Right-click Sequence and select Results > Export to File.

A screenshot of the Export to File option under the Sequence node.

In the Export to File Settings window, for the Run option, select Animation 1. In this simple example with just one node under Export, we could here just as well have left the default option All.

A screenshot showing the Export to File Settings window.

To solve using the Sequence, right-click and select Run. Alternatively, click the Run button at the top of the Settings window.

A screenshot showing the settings used to export images automatically after solving your model.

Using Cut Planes to Export 2D Images

The previous export operation generated a series of 3D images. What if you want a series of 2D images for each slice? This can be accomplished by using a parameterized Cut Plane.

Right-click the Data Sets node and select Cut Plane.

A screen capture of the Data Sets node with the Cut Plane attribution.

In the Settings window of the Cut Plane, type xcut for the X-coordinate.

A screenshot of the Settings window for the cut plane.

The already existing 3D plot groups are not useful for 2D plots, so right-click Results and select 2D Plot Group.

A screenshot of the 2D Plot Group option under the Results node.

In the Settings window for the 2D Plot Group, select Cut Plane 1 as the Data set.

A screenshot of the 2D Plot Group Settings window.

Add a Surface plot node under the 2D Plot Group and change the Expression to c, corresponding to the concentration.

A screenshot of the settings for the Surface plot node, highlighting the changed expression.

To tidy up the list of plot groups, change the name of the 2D Plot Group to Cut Plane Concentration.

A screenshot showing the changed name of the 2D Plot Group, which is Cut Plane Concentration.

Now, go to the Animation node in the model tree. In the corresponding Settings window, change the Subject to Cut Plane Concentration.

A screen capture showing how to change the subject in the Animation node settings.

Click Export to generate the sequence of 2D images, as shown in the file browser view here:

A graphic showing the 2D image sequence in a file browser.

To get this view using Windows® Explorer, change the view to Large Icons.

Just like in the previous example, you can now go ahead and run the Job Sequence to solve and then have the set of images generated and saved to file — automatically.

To try this example yourself, click on the button above to access the MPH-files.

Microsoft and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

Comments (0)

Leave a Comment
Log In | Registration