Monthly Archives: September 2010

Porous Media Solution


Open up COMSOL Multiphysics 4.0a (Classkit License) and we will have a screen like this:

Model Selection

If you need a general procedure for how to open models in COMSOL head over to the previous post . For this particular problem let’s use the Chemical Species Transport and the sub-model Transport in Porous Media (chpm). Select Steady State and finish your model by clicking on the flag. You could do multiple physics to solve this problem… and in fact Matthew Bible and Isaac Keebler have a very nice solution using the Porous Media and the Dilute Species Transport. Their solution is cool but I’m going to keep it simple.

Building the Model

Refer to the previous post and see how to generate geometries… here we will be making three rectangular shapes to represent our problem. With three rectangles that are 0.2 m wide and 0.8 m tall. Although maybe not required I would union the geometries.  Union by selecting Geometry in the Model Builder and selecting all the geometries under your Geometry heading and then pressing the Form Union item on the list. Follow your nose to get the union. Here’s the geometry:

Boundary Conditions

Basically you want two concentration conditions on the vertical boundaries at x=0 and x=0.6, open boundaries at the top and bottom of your geometry in you second rectangle (that’s the lines y = 0 and y = 0.8 where x \in (0.2, 0.4), a high diffusivity in the middle of your geometry (~1), a large porosity in your middle geometry (~1), and lower diffusivity and porosity on either side of your middle region (I used the defaults).

That’s it, solve your model. Solution screen shot, maybe soon.



An important thing to remember about COMOSL 4.0 is that this interface employs the use of right clicks as shortcuts. One of the big changes from the old COMSOL gui to the new one is the suppressed menus; the upshot is a simpler looking interface… though sometimes finding stuff can be difficult. That said just don’t forget to look for menus by right clicking.

Start Up

COMSOL Multiphysics 4.0a (Classkit License) is the specific breed of software we will be using. After entering the program there should be a screen like this:

There’s a couple of things to get used to here but mainly just focus on the left hand menu bar… we are going to select a geometry there. The example problem we have is the handout given on Tuesday. If you don’t have it, just keep reading.

Model Selection

We are going to start off with a 2D model with three domains (regions). Here are the steps:

  1. COMSOL Multiphysics 4.0a (Classkit License) startup
  2. Left hand column, under Model Wizard, go ahead and Select Space Dimension (click on 2D and press the advance arrow at the top of the Model Wizard bar)
  3. Left hand column, under Model Wizard, let’s pick a physics… under Chemical Species Transport select Transport of Diluted Species. Select the type and then click the advance arrow.
  4. Left hand column, under Model Wizard, if you will Select Study Type… that’d be stationary. Try to suppress the urge to select time dependent, we can be men later. Click the flag to finish your model selection.
  5. Now left hand column, select the Model Builder tab. You’re done!

Here’s your screen if you have followed the steps:


The subject I kicked my big brother’s butt in. Anyway let the first thing that you notice on your screen (or the second) be that along the top bar there are a good number of icons for building a certain type of geometry. You can use those, but the old COMSOL used to have an interface where you could input your dimensions of the geometry by points and side lengths, radii… basically a less visual but often more precise method. That still exists in COMSOL but it’s under the right click menus. Try this out:

  1. Left hand column, under Model Builder, left click once on Geometry then right click on Geometry to bring up the right click menu.
  2. Select rectangle and aha! there is that menu that let us build stuff.
  3. Put in values for your rectangle: height = .8 and width = .2.
  4. Press enter and… nothing happens. You need to Build All, it’s under that left hand column near the top… it looks like a building.

Boundary Conditions

Let’s have a short talk about this stuff… like what was said before, the right click menus are important here. We’ll do a sample and then suppose that the rest are similar. Check it out:

  1. Left hand column, under Model Builder, left click Transport of Diluted Species (chds) then right click the highlighted selection and a list of BCs come up.
  2. Try Concentration and check the Species C checkbox. Enter the concentration the desired concentration.
  3. Go into your Graphics window and select a boundary, then click the + button. If you have done it correctly your boundary number shows up in your list.

Here is where you can set certain physical properties like bulk density, porosity… etc. Just try to find the appropriate condition where you would find your value and it’s likely there. Do not forget to press the + or your changes will not take place.


Let’s mesh:

  1. Left hand column, under Model Builder, let’s right click the Mesh category
  2. Select your mesh
  3. Top right corner of the left hand column, Build All… it’s the building… haha.

Now your meshed. Let’s solve this puppy:

  1. Left hand column, under Model Builder, right click the category for Study.
  2. Compute

That’s it… I think there’s a COMSOL help manual somewhere crying tears of blood and shaking it’s head at me right now. Toododoloose.

Matlab Differential Equations


These differential equations can be solved analytically but practice with numerical solvers is kind of a cool thing.

Anyway, the syntax for the ode solver is pretty straight forward and standard for the different solvers:

ode23 (@fun, [t_0 t_1], initial, options)

with the inputs, in order from left to right, being a Matlab function, time interval, initial conditions and options (which is a place for specifying precision and some other stuff). What really needs to be emphasized is that this functional input needs to be a Matlab function stored in the current working directory as a .m file that has the same name as the Matlab function. Matlab has kind of a slick way of brushing that under the carpet in their help file but the point is that your DE(s) need(s) to be stored that way; meaning what? You have to use a combination of the command line and .m file editor to solve your differential equation. An example function:

%Matlab function f.m
function dy = f(t,y)
k1 = 2;
k2 = .5;
dy = zeros(2,1);
dy(1) = -k1*y(1);
dy(2) = -k2*y(2) + k1*y(1);

Here we are solving \frac{dy_1}{dt} = -k_1 y_1 and \frac{dy_2}{dt} = -k_2 y_2 + k_1 y_1. Save this as f.m. Then solve the system at the command prompt:

ode23(@f, [0 20], [1 0])

A quick check, if this is not working have you:

  1. opened and saved your function containing your DE(s) as f.m
  2. saved this file in your current directory? If you don’t know what this is type cd at the Matlab command prompt.
  3. been prompted to “Add to path”, “Make current path”, etc? Adding to the path is easiest.
  4. any variables that you were using previous to this that would have the names “y” , “t”, “k1” or “k2”? Try clear variables at the command prompt.
  5. copied and pasted the code. Sometimes it’s easy to small stuff.

Probably the oddest part of this form is the ‘@’ call. This is used for designating a function, so if our differential equation is represented by function fun then @fun tells Matlab to use the saved function fun. This is probably the way that Matlab separates a variable and function by syntax. There’s more to be said about this, if your interested skip down the page to Functions.


Functions in Matlab must be saved as .m files. This is a unique feature in Matlab and is mostly a consequence of the fact that Matlab is hardcore numerical. There are a couple of different kinds of functions; specifically I will only talk about anonymous and “regular” functions (sorry about that, I really did try to find out what Matlab called these things… I just didn’t see anything about it).

Regular functions are those which must be saved in .m files… which suggests that there are some functions that don’t have to be saved in .m files. Ok, so I lied… but for good reason: in ALMOST all cases functions must be saved as .m files and while it is inaccurate  to say ALL functions must be saved as .m files, there are VERY few cases where anonymous functions can be used. However, anonymous functions are a slick way to get something done quickly. For the differential equation:

\frac{dy_1}{dt} = -k*y_1

We would have to build a .m file for something very very simple. The .m file:

%Matlab function simple.m
function dy = simple(t,y)
k = 1;
dy = -k*y

The fact that you would have to generate this .m file for something so simple is kind of frustrating, huh? If you’re not convinced take a look at how much work you would have to go through to try and change your k for say 50 or 100 values of k. That’s a lot of clicks. Wouldn’t be great if there was something that allowed you to do all of this from the command prompt without the accessory .m file? Anonymous functions will let you. In the command prompt:

%command line
k = 1;
dy = @(t,y) -k*y;
ode23(dy, [0 20], 1)

Ok, that wasn’t that great. Let’s do something better:

%command line
for k=.5:.01:1.5
dy = @(t,y) -k*y;
ode23(dy, [0 20], 1)
hold on

Here’s what I got:

Ha… cool. That’s it.