comsol

Introduction

The COMSOL Multiphysics engineering simulation software environment facilitates all steps in the modeling process ? defining your geometry, meshing, specifying your physics, solving, and then visualizing your results.

http://www.comsol.com/products/multiphysics/

We have version 4.3 installed on gowonda

Usage on gowonda

New Installation
module load comsol/61
(uses matlab/2022b)
OR
module load comsol/56
(uses matlab/2021a)
OR:
module load comsol/55
(It was installed with matlab/2019b)

OLD Installation (old cluster)
========================
module load comsol/4.3
OR
module load comsol/4.3-withMatlab2011a

How to Run Simulations in Batch Mode from the Command Line

Ref: https://www.comsol.com/blogs/how-to-run-simulations-in-batch-mode-from-the-command-line/

Ref: https://doc.comsol.com/5.5/doc/com.comsol.help.comsol/comsol_ref_running.29.30.html

Ref models: https://www.comsol.com/models


An example is this: comsol batch -inputfile Model1.mph -outputfile Model1_solved.mph

comsol batch -inputfile /sw/comsol/55/multiphysics/demo/builder/thermoelectric_leg.mph -outputfile thermoelectric_leg_solved.mph


Sample pbs script to run a comsol job in batch mode

#!/bin/bash
#PBS -N ComsolJobTest
#PBS -m abe
#PBS -M YourEmail@griffith.edu.au
#PBS -l select=1:ncpus=1:mem=32gb,walltime=10:50:00
#PBS -q workq
module load  comsol/61
echo "Starting job: "
cd  $PBS_O_WORKDIR
##comsol batch -inputfile Model1.mph -outputfile Model1_solved.mph
comsol batch -inputfile /sw/comsol/55/multiphysics/demo/builder/thermoelectric_leg.mph -outputfile thermoelectric_leg_solved.mph

 


Installation

Use instructions in this manual to install comsol on /sw/comsol/4.3.

Matlab 2011a was used for livelink for matlab. If this needs to be a different version of matlab, check:
livelink for matlab

http://www.comsol.no/support/knowledgebase/1113/

Solution Number: 1113
Title: COMSOL does not start with the latest MATLAB version
Platform:
Applies to: LiveLink™ for MATLAB®
Versions: 4.2
Created: April 14, 2011
Last Modified: May 27, 2011
Categories:
Keywords:
Problem Description
I have installed a new MATLAB version, but running COMSOL with MATLAB keeps opening the older version of MATLAB.

Solution
You need to manually update the MATLAB root directory path:

Use a text editor to edit the files comsolserver.ini and comsol.ini located in the COMSOL installation directory:

installdir\COMSOL42\bin\archdir

where installdir is your local COMSOL root directory and archdir is your architecture directory, for example win64. In both files, update the MATLAB root directory path, in the line starting

-Dcs.mlroot=

Please verify the COMSOL Livelink for MATLAB system requirements to ensure the compatibility between the programs.

Sample PBS script

A sample comsol file model_in.mph is here
there are lot of examples in the directory : /sw/comsol/4.3/models
e.g: /sw/comsol/4.3/models/COMSOL_Multiphysics/Fluid_Dynamics/falling_sand.mph

Interactive usage

module load comsol/4.3-withMatlab2011a
comsol 

Serial batch run

cd ~/data

cp /sw/comsol/4.3/models/COMSOL_Multiphysics/Quantum_Mechanics/conical_quantum_dot.mph .


module load comsol/4.3
comsol batch -inputfile ~/data/conical_quantum_dot.mph -outputfile conical_quantum_dot_out.mph

##model_in.mph is the name of your model. Change it to your model name. If output model name is not specified, results will be written to the model specified in input switch

Sample PBS script

#PBS -m abe
#PBS -M emailID@griffith.edu.au
#PBS -N TestRun_Comsol
#PBS -l select=1:ncpus=1:mem=7g,walltime=24:00:00
cd $PBS_O_WORKDIR
module load comsol/61
comsol batch -inputfile ~/data/conical_quantum_dot.mph -outputfile conical_quantum_dot_out.mph

Batch run- parallel

The parallel sections of Comsol are based on the parallel shared memory model. The solvers, assembly and meshing in Comol Multiphysics and the linear algebra functions in Comsol script benefit from parallelism


##!/bin/bash
#PBS -m abe
#PBS -M YourEmail@griffith.edu.au
#PBS -N ParallelComsol
#PBS -l select=1:ncpus=4:mpiprocs=4:mem=7g,walltime=24:00:00
## The number of chunks is given by the select =<NUM > above
CHUNKS=1
###$PBS_NODEFILE is a node-list file created with select and mpiprocs options by PBS
###### The number of threads available is ncpus * CHUNKS (=NPROCS)
NPROCS=4
cd $PBS_O_WORKDIR
module load comsol/61
export COMSOL_NUM_THREADS=$NPROCS
comsol batch -np $NPROCS -inputfile ~/data/conical_quantum_dot.mph -outputfile conical_quantum_dot_out.mph

Comsol in Matlab

As we use the command "comsol server" , it needs to record your username. So before you could run any batch jobs, you will have to do this once. From gowonda console, type

module load comsol/4.3-withMatlab2011a
comsol server

It will ask for the following info:

Username: s123456  <======== Use your login ID
Password:
Confirm password:
COMSOL 4.3 (Build: 151) started listening on port 2036
Use the console command 'close' to exit the application

Please type close to exit the above.

Once you have done this, you do not need to do this ever again.
use the following example to run comsol and matlab:

There are a lot .mph and matlab models located here: /sw/comsol/4.3/models/LiveLink_for_MATLAB/Tutorial_Models
e.g:
/sw/comsol/4.3/models/LiveLink_for_MATLAB/Tutorial_Models/pseudoperiodicity_llmatlab.m
/sw/comsol/4.3/models/LiveLink_for_MATLAB/Tutorial_Models/pseudoperiodicity_llmatlab.mph

Sample pbs script pbs.003

#!/bin/bash
#PBS -m abe
#PBS -M YourEmail@griffith.edu.au
#PBS -q mpi
#PBS -N TestRun_Comsol
#PBS -l select=1:ncpus=2:mpiprocs=2:mem=7g,walltime=4:00:00
## The number of nodes is given by the select =<NUM > above
CHUNKS=1
###### The number of threads available is ncpus * CHUNKS (=NPROCS)
NPROCS=2

module load comsol/61
cd $PBS_O_WORKDIR
comsol server  < /dev/null  > comsolserver1.log &
matlab -nodesktop -nosplash -r  "addpath /sw/comsol/comsol61/multiphysics/mli/, mphstart, pseudoperiodicity_llmatlab, exit"

In the above, make sure you the file named pseudoperiodicity_llmatlab.m in the directory.
(cp /sw/comsol/4.3/models/LiveLink_for_MATLAB/Tutorial_Models/pseudoperiodicity_llmatlab.m .)

If there is a need to run shared memory MP jobs, use the following script to use 8 threads.

#!/bin/bash
#PBS -m abe
#PBS -M YourEmail@griffith.edu.au
#PBS -q workq
#PBS -N TestRunComsol
#PBS -l select=1:ncpus=8:mem=7g,walltime=4:00:00
## The number of nodes is given by the select =<NUM > above
NODES=1
###### The number of threads available is ncpus * NODES (=NPROCS)
NPROCS=8

module load comsol/61
cd $PBS_O_WORKDIR
comsol server -np $NPROCS  < /dev/null  > comsolserver1.log &
matlab -nodesktop -nosplash -r  "addpath /sw/comsol/4.3/mli/, mphstart, myfile, exit"

Here is a sample myfile.m file:

cat myfile.m

% Example MATLAB script demonstrating batch operation of a script driven
% COMSOL Multiphysics model.

% Setup
mphstart
import com.comsol.model.*
import com.comsol.model.util.*

model = ModelUtil.create('Model');

% Solve the Poisson equation on the unit circle union with unit
% rectangle
geom1 = model.geom.create('geom1', 2);
geom1.feature.create('c1', 'Circle');
geom1.feature.create('r1', 'Rectangle');
geom1.run;
g = model.physics.create('g', 'GeneralFormPDE', 'geom1');
cons1 = g.feature.create('cons1', 'Constraint', 1);
cons1.selection.set([3 4 5 6 7]);
cons1.set('R','-u');
model.mesh.create('mesh1', 'geom1');
std1 = model.study.create('std1');
std1.feature.create('stat', 'Stationary');
std1.run;

% Save the result in a binary file pois.mph.
model.save('pois');

% Extract data from the model and save these in a file
u = mphinterp(model,'u','coord', [-0.5 0 0.5;0 0 0]);
save udata u

% Important to have matlab exit at the end
exit

Reference

1. http://www.comsol.com/community/forums/general/thread/26095/
2. https://rcc.its.psu.edu/resources/software/comsol/
3. Sample Model from: https://sites.google.com/site/mashavudermascope/comsol-model
4. apac.edu.au
5. http://www.comsol.com/community/forums/general/thread/9299/
6. apac.edu.au