How to Run COMSOL Multiphysics® from the Command Line

December 9, 2020

Have you ever wanted to run repeated variations of the same model file from the command line and automatically export data? With just a little bit of preparation work within the user interface (UI) of the COMSOL Multiphysics® software, you can augment your model file with a method that can automate quite a bit of model setup and evaluation from the command line. Let’s find out how…

Writing Out Data from a Model

To put this workflow into context, let’s look at a tutorial model of a busbar, which we use in our introduction to the COMSOL® software. This model has several parameterized inputs to study as well as outputs to report.

Let’s begin by writing some data out of our model to disk. Our model has several plots already set up, including one of the temperature field. Right-click on this feature and select the Add Image to Export option. You will get a Results > Export > Image feature, with settings as shown in the screenshot below. Within these settings, enter an output filename. Note that you can also adjust the Image and Layout settings as desired. You can add several of these features, and export other types of data, including animations, mesh information, and more. To write out all of these data at once, right-click on the Export branch and select the Export All option.

A screenshot of the COMSOL Multiphysics model tree with the Results node opened to the Export branch and the image export settings opened.
Screenshot showing the Export branch within the Results used to write out an image file.

Let’s also extract some scalar values. Add an Evaluation Group feature to the Results branch, and add to it any number of different derived scalar values. For example, take the integral of the losses over all domains and evaluate maximum temperature. Within the settings for the Evaluation Group, also specify the filename, as shown below.

A screenshot of the Evaluation Group node opened to the settings that specify writing numerical data to a file.
Screenshot showing the Evaluation group, which will write a combination of numerical data out to a file.

With the combination of the Evaluation Group and the Export features, we can write out any amount of data from our model. Now, let’s look at how we can automate this a bit further.

Automating Operations via a Model Method

To get started with using methods, we need to be working on the Microsoft® Windows™ operating system, since we will be using the Application Builder functionality. Click on the Application Builder button in the ribbon, or use the keyboard shortcut Control+Shift+A, and you’ll be brought to the interface shown below. The one task that we will do here is add a new method with the Methods branch. Give it a name, e.g., my_method, but let’s not put anything into it yet. Return to the Model Builder via the button in the top left, or via the keyboard shortcut Control+Shift+M.

The Application Builder zoomed in and open on a blank Method Editor window for writing a method to run COMSOL Multiphysics from the command line.
Screenshot of the Application Builder with a method.

Once back in the Model Builder, let’s add in a call to our method. Right-click on the Global Definitions and go to the Method Calls submenu, where you’ll see the method we just created. Add this, and make sure to also alter the Tag field. Usually, you can just use the same name as the name used in the Application Builder. You can see in the screenshot below that the Method Call feature has three buttons:

  1. Run
  2. Pause
  3. Edit Method

The Edit Method button is a convenient shortcut to take us back to the Application Builder, which is what we’ll do as soon as we have some code to put into our method.

A screenshot of a Settings window showing how to add a call to a method in the Model Builder.
Adding a call to our method within the Model Builder, which will let us run and edit the method.

If you’re not very familiar with coding, there’s really just one thing that you’ll need to know for what we’re about to do: A method contains snippets of code, with each line containing some kind of modification or action on the model. There are two ways to extract these code snippets from the Model Builder.

The first approach is to go to the Developer tab and click on Record Method. You can then perform a whole series of actions within the Model Builder, until you click the Stop Recording button. The related code will be saved into the new method.

In the second approach, right-click on a feature within the model tree, and (in most cases) you’ll see a Copy as Code to Clipboard submenu. Try this on the Study branch of the model, where one of the options is Run. Select this option to copy the code snippet for running the study to the clipboard. Go to the Method Editor and paste (Control+V) to see the code:

model.study("std1").run();

This second approach is a bit simpler, so let’s use this approach within this blog post.

A screenshot of the export settings with a menu expanded and the option Copy as Code to Clipboard selected.
Copying the code to run a particular node of the model tree.

We can add more code that automates the data extraction. We already set up features that write data out to disk, and now we just need to include the running of these features in our method. Right-click on the Export feature and again use Copy as Code to Clipboard in the Run command, as shown above. Paste these into your method, and repeat this for the Evaluation Group. Your completed method will look like this:

model.study("std1").run();
model.result().export().run();
model.result().evaluationGroup("eg1").run();

Since we are now writing to disk via a method, we probably also need to change some of the default security preferences. Go to the File Menu > Preferences > Security, and allow File system access for all files. This setting is shown in the screenshot below. Otherwise, you will only be able to write to the Temporary and Application Files locations, which are specified via File Menu > Preferences > Files.

A screenshot of the Preferences window opened to the Security options.
Modify the security preferences to allow a method to write to files, if needed.

Now when you run this method, the entire model is re-solved, and new data and image files are written out to disk. The last step is to do this all from the command line, without using the UI at all.

Running from the Command Line

Save this modified file, call it my_file.mph, and close out of the COMSOL Multiphysics UI. Bring up the Windows Command Prompt and navigate to the directory containing your file. Make sure that the path to the COMSOL® executables is added to the PATH environment variable, and enter the following command:

comsolbatch -inputfile my_file.mph -pname tbb,Vtot -plist "1[cm]","10[mV]"  -methodcall my_method -nosave

This will open the model and modify the value of two of the Global Parameters, due to the combination of the -pname and -plist arguments. In this case, the parameter tbb alters the bar thickness and Vtot is the applied voltage.

Our customized method will be called (due to the -methodcall argument) and will run the model and write out data. The additional optional argument -nosave means that the model file isn’t saved. For full details of this and all other optional arguments, see the section “The COMSOL Commands” in the COMSOL Multiphysics Reference Manual.

There we go: The entire process of modifying the model, and writing out data, is reduced to a single command!

What Else Can We Do?

Of course, what we’ve done here is a very minimal example to demonstrate the automation of running a model and writing outputs, but there is so much more we can do. You can put essentially any code that you want into the model method. For example, you can include code that will:

In addition, it’s worth mentioning that there is an alternative workflow that can achieve much of the same data extraction via the Job Sequences functionality (this approach does not use methods).

Finally, it’s worth remarking that you can run multiple different batch jobs in parallel at the same time, which is particularly attractive if you have a machine with a lot of memory and cores.

 

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


Comments (9)

Leave a Comment
Log In | Registration
Loading...
RD
RD
March 2, 2023

Thank you for the post. Is there a way to automate opening files one-after-another, post-processing and exporting data from each? I suppose I could do by calling a method from the command prompt for each of the files. But, none of the files have a method included in them. To avoid opening each file one-by-one and saving the method inside, is there a way I can call an external method for a given .mph file? This way, I can write a command line script to iterate through all files. Thanks again for your informative posts!

Walter Frei
Walter Frei
March 3, 2023 COMSOL Employee

Since a COMSOL file can contain essentially arbitrary data, there would not be a way to do so from the command line. You could likely do this via the LiveLink for MATLAB, but you might need to write quite a bit of code to consider all corner cases of what could possibly be in each file if you truly want to automate this.

RD
RD
March 3, 2023

Thank you.

Diotima
Diotima
July 19, 2023

This blog is extremely helpful, thank you for your excellect blog. I also have a question: Do I have to add batch into my study to enable the whole process can be run under cmd?

Walter Frei
Walter Frei
July 19, 2023 COMSOL Employee

Hello Diotima,
No, you do not need to add a batch step to the study. You can just run from the command line. As an aside about the batch sweep, you may also find this helpful: https://www.comsol.com/blogs/exploiting-hardware-parallelism-with-comsol-batch-sweeps/

Masoud Mohammadi Arzanagh
Masoud Mohammadi Arzanagh
March 12, 2024

Thanks Walter for the tutorial. I have successfully performed the methodcall in the command line on my windows and got the results. However, when I try to do the same thing on the cluster I get the error:
/******************/
/*****Error********/
/******************/
Continue is not supported by method calls.

The last line of the .sh file that I submit for this job is the following:

# COMSOL command, using -nn 8 and -nnhost 2 deduced from SLURM
./comsol batch -mpibootstrap slurm -inputfile ${INPUTFILE} -methodcall Method_M1 -outputfile ${OUTPUTFILE} -batchlog ${BATCHLOG} -alivetime 15 -recover -mpidebug 10 -recoverydir $RECOVERYDIR -tmpdir $TMPDIR

Could you please let me know what I can do to resolve the issue?

Masoud Mohammadi Arzanagh
Masoud Mohammadi Arzanagh
March 12, 2024

Thanks Walter for the tutorial. I have successfully performed the methodcall in the command line on my windows and got the results. However, when I try to do the same thing on the cluster I get the error:
“Continue is not supported by method calls.”
The last line of the .sh file that I submit for this job is the following:
# COMSOL command, using -nn 8 and -nnhost 2 deduced from SLURM
./comsol batch -mpibootstrap slurm -inputfile ${INPUTFILE} -methodcall Method_M1 -outputfile ${OUTPUTFILE} -batchlog ${BATCHLOG} -alivetime 15 -recover -mpidebug 10 -recoverydir $RECOVERYDIR -tmpdir $TMPDIR
Could you please let me know what I can do to resolve the issue?

Rachel Keatley
Rachel Keatley
March 13, 2024

Hi Masoud,

Thank you for your comment! Please go to comsol.com/support for assistance with your modeling problem.

Best regards,

Rachel

Masoud Mohammadi Arzanagh
Masoud Mohammadi Arzanagh
March 12, 2024

Hi

EXPLORE COMSOL BLOG