Catalog
  1. 1. Run COMSOL on server
  2. 2. Run MATLAB on server
  3. 3. Run COMSOL live link with MATLAB on server
How to use COMSOL, MATLAB and COMSOL livelink with MATLAB on Server

[TOC]

This note is to show the way of using MATLAB, COMSOL,COMSOL Livelink With MATLAB on our server (ubuntu). We hope that we can run them with bash code in background and after we logout or exit the server the simulations will continue. We also need to know the simulation progress from a log file. These needs can all be satisfied only if you write proper bash code.

Run COMSOL on server

If you want to run COMSOL on server, you can use the command of the following form

1
nohup comsol batch -inputfile <inputfilename> -outputfile <outputfilename> -batchlog <logfilename> -np <number of cores> &

above command shows the smallest example and the “< >” isn’t included in the actual terminal command. The form nohup <command> & is used to execute the command in the background even if we logout from the server. To avoid confusion, I give a more specific example, If we want to simulate the file “test.mph”, then the command should be as follows

1
nohup comsol batch -inputfile test.mph -outputfile test_out.mph -batchlog test.log -np 30 &

In above example we use 30 cores (set with “-np 30”)to simulate our mph file. If we have many mph files to simulate, we can write a bash code to arrange our simulation works. We create a file with the name “comsolscript.sh” with the following content

1
#! /bin/bash
2
nohup comsol batch -inputfile test1.mph -outputfile test1_out.mph -batchlog test1.log -np 30 &&
3
nohup comsol batch -inputfile test2.mph -outputfile test2_out.mph -batchlog test2.log -np 30 &&
4
nohup comsol batch -inputfile test3.mph -outputfile test3_out.mph -batchlog test3.log -np 30 &

Above code will simulate the file “test1.mph”, “test2.mph”, “test3.mph” respectively. The “&&” at the end of the line will let the server to execute the next command only when this command complete. if you use “&” then all the mission will simultaneously execute. You need upload the “comsolscript.sh” file to the correct folder and make it executable

1
chmod +x ./comsolscript.sh

then in the corresponding folder type

1
./comsolscript.sh

The simulation will begin and you can see the progress from the log file by typing vi test.log.

If you write the bash code on windows then upload the file to the server, when you try to run the bash file there may be error: “/bin/bash^M:bad interpreter:No such file or directory“, this is because of the difference of coding in Unix and Windows. You can use the following command in the corresponding folder to fix it

1
sed -i -e 's/\r$//' ./comsolscrpt.sh

if you use vi on the server to create the bash file, this error can also be avoided.

Run MATLAB on server

Before using the MATLAB on server, you need make sure that the matlab is in the system enviroment path. The method of adding the MATLAB path to the system enviroemnt can be found from my another note Ubuntu安装COMSOL,MATLAB以及FDTD Solutions.

If you want to run matlab without displaying the desktop, you can type

1
matlab -nodekstop -nosplash -nojvm

If you want to run a file in the background, you can type the following command

1
nohup matlab -nodesktop -nosplash -nojvm <test.m> progress.txt &

above code run the “test.m” in the background and save the screen display into the file “progress.txt”, so we can know the progress from the file. “-nojvm” is used to hide the plot progress if you don’t want to see the plot during the simulation. When the simulation meets some problems, there will be a “nohup.out” file to show the error information.

A better choice is to use a bash file “matlabscript.sh” to run the corresponding MATLAB file.

1
#! /bin/bash
2
matlab -nodesktop -nosplash -nojvm <test1.m;test2.test3.m;exit;> progress.txt

Above bash file doesn’t use the “nohup command &” and it will not run in the background. If you want to run in the background, you can type

1
nohup ./matlabscript.sh &

The “exit” is important and if not after the simulation the MATLAB will be still alive in the background, then you need kill the MATLAB by typing kill <pid of MATLAB progress>.

The usage of COMSOL live link with MATLAB can be found on the installation folder of COMSOL, the detailed usage is in the file “LiveLinkForMATLABUsersGuide.pdf”.

If you can use the graphics window, then you can run a MATLAB file “test.m” to run a COMSOL file by typing

1
comsol mphserver matlab test

You can also run the script in batch without the MATLAB desktop and the MATLAB splash screen. Enter this command:

1
comsol mphserver matlab test -nodesktop -mlnosplash

However, if you want to run the script in the background, you can not just simply add a “nohup+command+&”, I don’t know the reason. From the “LiveLinkForMATLABUsersGuide.pdf”, we can see the following words

To connect COMSOL with a MATLAB terminal requires that xterm is installed on the machine. If this is not the case as it might be for a computation COMSOL server, a workaround is to connect manually MATLAB to a COMSOL server with the function mphstart.

After trying for a while I finally know how to run the COMSOL livelink with MATLAB in the background on server. We need to create four files named “matlab_exe.m”,”test.m”,”comsolscript.sh”,”test.mph”.

  • “test.mph” is the COMSOL file you want to simulate.
  • “test.m” is the file to run “test.mph” from MATLAB.
  • “matlab_exe.m” is used to link the MATLAB with COMSOL mphserver and run the “test.m” file and finally exit MATLAB.
  • “comsolscript.sh” is the bash code to open the COMSOL mphserver and run the file “matlab_exe.m”
    the following is the detailed code.

For “matlab_exe.m”

1
% This code is to connect to the COMSOL livelink with MATLAB server
2
3
% add the livelink path to the server for comsol5.4
4
addpath('/usr/local/comsol54/multiphysics/mli/'); 
5
6
% connect to the server, the default port number is 2036 and then 2037 ...
7
mphstart(2036)
8
9
% the MATLAB file you want to run
10
test;
11
12
% exit the program
13
exit;

For “comsolscript.sh”

1
#! /bin/bash
2
# if the run the file and getting the error "/bin/bash^M:bad interpreter:No such file or directory",you can use 
3
# the following command in the corresponding cluster to fix it
4
# sed -i -e 's/\r$//' scriptname.sh
5
6
7
# this is to run the MATLAB file in script
8
# First start the COMSOL livelink with MATLAB for 5.4
9
# use "np" to define the number of cores you will use
10
comsol54 mphserver -np 28 -silent &
11
12
# run the MATLAB file
13
# The "screenout.txt" will store the screen output for MATLAB
14
matlab -nodesktop -nosplash  <matlab_exe.m> screenout.txt

For “test.m”

1
% save the COMSOLcprogress file as "COMSOL_Progress.txt" during simulations
2
clear;clc;
3
import com.comsol.model.*
4
import com.comsol.model.util.*
5
ModelUtil.showProgress('COMSOL_Progress.txt');
6
7
% other command
8
%........

The note ends here. Thanks for your reading, if you have any questions please contact me
knifelees3@gmail.com

Author: Knifelee
Link: https://knifelees3.github.io/2019/07/06/A_En_How_To_Use_COMSOL_LiveLink_With_MATLAB/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶

Comment