lynda.com

How do I debug my code when it’s broken ?

August 28th, 2009 by windmaomao

sadly, the answer is, I don’t know either.

debugging is an art, fortunately art can be trained to certain level as well.

so here’s some general guidance.

Repeat the problem
Make sure the error you saw isn’t random, maybe it sounds funny. But I see random errors, nasty ones that jump around which is very hard to debug. It also gives hints to where the problem might be. Since computer is consistent, the “random” error might due to memory leak or random designed by your code.

So setup a scene, make sure you can repeat the error whenever you want, I know you want to jump directly to the problem and fix it and don’t want to see it again. But please be patient to setup the crime scene first.

See what happened
It’s not strict forward as it sounds, you saw a message on the screen. It doesn’t exactly tell you what happened, nor the location, not alone why it happened. To find out why, you need to be a detective. Print out everything that is close to the problem. If all the information making sense, then print more info around the problem, and start to trace back. If luckily you find out something isn’t making sense, congratulations, you’re close to the answer :)

In simulation, to see whatever you want adds some complexity due to the size of the problem and the amount of the data. Ex. make sure you can visualize your matrix or customized data-set when problem happens; and sometimes if you don’t save all the time, make sure you can repeat to the exact time before the problem.

In general, you want to have all the tools to see whatever you want around the crime scene, the more detailed key information you can access, the easier the job is, like in the movie.

Log events and important informations
When everything goes fine, nobody want to waste time on old records. But things normally don’t turn out to be what you want, if you have a log, it’ll make your life much easier, since you can trace back what you’ve done before.

Log and document your coding, make comments on what you did on this piece of code. If things get wrong tomorrow, you might go to the change you made right away if things were good before the change.

Log your events when code starts to run, let your code write some messages telling you what it’s doing, and some key parameters it generates when it runs, so you know it’s working. If code break at certain point, you can look at the file it generates and start at approximate key algorithm location.

Imagination and logic
Please do use your imagination when debugging, you might get to the point quicker if you’re lucky, some bugs can be very hard and nasty to dig out if you follow logic sense too strictly.

Btw, did I mention, debug is an art, well, your talents (imagination and luckiness) can be good here, and hard working (log and documentation) does make life a bit consistent ;)

Notes : I personally don’t use debugger these days, because debugger version is too slow to run, and release version doesn’t contain debugger info. And normally debugger scales me, I don’t know what to do when I’m in them. I assume, only hacker use debugger, and I’m no hacker, I do want to be one of them though.

VN:F [1.6.4_902]
Rating: 8.0/10 (1 vote cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

Access mac workstation for simulation

August 27th, 2009 by windmaomao

Couple of ways to access our powerful mac pro workstation, I’ll outline some possibilities and some recommendations. Basically there’re two of them when you are away from these computers.

One is to remote login them and use your own computer as a command center, the output of the workstation will get back to you through the terminal window, if x11 window is enabled, you’ll see a beautiful graph showing on your screen. The computation is all on the workstation.

Another one is less obvious, you mount the workstation hard drive on your own computer, and do the work as you do normally. When accessing data, it’ll be sent to your computer from workstation transparently. The computation is all on your own machine, but all the software you installed on your own machine will shine.

You can see, both ways has it’s good and bad. It’s up to you to find the best combination to do the work at hand. For some of us, the following combination might work.

1) Remote login to any of our workstation through xterm. Use “matlab -nojvm” to launch matlab for post-processing, 2D, and minimum 3D graphing job. There’s no good 3D support and window control for matlab in OSX through x11 for now.

2) Another way I heard is that you copy data back to your own machine and launch matlab there. If that’s the case, you could try mount the harddrive on your machine, so it’ll save you lots of time moving back and forth the data file.

3) For trivial task, you can do either of the way mentioned above, personally I found the mounting works on my mac laptop, since I have my workflow unstopped.

Hope the above helps, if you want to do mount on mac, try this post for mounting any internet hard drive on your computer. If you have any question, just come and ask, thanks.

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

How to extend your work with others ?

August 11th, 2009 by windmaomao

Last night I was asked for advice about how to combine your work with others’ in coding. Interestingly enough, I think this can be a general question in day-to-day work, how to borrow other people’s work, how do I extend them, how can I include some feature on top of other’s work?

Well, the best way I can think of right now is the following.

1) leave other’s contribution intact. It’s very easy to steal the idea, but trust me, it’s much harder to support the work, especially the work not done by you. So the best way to incorporate other’s work is first to credit them (or pay them) and leave their work for their own goods.

2) leave your work intact. Same thing as above, only this time applying to yourself.

3) build a bridge between them. Same thing as above, only this time applying to the bridge (or interface) or the connection. You need to credit the connections these days, while sometimes over-charged, they can be very useful and efficient.

Ok, essentially three component, each of them serve only one purpose. Actually there’re still one component missing, the final user, who’s gonna use the work through the connection which binds two unique skills.

This is how the work is extended in the modern life, I think. So coding is no exception. The rule of thumb is to make each component as independent as possible. The more you can achieve this, the better it can turn out to be ;)

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

Include png jpeg picture in Texshop on mac latex

July 28th, 2009 by windmaomao

it really took me a while to figure out how to include pictures in the latex, after trial and error and some online helper, it turns out to be very easy.

add the following in the beginning of the document
\usepackage{graphicx}

and whenever you need to include picture, add
\includegraphics[scale=0.5]{myfig}

make sue two things, 1) the myfig file need to be in the same path of the tex file; 2) no extension of the file should be appended, ex. png, jpg. I don’t know why, but if you add the extension, the texshop gave an fatal error. And the documentation of the texshop actually doesn’t have extension included either when it refers to include pdf and eps. If you get a missing bounding box error, please don’t go investigate bounding box, it’s not related as far as I see.

You can adjust the options in the bracket [], a small documentation you can found from here.

hope the above helps :)

VN:F [1.6.4_902]
Rating: 7.0/10 (4 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

Hack or do extension, I

July 9th, 2009 by windmaomao

If we live in perfect world, nobody want to hack anything, ’cause you can just take it :)

In programming, sometimes you need to borrow other people’s library, either you don’t like the design or you want to add something on top of that, you are facing one million dollar question ?

hack ? or do extension.

well, let me explain what’s hacking here, you open the library, go to line 2375, write your stuff, write your comment if you are nice person. You compile the code, it worked.

it takes maybe 10 seconds to modify it after you read the manual (if any) for hours to days, it solves the problem quick. The only problem here is, next time if you want to do something else, it’s difficult for you to track what’s changed and reuse the change you made. As you can see here, it’s not a formal extension, it’s just a quick hack.

For consistency and extensibility, you probably want to think about writing an extension, especially when you have another library at hand, one good approach is to write something connecting both without hurting any of these two libraries. What we do usually is to write another container class holding both. But in the un-perfect world, what if one of the library isn’t OOPed, or not written independently?

Wow, another million dollar question ?

still, you can make it, the sacrifice here is you have to modify one of the library, but still there’s nice way to deal with it.

the answer is to write an bridge, an interface wrapping most of the functionalities of one library, and this wrapper gets called by another library. Why not just call one library inside another library? Well, for lots of reason, what I can think of right now is, one library might not be bored to be with another library most of time, so the easy way to maintain their relationship is to create a buffer to cool down the tension.

ok, well said, here’s an example in fortran (one of the worst language for OOP), I want to use Steve’s package, how do I extend it with my code, ex. XXX. ok, steve’s code is well organized, since it’s not OOPed, let’s just leave it as a black box.

so your code should look something like this,

module Cell_System
 
	type :: cells
	end type cells
 
contains
 
	subroutine cellsys_create(csys)
               type(cells) :: csys
 
	end subroutine cellsys_create	
 
	subroutine cellsys_destroy(csys)
               type(cells) :: csys
 
	end subroutine cellsys_destroy
 
       !! lots of functionalities about cells here
	subroutine cellsys_do_something_1(csys)
               type(cells) :: csys
 
	end subroutine cellsys_do_something_1
 
	subroutine cellsys_do_something_2(csys)
               type(cells) :: csys
 
	end subroutine cellsys_do_something_2
 
end module Cell_System

let’s make a extension for our problem now, the interface should look something like this

module Problem_Cell
	use Cell_System
	implicit none
 
	type :: cell_app
		type(cells) :: cell_sys
	end type cell_app
 
        !! lots of problem related parameters here
 
contains
 
	subroutine cprob_create(capp)
               type(cell_app) :: capp
 
	end subroutine cprob_create
 
	subroutine cprob_destroy(capp)
               type(cell_app) :: capp
 
	end subroutine cprob_destroy
 
       !! lots of functionalities related to problem here which uses cell system, ex.
	subroutine cprob_do_something_with_problem(capp)
               type(cell_app) :: capp
 
              call  cellsys_do_something_1(capp%cell_sys)
              call  cellsys_do_something_2(capp%cell_sys)
 
	end subroutine cprob_do_something_with_problem
 
 
end module Problem_Cell

It’s pretty similar to the library code, yes, but it’s related to problem and tuned to problem domain. ok, now in the problem.f90, you call write,

MODULE Problem
 
	! define cell system object
	type(cell_app) :: capp
 
contains
 
	subroutine SetProb()
 
		call cprob_create(capp,domain)
 
	end subroutine SetProb
 
	subroutine AfterRun()
 
		call cprob_destroy(capp)
 
	end subroutine AfterRun
 
	!! lots of functionalities referred to cell system calls
	subroutine Step()
 
		call cprob_do_something_with_problem(capp)
 
	end subroutine Step
 
 
end module Problem

I hope you get the idea, take a look at all three codes, they’re quite similar, you build two houses, instead of drilling holes on both houses to connect them, you build a small house in-between to keep the integrity of both. Well in this case, we have to arrange the furniture of one of the house.

FYI, one of the house (cell system)) can be built multiple times while one of the house (steve’s library) can only have one copy. If both are designed to be multiple times allowed, we could do another approach that builds a bigger house containing both while keeping integrity of both so that each can be maintained and improved separately. If you interested, the fundamental idea behind this is information hiding and isolation, one of the topic in OOP (object oriented programming), here I used it in fortran.

Hope the above isn’t too overwhelming for non-computer science major people ;)

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

FJVis extension I, turn adaptive into uniform

July 1st, 2009 by windmaomao

What if you want to compare your adaptive result with analytic solution to make sure you’re on the right track ?

It’s easy. Step 1, Load your data first as always,

[time,fd]=tmmpost(nframe);

Step 2, create a uniform grid that you want to convert to, here’s an 2D example.

gd={-1:0.01:1;-1:0.01:1};

Step 3, call fjuniform to convert,

fdu=fjuniform(fd,gd);

Type fd, and fdu to take a look

1
2
3
4
5
6
fd{1}
    [34x34 double]    {2x1 cell}    [0]
    [12x10 double]    {2x1 cell}    [1]
    [10x12 double]    {2x1 cell}    [1]
fdu{1}
    [11x11 double]    {2x1 cell}    [0]

so now fdu only contains uniform meshes, the density of the mesh and the location solely depends on the gd. The algorithm behind fjuniform is based on the interpolation of possible finest mesh onto new mesh.

The last question is how do I visualize the uniform field then ? simple, plug back into fjcolorgridm as usual :)

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

FJVis 3D tutorial

June 25th, 2009 by windmaomao

This article will explain 3D visualization using FJVis, in case you need to get to know FJVis, check out FJVis tutorial for 2D and data structure explained here.

isosurface_4d_demo
(4D plot using FJVis in Dr. Li (Xiangrong)’s research)

3D may or may not be the world scientific people would consider at this stage (well worth a paper, isn’t it?) But either way, it’s nice to present data this way sometimes.

Good news is that, the usage of 3D plot in FJVis are pretty similar to the ones in 2D, and if needed data can be converted from 3D to 2D with slice control easily.

Let’s load the data first. Ye, it start from the same command tmmpost, the 2D/3D dimension info are hidden from end-user. Just make sure you remember what dimension of data you are dealing with at one time, ex. use a good name, fd2, fd3 :)

[time,fd]=tmmpost(nframe);

3D Isosurface

Navigation in 3D is hard in general, one of the approach people normally use is to find surfaces with certain value and connect these surfaces together to make an iso-surface plot.

The command is,

fjisosurfm(fdd,bounding,view,option)

where fdd is the data you want to plot, other than the data handle as you would think, fdd includes isosurface value, color of surface, transparecy of the surface info using a cell structure, like

fdd={fd{1},0.5,'r',0.2};

which tells to choose the first variable and look for the iso-surface value of 0.5 (normally this is our interface value), and the color of the surface will be plotted using red color, and the handy transparency value turns the surface to be look-through, so you can plot couple of iso-surface together, like the following

fdd={fd{1},0.5,'r',0.2;fd{3},0.3,'b',0.1};

By blending surfaces with different transparencies, you can plot couple of quantities at same time and stories normally better told this way.

well, this is just the first parameter, the rest of the input parameters are easy to follow. bounding is a bounding vector as usual, including axis range and value rang. ex.

ex: [] for automatic x, y and z
ex: [0 1 0 1 0 1] for x->[0,1], y->[0,1], z->[0,1]
ex: [0 1 0 1 0 1 0 0.5] for x->[0,1], y->[0,1], z->[0,1] and value->[0,2 ];

option is a string of option letters as always :), ex.

‘o’: plot on new figure instead of previous one
’s’: scale the graph with x to y ratio 1:1
‘c’: use simple color scheme instead of standard hsv
‘b’: show all patches box boundary
‘g’: show all grids, caution: time consuming
‘f’: plot on 4D surface grid, automatically if set fdd right
‘a’: turn off all axis and colorbar
‘r’: turn on rotation helper, only works for 3D

4D property mapping

Sometimes you want to take a look of one variable on the iso-surface of another variable (like the picture on the top), this is referred as 4D plot :)

Luckily, the only change to turn 3D into 4D is to set fdd input variable accordingly, instead of referring to a color, now you refer to a field, like this

fdd={fd{1},0.5,fd3{5},0.8};

and yes, more than one surface can be plotted as long as you can mange the transparency right.

2D slice

I hate to mention this, but in general, please always forget beautiful pictures for one second, and forget dimension for another second, ’cause it’ll all down to the debugging sooner or later, and 2D is the world that can save your ass in the long run. While 3D or 4D gives you chilling excitement, 2D gives you accuracy and confident.

So, it’s easy to slice 3D data, type

fd2=fjslicem3m(fd3,slice_plane);

Here, fd2, fd3 are handles in two and three dimensions. Load 3D data into fd3, fd2 gives back 2D slice of the complete dataset; Where you want to slice should be given in slice_plane parameter like this,

slice_plane=[0.0 NaN NaN];

It only supports slicing through x, y or z plane, so the other two axis needs to be set as NaN, the above slices through x=0.0 plane.

What’s next after the slice ? Plot use fjcolorgridm with data handle fd2, we’re back in 2D world again, wola ;)

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

FJVis data structure explained

June 25th, 2009 by windmaomao

If you are not sure what FJVis is, please first check the first FJVis tutorial for 2D plot here.

Data conversion

The simulation data normally are saved in ASCII or binary file. And here FJVis first loads the data of one time frame saved from Steve’s package into matlab and then organized into groups using cell structure before plotting commands are issued.

And all this is accomplished by single call to

[time,fd]=tmmpost(nframe);

Meshes are converted into matlab friendly meshes, and there’s some conversion between raw data and the data used for post-processing. Most likely, the differences before and after the conversion are not noticeable. But in case you need to work on grids or raw data and raw meshes, you can use

[time,fd]=tmmpost(nframe,'d');

or

[time,fd]=tmmpost_noshift(nframe);

to prevent the conversion. Caution needs to be taken if you omit this conversion, since the plot without conversion is only for debugging, not presenting.

Data strucure

If you interested at how data are grouped internally, just type the loaded handle fd and return.

1
2
3
4
fd =
    {21x3 cell}
    {21x3 cell}
    {21x3 cell}

Inside this hugh cell structure, the first levels show all the variables and then type fd{1},

1
2
3
4
fd{1}
    [34x34x34 double]    {3x1 cell}    [0]
    [12x10x18 double]    {3x1 cell}    [1]
    [10x12x18 double]    {3x1 cell}    [1]

you reach second level with all the meshes associated with first variable. The data are organized into matrix data, data axis range and mesh level, ex. fd{1}{1,2} gives you the data axis range of first patch mesh of the first variable.

Package extension

Since now you know how the data are organized internally, you can write your own hack to access information embedded inside. Ex. to find the maximum pressure, try

fjmatmax(fd{4});

and for your own extension, take a look at the script to get started.

Since the introduction of FJVis last year, many extension has been written including data extracting, conversion and calculation. List all script with prefix “fj”, and see if anything catch your attention before you write your own extension or just ask around :)

Have fun.

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

Simulation and Mathematical Modeling

June 8th, 2009 by hyoussef

Mankind has always tried to understand natural phenomena and predict the uncertain future of their surrounding. It is thousands of years that mankind tries to predict natural phenomena such as weather, change of seasons, movement of planets, moon and its effect on ocean tie and etc. Most of these predictions were base on observations and experimentations through thousands of years. Within the past few centuries and with the emergence of more advanced sciences such as mathematics, physics, chemistry and biology the path of understanding nature has changed. Scientists have learned a more systemic approach to investigate their surroundings. These include a more targeted experimentation based on proven science, use of mathematics as a tool to model the nature and to have an interdisciplinary approach to solve problems by applying an amalgam of these sciences.

This approach further expanded the boundaries of knowledge to the point we are at. The only catch is that the more we learn, the less these methods are going to be applicable. Therefore, new tools are essential in order to further understand the challenging problems of the day. One of these tools that gives extraordinary power to scientists and researchers to investigate highly complex systems is mathematical modeling and computer simulation. Simulation science has recently gained a lot of attention after development of computers in 80s and 90s. Computers have made it possible to solve complicated system of ODEs or PDEs numerically by performing millions of operations in seconds.

In order to run a successful simulation, a problem (or a system) should be initially modeled with proper equations. Then these equations should be discretized and non-dimensionalized to a system of algebraic equations. Afterward, the system of algebraic equations can be solved to obtain numerical values of the equation in the current time (these values belong to a certain point that is called the node). The information obtained for each node can be used to obtain the future values of the nodes for the consecutive time steps. This will allow predicting the current and future behavior of the system in such a short period of time. It is quite clear that computer simulation can not be substituted with experimentation but it can help cutting the experimentation expenses drastically by avoiding unnecessary experiments. It also helps understand the behavior a system when experimentation is not possible due to hazard (such as simulation of nuclear reaction) or it is very expensive such as cancer modeling or particle physics experiments.

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn

FJVis 2D tutorial

May 31st, 2009 by windmaomao

FJVIS package is designed to visualize and post-processing simulation data-set generated from Wise’s PDE solver developed in Lowengrub’s lab at UCI. It’s written in Matlab script and served as a bridge component from the design point of view.

Load data

To start, first load simulation data file into memory by using

[time,fd]=tmmpost(nframe);

where the input parameter nframe2 is the frame number of the data-set; and the function returns time of the frame and data storage object fd which is organized into variables, each of which consists patches of different levels. Both 2D and 3D data are supported.

Colorgrid plot

To plot two dimension data, it’s always good practice to project data onto 2D plane while using color to indicate value magnitude. This type of plot is referred as colorgrid plot (other name includes, pcolor in Matlab, pm3d in GNUPlot, etc.). In FJVis, the command is,

[h]=fjcolorgridm(fd{n},bounding,option)

where fd{n} is n-th variable for tumor data object; bounding is a bounding vector, including axis range and value rang. ex.

ex: [] for automatic x and y
ex: [0 1 0 1] for x->[0,1], y->[0,1]
ex: [0 1 0 1 0 2] for x->[0,1], y->[0,1] and v->[0,2 ];

option is a string of option letters, ex.

‘o’: plot on new figure instead of previous one
’s’: scale the graph with x to y ratio 1:1
‘c’: use simple color scheme instead of standard hsv
‘b’: show all patches box boundary
‘g’: show all grids, caution: time consuming
‘f’: plot on 3D surface grid, need to change bounding vector as well
‘a’: turn off all axis and colorbar
‘r’: turn on rotation helper, only works for 3D

and the output parameter h contains a vector of patch handles.

vessel_nutrient

VN:F [1.6.4_902]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.4_902]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn