Discussion Forum

Tip: Batch calculations with Matlab + Comsol 4.0a

Topics: MATLAB, 4.0a, 4.1, 4.2a

Thread index  |  Previous thread  |  Next thread  |  Start a new discussion

RSS FeedRSS feed   |   Email notificationsTurn on email notifications   |   11 Replies   Last post: May 11, 2012 7:56pm UTC
Charles Doiron

Charles Doiron

September 9, 2010 1:26pm UTC

Tip: Batch calculations with Matlab + Comsol 4.0a

Hi,

I just wanted to share my experience regarding batch-type (i.e., non-interactive) calculations using comsol 4.0a and the matlab livelink. I lost quite some time trying to run a comsol matlab script (.m file) on a linux cluster, and I thought some people might be interested in hearing the workaround I found.

When working with the cluster, I have two major constraints:
1. I do not have any kind of display (X) support,
2. I have to submit the calculation via a simple bash script.


In 3.5a, running a .m file in batch mode was very easy (At the very least, it was nicely documented : http://www.comsol.com/support/knowledgebase/980/), One would use:

comsol matlab -ml -nodesktop -ml -nosplash -mlr "myscript, exit"

In comsol 4.0a, using something along the lines of

comsol server matlab -nodesktop -nosplash -r myfile

does not lead to similar behavior. First, an xterm is spawned, and the 'nodesktop' matlab runs in this new terminal. The '-r myfile' option is ignored, and matlab does not run my .m file on startup.

To work around the fact that 'comsol server matlab' spawns an xterm, I was told by comsol support to do the following:

1. Start the comsol server
2. Start matlab, and connect to the server using mphstart



To be able to do this within one bash script, I basically had to launch the server in the background. However, I could not use

comsol server &
matlab -nodesktop -nosplash -r myfile

as the server process would stop before matlab could connect to it whenever it was launched as a background process. By using instead

comsol server </dev/null > /dev/null &
matlab -nodesktop -nosplash -r myfile

instead of simply "comsol server &" then the server process is not stopped and you can run matlab, connect to comsol server, and run your .m script, without any user-intervention nor need for X support. Also, the comsol server exits when running ModelUtil.disconnect in Matlab, so I don't leave any process on the compute node..

Hoping this might be useful for someone ;)
By the way, if anyone has a better (cleaner?) way of achieving the same thing, I'd be happy to hear it!

Charles



Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Ivar Kjelberg

Ivar Kjelberg
Moderator

September 10, 2010 9:06am UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Hi

thanks for the tips, I start too to have large models I would like to run batch over the night/week-end so this will be usefull.

Hope COMSOL will make it cleaner/easier in next release

--
Good luck
Ivar

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

sandy

sandy

September 14, 2010 10:18am UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

This is a very good tip, i just need it to solve my problem, but as i tried as you suggested.

the command
$comsol server < /dev/null > /dev/null &
does work, while after running this command, you can see there is a 'comsollauncher' process
running in the background, also consuming quite a lot of cpu, though nothing is running at all.
then i run the command
$matlab -nodesktop -nosplash -r file
it gives me an error like
'??? Undefined function or variable 'ModelUtil'.'

Do you guys know what's wrong with it?

Thanks in advance.

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

MOhmesICx

MOhmesICx

September 16, 2010 6:01pm UTC in response to sandy

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Make sure that you have your COMSOL40a\mli directory added to the Matlab path.

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Orad Reshef

Orad Reshef

December 7, 2010 2:14am UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

This is a great tip!

Perhaps one step better, for people running OSX - I've saved the whole thing into it's own symbolic link.

Navigate to /usr/bin, create a file with the text editor of your choice (I used nano, for simplicity's sake). In the file, put in:

#!/bin/bash
comsol server </dev/null> /dev/null &
matlab -nodesktop -nosplash -r 'mphstart'

If it gives you trouble, you might have to use a sudo command to get it to create (as in sudo nano shortcutname).

Finally, you need to give everyone access to run this program, so back in the /usr/bin directory,

chmod +x /usr/bin/shortcutname

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Shakeeb Bin Hasan

Shakeeb Bin Hasan

January 18, 2011 4:43pm UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

I won't say it is a cleaner way, but it is more 'transparent' so to say for many people to start comsol's server like this:

comsol server > server.txt &

This way whatever output comsol server wants to spew will be dumped in server.txt file. And honestly, sometime during debugging it is useful to know what it has to say :)

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Tanja  Etzelstorfer

Tanja Etzelstorfer

April 6, 2011 12:39pm UTC in response to Shakeeb Bin Hasan

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Good tip, that also works for Comsol 4.1

But nevertheless: very inconvenient to be honest..
Hopefully there is a better solution in the next version.

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

br.rabbit

br.rabbit

May 4, 2011 4:22am UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Thank you very much, Charles!

I've been trying to run Comsol 4.0a in batch mode on our cluster for like a year (periodically dropping it and coming back to it again and involving our cluster and comsol tech support with no results). I've just tested what you suggested - your method works nicely! Finally!!!

Sincerely,
Alex.

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Muhammad Mohsin Rehman

Muhammad Mohsin Rehman

October 26, 2011 3:29am UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Hi Charles,

Do you have any idea of running batch process on windows?
I have two files and I want to run them one by one in the way that results of one should be used as initial conditions for the other, is there any way of doing it?

Thanks in advance for any of your help!

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Charles Doiron

Charles Doiron

October 26, 2011 12:05pm UTC in response to Muhammad Mohsin Rehman

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Hi Muhammad,

Sorry, I cannot really help you with running batch calculations under Windows. I mainly use my employer-issued Windows laptop as a glorified email client, and therefore don't have much experience with Comsol under Windows.

Regarding the second part of your question; could you not simply merge the two files into one, using the second file as a second study step? In that case it would be straightforward to setup the calculation in such a way that the results of the first calculation can be used as initial conditions for the second calculation..

Good luck,
Charles

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Muhammad Mohsin Rehman

Muhammad Mohsin Rehman

October 26, 2011 1:19pm UTC in response to Charles Doiron

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

Thanks a lot Charles for your feedback!

I have defined the second study step in the same file but could not find any option in the second study to set the results of first study as initial values.(Please see the attached image for the options)

Do you mean that I need to define the whole model again?

Can you please elaborate what do you mean by setting the second file as second study step?

Thanks in advance!

Attachments:   study2.JPG  

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse

Martin Edwards

Martin Edwards

May 11, 2012 7:56pm UTC in response to Muhammad Mohsin Rehman

Re: Tip: Batch calculations with Matlab + Comsol 4.0a

The above worked for me on 4.2a. Useful tips, many thanks. Saved me a wasting an afternoon.

Reply  |  Reply with Quote  |  Send private message  |  Report Abuse


Rules and guidelines