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
Old software stack (environment modules):
COMSOL 5.1 and MATLAB 8.5
New software stack (lmod modules):
COMSOL 5.6 and MATLAB R2020b
Other combinations might also work, but they are not officially supported by COMSOL. The paths for MATLAB 8.5 and R2020b are
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.
Old software stack:
[sfux@eu-login-05 ~]$ module load legacy comsol/5.1 [sfux@eu-login-05 ~]$ comsol server Username: sfux Password: Confirm password: COMSOL 5.1 (Build: 145) Multiphysics Server started listening on port 2036 Use the console command 'close' to exit the program close [sfux@eu-login-05 ~]$
New software stack:
[sfux@eu-login-36 ~]$ module load comsol/5.6 [sfux@eu-login-36 ~]$ comsol server Username: sfux Password: Confirm password: COMSOL Multiphysics server 5.6 (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 #BSUB -W 8:00 #BSUB -R "rusage[mem=4000]" #BSUB -n 1 comsol server -silent -port 12345 -tmpdir $TMPDIR -login never & sleep 10 matlab --nodesktop -nodisplay -singleCompThread -r my_matlab_script ;
Here, the so-called LSF pragma's (#BSUB) 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.
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 #BSUB -W 8:00 #BSUB -R "rusage[mem=4000]" #BSUB -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 &
Please note that each of the matlab scripts needs to use the port that is specified for the corresponding comsol mphserver instance.
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. Therefore the MATLAB script should contain the following two lines at the beginning:
COMSOL versions from the new software stack have a slightly different path, e.g., COMSOL 5.6:
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 LSF is reached. Therefore it is important that the MATLAB script has an exit command at the end:
This will cause the COMSOL server to stop and finish the job.
Submit the job
In order to submit such a job using a runscript run.sh, you would use the following command: