Comsol matlab livelink
COMSOL Multiphysics can be integrated with MATLAB to extend its modeling with scripting programming in the MATLAB environment. LiveLink for MATLAB allows to utilize the full power of MATLAB and its toolboxes in preprocessing, model manipulation, and postprocessing.
In order to prepare your setup for LiveLink jobs, you first need to make sure that COMSOL knows, which MATLAB version it is supposed to use:
- Start up the GUI of COMSOL on one of the login node of the cluster.
- Go to the Options menu in the top of the window and choose preferences
- In the preferences window, there is a navigation section in the left part. Choose the last entry LiveLink products
- Make sure that the MATLAB path is set to the Matlab version you would like to use together with COMSOL
As the COMSOL preferences are saved, this step only needs to be done the first time. Please be aware that there are certain combinations of COMSOL and MATLAB, which are officially supported. For instance
New software stack (lmod modules):
COMSOL 6.0 and MATLAB R2021a
Other combinations might also work, but they are not officially supported by COMSOL. The path for MATLAB R2021a is
You can check supported combinations on
Afterwards, you need to start the COMSOL server on one of the login nodes in interactive mode in order to specify a username and password. The default settings do not require you to ever enter this password again if you do not enable the option for this. Therefore you can specify anything for the username and password.
New software stack:
[sfux@eu-login-36 ~]$ module load comsol/6.0 [sfux@eu-login-36 ~]$ comsol server Username: sfux Password: Confirm password: COMSOL Multiphysics server 6.0 (Build: 280) started listening on port 2036 Use the console command 'close' to exit the program close [sfux@eu-login-36 ~]$
After specifying the username and password, your setup is ready for COMSOL/MATLAB LiveLink jobs.
Runscript for LiveLink jobs
In a LiveLink job, you first need to start the COMSOL server and afterwards MATLAB. The best solution for running such a job is to have a runscript (for instance run.sh) that takes care of the multiple steps of the workflow.
run.sh could look like:
#!/bin/bash #SBATCH --time=1:00:00 #SBATCH --mem-per-cpu=3000 #SBATCH -n 1 comsol server -silent -port 12345 -tmpdir $TMPDIR -login never & sleep 10 matlab --nodesktop -nodisplay -singleCompThread -r my_matlab_script; wait
Here, the so-called SLURM pragma's (#SBATCH) are used to specify the resource requirements of the job inside the shell script. Then the COMSOL server is started in the background. With the -port you can specify the port that is used by the COMSOL server to communicate with MATLAB. After starting up the COMSOL server, we recommend to run a sleep command, as the server needs some time before it is ready to communicate with MATLAB. In the last step, MATLAB is started.
The MATLAB script that is used in the LiveLink job also needs to contain certain information. First of all it needs to know where to find COMSOL and through which port the communication should take place. You can find a minimal hello wolrd example below:
addpath('/cluster/apps/nss/comsol/6.0/x86_64/mli'); mphstart(12345); disp("hello world") exit;
Please make sure that you use the correct path to the COMSOL version that you would like to use in the LiveLink job and that the port number is the same as in the runscript that is described above.
Since the COMSOL server is running in the background, it will continue to run until the run time limit that was specified to Slurm is reached. Therefore it is important that the MATLAB script has an exit command at the end, as this will cause the COMSOL server to stop and finish the job.
Runscript for multiple LiveLink jobs using the mphserver
In this example, three comsol mphservers are started, using different ports. If multiple comsol instances are running on the same compute node, only 1 license will be checked out. This setup therefore helps to reduce the number of licenses used when running multiple simulations.
#!/bin/bash #SBATCH --time=1:00:00 #SBATCH --mem-per-cpu=3000 #SBATCH -n 12 comsol mphserver -np 4 -silent -port 2036 -tmpdir $TMPDIR -login never & comsol mphserver -np 4 -silent -port 2037 -tmpdir $TMPDIR -login never & comsol mphserver -np 4 -silent -port 2038 -tmpdir $TMPDIR -login never & sleep 15 cd stage1 matlab -nodesktop -nosplash -singleCompThread -r my_matlab_script1 -logfile log < /dev/null & cd .. cd stage2 matlab -nodesktop -nosplash -singleCompThread -r my_matlab_script2 -logfile log < /dev/null & cd .. cd stage3 matlab -nodesktop -nosplash -singleCompThread -r my_matlab_script3 -logfile log < /dev/null & wait
Please note that each of the matlab scripts needs to use the port that is specified for the corresponding comsol mphserver instance.
Submit the job
In order to submit such a job using a runscript run.sh, you would use the following command:
sbatch < runscript