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 COMSOL 5.1 and MATLAB 8.5).
Other combinations might also work, but they are not officially supported by COMSOL. The path for MATLAB 8.5 is
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.
[leonhard@euler01 ~]$ module load legacy comsol/5.1 [leonhard@euler01 ~]$ comsol server Username: leonhard 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 [leonhard@euler01 ~]$
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 & sleep 10 matlab -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.
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:
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: