Return to Stuff

Running Comsol with Matlab on Cepof’s Cluster

Steps

  1. Connect via ssh -X user@143.106.11.222 -p 8022
  2. Edit comsolmatlab.qsub environment variables MATLAB_FILE and COMSOL_FILE
  3. qsub comsolmatlab.qsub

 

Example: Step index fiber Dispersion diagram

Using comsol 5.3. Files can be found on /home/maicon/template.  We use an existing mph file create on local comsol instalation. In this example we create a basic step index silicon/dopped-silicon fiber and solved the modes in a wavelenght sweep. To create the datasets, surfaces and animations relate to the post processing we made a complete study with coarse mesh and sweep for a few wavelengths.

Sweep definitions

Export definitions

 

 

After running the example we increase the number of wavelengths and the mesh refinement, save (without running) and copy to the cluster

scp -P 8022  step_index_fiber.mph user@143.106.11.222:~/

After connecting with 

ssh -X user@143.106.11.222 -p 8022

Edit comsolmatlab.qsub with the full path of matlab and comsol files
cat comsolmatlab.qsub
 #!/bin/bash
 #PBS -l nodes=1:ppn=16,walltime=10:00:00
 #PBS -N myjob
 #PBS -X -V
 #####################################################################################
 # Runs Comsol mph file from matlab to allow doing postprocessing in a cluster node. #
 # This running script just accepts one comsol run per node. Keep ppn=16!!!! #
 # For support: maicon@ifi.unicamp.br #
 #####################################################################################

export MATLAB_FILE=/home/maicon/template.m # Matlab file path
export COMSOL_FILE=/home/maicon/sandbox/step_index_fiber.mph # Comsol input file path
##############################################################################

################# Don't edit from here if you are not sure ##################
 export BASENAME_MATLAB_FILE=$(basename $MATLAB_FILE)
 export BASENAME_COMSOL_FILE=$(basename $COMSOL_FILE)
 export WORK_DIR=$HOME/tmp/$PBS_JOBID/
 export OUT_BASENAME_COMSOL_FILE=OUT_$BASENAME_COMSOL_FILE
 mkdir $WORK_DIR
 #Copy file to local disk in a processing node
 cp $MATLAB_FILE $COMSOL_FILE $WORK_DIR
 cd $WORK_DIR

#Runs inputfile and outputs result in a new mph
 comsol batch -inputfile $BASENAME_COMSOL_FILE -outputfile $OUT_BASENAME_COMSOL_FILE --batchlog batch_COMSOL.log
 #Starts a comsol server with matlab (Live Link)
 xvfb-run -a comsol server -graphics -tmpdir tmp & # calls comsol server
 sleep 20
 #Runs matlab commands in a .m file
 xvfb-run -a matlab -nodesktop -r " try, run(getenv('BASENAME_MATLAB_FILE')) ; end ; quit " # runs matlab script
 sleep 10
 #Gets all files to Home
 mv $WORK_DIR $HOME

Post Processing

The post processing is done with the matlab Live Link https://sites.ifi.unicamp.br/maicon/livelinkformatlabusersguide-4/. In this example we save data with mode’s effective index and wavelenght, also we save figures for every mode field profile.

cat template.m
 addpath('/opt/comsol53/multiphysics/mli')
 mphstart
 import com.comsol.model.*
 import com.comsol.model.util.*
 workfile = getenv('OUT_BASENAME_COMSOL_FILE')
 model = mphload(workfile)

% Start to edit here
 model.result.export('anim1').run
 model.result.export('tbl1').run

%
 exit

The results can be found at <job id> file, submitting messages are in myjob.o<job id> and myjob.e<job id>