Difference between revisions of "Comsol-Matlab tutorial"

From Applied Optics Wiki
Jump to: navigation, search
Line 7: Line 7:
 
Within the CM interface all Matlab functionality is unchanged. In addition to this a new set of Comsol commands are available, all of which are documented and have standard ''help'' functionality.
 
Within the CM interface all Matlab functionality is unchanged. In addition to this a new set of Comsol commands are available, all of which are documented and have standard ''help'' functionality.
  
===Building a model===
+
==Building a model==
  
 
There is no particular correct way to build your model in a CM interface, it is not all that intuitive. Personally, I have found that the best way to work with and build models is within a standard Comsol session. Once built a model can easily be manipulated within the CM interface. The internal language used by Comsol is fully compatible with Matlab script, and as you build a model Comsol saves the commands in a Matlab usable format in a file named ''myModel_history.m''. I have uploaded an example script here [[File:JcModel history.txt]].
 
There is no particular correct way to build your model in a CM interface, it is not all that intuitive. Personally, I have found that the best way to work with and build models is within a standard Comsol session. Once built a model can easily be manipulated within the CM interface. The internal language used by Comsol is fully compatible with Matlab script, and as you build a model Comsol saves the commands in a Matlab usable format in a file named ''myModel_history.m''. I have uploaded an example script here [[File:JcModel history.txt]].
  
The history file is generally split into through parts; setting up the model, running the model, and post-processing and plotting. If your model has been tweaked, run and saved multiple times then the history file can become quite convoluted. Since this file is written chronologically, the most up-to-date version of your model will be at the end.
+
===Building your initial script===
 +
 
 +
The history file is generally split into three parts; setting up the model, running the model, and post-processing and plotting. If your model has been tweaked, run and saved multiple times then the history file can become quite convoluted and there will be multiple instances of these three parts. Since this file is written chronologically, the most up-to-date version of your model will be at the end and this is the part that should be copied into a new script.
 +
 
 +
There are several actions that must be in your script to define the model, most of which will be pulled directly from the history script;
 +
* The ''Comsol version'' blurb, at the very start of the history script.
 +
* The description of the ''Geometry'', also found at the start of the script. This commands for the geometry are not reissued each time you run the model unless changed. You may have to scroll through the model to find the most recent version of the geometry description. This must be followed by the ''Analysed geometry'' commands.
 +
* A ''Constants'' section is next followed by an ''Application mode'' section. Both of these describe the mode in which Comsol runs and the constants it uses.
 +
* Various ''Boundary'' and ''Subdomain'' settings and equations which describe the variables to be kept track of, and any input (heat, pressure etc) to the model along its boundaries or surfaces.
 +
* A ''Library materials'' section which is completely optional.
 +
* A few model initialisation commands.
 +
* A mesh initialisation command.
 +
* A 'run the model' command.
 +
* Post-processing commands.
  
  
  
 
===Memory issues===
 
===Memory issues===

Revision as of 14:54, 7 January 2013

Setting up the software

The first step is installing the correct software. You will require a licensed copy of Comsol 3.5a, and a licensed copy of Matlab 2007. Both of these can be installed from the VPM repo.

A Comsol-Matlab (CM) interface is initialised by running the command comsol_35a matlab. This should start a java instance of Matlab 2007 with Comsol commands enabled. To check the link try the command flclear fem which will not work in a standard Matlab instance.

Within the CM interface all Matlab functionality is unchanged. In addition to this a new set of Comsol commands are available, all of which are documented and have standard help functionality.

Building a model

There is no particular correct way to build your model in a CM interface, it is not all that intuitive. Personally, I have found that the best way to work with and build models is within a standard Comsol session. Once built a model can easily be manipulated within the CM interface. The internal language used by Comsol is fully compatible with Matlab script, and as you build a model Comsol saves the commands in a Matlab usable format in a file named myModel_history.m. I have uploaded an example script here File:JcModel history.txt.

Building your initial script

The history file is generally split into three parts; setting up the model, running the model, and post-processing and plotting. If your model has been tweaked, run and saved multiple times then the history file can become quite convoluted and there will be multiple instances of these three parts. Since this file is written chronologically, the most up-to-date version of your model will be at the end and this is the part that should be copied into a new script.

There are several actions that must be in your script to define the model, most of which will be pulled directly from the history script;

  • The Comsol version blurb, at the very start of the history script.
  • The description of the Geometry, also found at the start of the script. This commands for the geometry are not reissued each time you run the model unless changed. You may have to scroll through the model to find the most recent version of the geometry description. This must be followed by the Analysed geometry commands.
  • A Constants section is next followed by an Application mode section. Both of these describe the mode in which Comsol runs and the constants it uses.
  • Various Boundary and Subdomain settings and equations which describe the variables to be kept track of, and any input (heat, pressure etc) to the model along its boundaries or surfaces.
  • A Library materials section which is completely optional.
  • A few model initialisation commands.
  • A mesh initialisation command.
  • A 'run the model' command.
  • Post-processing commands.


Memory issues