Installing UQTkV3.0 on MAC OS x 10

Version 3 of UQ Toolkit is out.

The UQ Toolkit (UQTk) is a collection of libraries and tools for the quantification of uncertainty in numerical model predictions. Just like version 2, version 3.0 offers intrusive and non-intrusive methods for propagating input uncertainties through computational models, tools for sensitivity analysis, methods for sparse surrogate construction, and Bayesian inference tools for inferring parameters from experimental data.

Installing UQTk version 2 on a Mac was easy. Use gcc or clang and fire up the make file, but for version 3, the team has now shifted to cmake and if you want python interface you also need SWIG.

Just like in life, additional dependencies bring additional complexity and chances of things going wrong. Well this was my experience while installing UQtk version 3 on my mac.

Here are my running notes while installing the library on OS X 10.

Cmake doesn’t come preinstalled on a Mac. So get it from here. Installation is easy.


I didn’t have swig so had to download swig from here

Installing is 4 steps

  • Go to the untarred directory,
  • ./configure
  • make
  • sudo make install

I forgot the 4th step and got the following error while installing uqtk

CMake Error at /Applications/CMake.app/Contents/share/cmake-3.6/Modules/FindPackageHandleStandardArgs.cmake:148(message):

Could NOT find SWIG (missing: SWIG_EXECUTABLE SWIG_DIR)

Call Stack (most recent call first): /Applications/CMake.app/Contents/share/cmake-3.6/Modules/

— Configuring incomplete, errors occurred!

See also “/Users/sukhbindersingh/PROJECTS/UQTk_v3.0/builddir/CMakeFiles/CMakeOutput.log”.

 

So make sure, you do all the four steps…

 

SWIG needs PCRE-Perl compatible Regular expression library, which was not available on my mac, so download that too from here

Download PCRE not PCRE2. I wasted time installing PCRE2 to find that swig doesn’t recognise it.

 

Make CMAKE work in command line

 

Now cmake opens up as GUI in Mac OS by defauly, but we need command line version, so we have following 3 options

One may add CMake to the PATH:

PATH=”/Applications/CMake.app/Contents/bin”:”$PATH”

Or, to install symlinks to ‘/usr/local/bin’, run:

sudo “/Applications/CMake.app/Contents/bin/cmake-gui” –install

Or, to install symlinks to another directory, run:

sudo “/Applications/CMake.app/Contents/bin/cmake-gui” –install=/path/to/bin

 

I opted for the second option, mostly because it was simplest and I want cmake to available everywhere and every time I need it

sudo “/Applications/CMake.app/Contents/bin/cmake-gui” –install

 

Where are your gcc, gfortran, and clang?

 

Once we have all these and before you start, we have to check few things. In this installation, I wanted to use gcc, gfortran, and clang for my setup, so get to know where they are located in your system by using the ‘which’ command

The Installations

 

In the terminal, go to the UQTk_v3.0 directory

  • mkdir builddir
  • cd builddir/
  • cmake -DCMAKE_Fortran_COMPILER=/usr/local/bin/gfortran -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/clang ../../UQTk_v3.0

 

Replace green text with your own path.

Once the cmake configuration is complete successfully, type make

All will go ok, until you hit this error.

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:23:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

MacOSX10.11.sdk/usr/include/unistd.h:510:14:error:declaration of ‘optarg’ has a different language linkage

extern char *optarg; /*

getopt(3) external variables */

^

/Users/sukhbindersingh/PROJECTS/UQTk_v3.0/cpp/lib/include/gen_defs.h:49:14: note:

previous declaration is here

extern char *optarg;

^

1 error generated.

make[2]: *** [cpp/lib/CMakeFiles/uqtk.dir/tools/multiindex.cpp.o] Error 1

 

Well this is easy to solve and I think it’s an error in the file. Open gen_defs.h file from cpp/lib/include and comment the following line as shown.

extern char *optarg

 

 

Another error that I faced was the missing gfortran library because of no LIBRARY_PATH.

ld: library not found for -lgfortran

clang: error: linker command failed with exit code 1 (use -v to see invocation)

make[2]: *** [cpp/app/gen_mi/gen_mi] Error 1

make[1]: *** [cpp/app/gen_mi/CMakeFiles/

gen_mi.dir/all] Error 2

make: *** [all] Error 2

 

This meant my libgfortran.a was not in the LIBRARY_PATH, so let’s find where the library is and then set the LIBRARY_PATH environment variable pointing it to that folder.

Locate libgfortran*.a

sudo find /usr -iname ‘libgfortran*.a’

 

Found them here.

/usr/local/gfortran/lib/i386/libgfortran.a

/usr/local/gfortran/lib/libgfortran.a

Set the environment variable to that folder.

export LIBRARY_PATH=/usr/local/gfortran/lib/

 

Compilation should continue smoothly and at last type make install and you should have everything installed.


Type ctest to check your installation


Recap

 

So here’s recap of the steps.

  1. mkdir builddir
  2. cd builddir
  3. cmake -DCMAKE_Fortran_COMPILER=/usr/local/bin/gfortran -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/clang ../../UQTk_v3.0 [Without python]

    or cmake -DPyUQTk=ON -P-DCMAKE_Fortran_COMPILER=/usr/local/bin/gfortran -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/clang ../../UQTk_v3.0 [With python]

  4. make
  5. make install

I faced two problems,

  1. error:declaration of ‘optarg’ has a different language linkage Solution: comment the offending line.
  2. ld: library not found for –lgfortran clang: error: linker command failed with exit code 1 Solution: export LIBRARY_PATH=/usr/local/gfortran/lib/

Using PyUQTk option gave me lot of problem, have delayed installing python binding for now due to lack of time.

Well now this is done, will spend couple of weekends looking at the library, you can fork the entire library from my github page. It has all the libs, so if the compliers are similar, they can be used as is without going through all this steps.

Using Aliases In windows CMD

If you are like me and use mac and Linux at home and use windows at work. Then the following will be regular errors.

‘ls’ is not recognized as an internal or external command, operable program or batch file.

I always use ls, clear, cat etc. in my CMD shell and eventually get the following errors. This has happened a lot of time for me to do something about it. Fortunately, there’s an easy way to setup alias in the cmd windows and never see this nasty ‘not recognised’ error. First create a text file like this.

You can use any other commands that you use regularly. Next, open the cmd windows and type the following command

Doskey /macrofile=path/to/alias/file/filename

As shown in the next figure. That’s it.

Here’s how it works, no more nag error messages

Don’t want to type this message every time, create a windows shortcut like this  

That’s it!!

Exploring Matplotlib Styles

Last week got some free time. Used it to upgrade my python installation on Mac, a long awaited task.

Looking at the upgrade log, was most excited to finally see the new version of matplotlib.

So launched it and went straight to the new style package.

Matplotlib is great at graphs but the default style before 1.4.3 left many things wanting.

The style package adds support for easy-to-switch plotting “styles” with the same parameters as a matplotlibrc file.

import matplotlib.pyplot as plt

What are different styles available in matplotlib?

print(plt.style.available)

[u'dark_background', u'bmh', u'grayscale', u'ggplot', u'fivethirtyeight']

Here’s how to use this.

But first let’s generate some data

import numpy as np
data = np.sin(np.linspace(0, 2*np.pi))

The default plot

plt.plot(data, 'r-o')

default_matplotlib_1.4.3_styles

Let’s use ggplot

plt.style.use('ggplot') 
plt.plot(data, 'r-o')

ggplot_matplotlib_style

Dark Background like excel 2007

plt.style.use('dark_background')
plt.plot(data, 'r-o')

dark_background_matplotlib

BMH style

plt.style.use(‘bmh’)
plt.plot(data, 'r-o')

bmh_matplotlib_style

Graystyle

plt.style.use(‘grayscale’)
plt.plot(data, 'r-o')

grayscale_matplotlib_style

fivethirtyeight Style

plt.style.use(‘fivethirtyeight’)
plt.plot(data, 'r-o')

fivethirtyeight_matplotlib

We can even add our own custom .mplstyle files to ~/.matplotlib/stylelib or call use with a URL pointing to a file with matplotlibrc settings. Follow the following link to define your own style.

A Gentle Introduction to the Finite Element Method

FEM Introduction numerical

Almost every engineer today have some knowledge of theoretical and practical aspects of FEM. And many have played with various software packages at some point of time.

Well that is enough if you just want to perform analysis but if you want to truly understand the mathematical aspects of FEM and want to get a feel of the numerical methods under the hood of FEM then this short lecture is for you.

Here’s the introduction, plucked straight from the course. IF you like it, you will enjoy the lecture.

The course is divided into five lessons and all this in 100 pages. Happy Learning!!

If you haven’t been hiding under a stone during your studies of engineering, mathematics or physics, it is very likely that you have already heard about the Finite Element Method. Maybe you even know some theoretical and practical aspects and have played a bit with some FEM software package. What you are going to find here is a detailed and mathematically biased introduction to several aspects of the Finite Element Method.

This is not however a course on the Analysis of the method. It is just a demonstration of how it works, written as applied mathematicians usually write it. There is going to be mathematics involved, but not lists of theorems and proofs. We are also going from the most particular cases towards useful generalizations, from example to theory.

It is going to be one hundred pages with many figures and many ideas repeated over and over, so that you can read it with ease. These notes have evolved during the decade I have been teaching finite elements to mixed audiences of mathematicians, physicists and engineers. The tone is definitely colloquial. I could just claim that these are my classnotes and that’s what I’m like.

There’s much more than that. First, I believe in doing your best at being entertaining when teaching. At least that’s what I try. Behind that there is
a deeper philosophical point: take your work (and your life) seriously but, please, don’t take yourself too seriously.

I also believe that people should be duly introduced when they meet. All this naming
old time mathematicians and scientists only by their last names looks to me too much
like the Army. Or worse, high school!

I think you have already been properly introduced to the great Leonhard Euler, David Hilbert, Carl Friedrich Gauss, Pierre Simon Laplace and George Green. If you haven’t so far, consider it done here. This is not about history.

It’s just good manners. Do you see what I mean by being colloquial?

Anyway, this is not about having fun, but since we are at it, let us try to have a good time while learning. If you take your time to read these notes with care and try the exercises at the end of each lesson, I can assure that you will have made a significant step in your scientific persona. Enjoy!

Click the below link to access the excellent PDF. Don’t forget to thanks to Francisco Jaview Sayas !

A gentle introduction to the Finite Element Method by Francisco–Javier Sayas

Quick Intro to 2D and 3D Visualization in Python

contour_visualization_with_python
Posted a quick tutorial introducing visualization in python.

This tutorial offers a quick look at some common 2d and 3d Visualization available in python. This tutorial is meant to give anyone a quick start in matplotlib and mayavi.mlab.

The tutorial was written as part of CSRP program of AeSIAA which I am volunteering as a mentor to few aeronautical students.

View the tutorial by clicking this Quick Intro to 2D and 3D Visualization in Python

LS-DYNA Examples

LsDynaBirdStrike.avi

Well this one will be a short post.

If you are into analysis and by any chance use LS-Dyna analysis software tool, then I have a site to recommend to you all.

It’s www.dynaexamples.com !!

Yes as the name suggest there are numerous downloadable ls-dyna examples on this website.

Even if you are not into Ls-dyna, but are in analysis, I suggest you take a tour of this website. You will come out by learning something useful.

That’s it, if you know about other sites that might be helpful to other users of this blog, please do name it in comments.

Simple Introduction To Working With HDF In PYTHON?

Simple_Introduction_to_working_with_HDF_in_python

In May 2014 I wrote this tutorial, but stupidly made it as a gif format. Don’t know why?

Today had the chance to revisit that page and I felt the GIF format made the tutorial useless, so dumped it and used ipython notebook to create a more updated but simple tutorial to get started working with HDF in python.

If you are into scientific coding, you will encounter HDF and I hope this simple tutorial will get you started in python. Enjoy!

Click here to view the tutorial

Voronoi with Python

Voronoi with Python

A few weeks back, a colleague was searching for matlab for a task to get the Voronoi diagram from some points he had. I suggested why not use #python? He was not convinced and he got hold of matlab license and did his job.

But I had this itch for trying Voronoi in python, so here it is. Finally got some spare time. It was so easy in python, that I built the example around a tkinter gui.

But before we go to code, for everyone’s benefit What is Voronoi Diagram?

In mathematics, a Voronoi diagram is a way of dividing space into a number of regions. A set of points (called seeds, sites, or generators) is specified beforehand and for each seed there will be a corresponding region consisting of all points closer to that seed than to any other. The regions are called Voronoi cells. It is dual to the Delaunay triangulation.

via wikipedia

And here’s the code.

Continue reading

Rotate

You don’t learn cool things everyday. And today is not everyday, learnt this neat trick to rotate a matrix with a one liner in #python!!!

Rotate_A_matrix_in_python

b = np.array(zip(*a[::-1]))

Simple and powerful. Waiting for a chance to use it somewhere in actual code…

Want to understand what’s happening visit this page by Peter Norvig, director of research Google!!

Brief Intro to FEA or Finite Element Method (FEM)

introduction-to-fem-short-introduction
Recently someone asked me for a simple and short primer or refresher material on Finite element Method.

Here’s a super simple, brief, to the point , no nonsense PDF on FEA theory from the university of victoria.

Brief and simple. Just enough material to spark interest for the curious!!!

Brief Intro to FEA or Finite Element Method (FEM) [PDF]

Do you know any such good, brief and useful resource? Please share.

3 Simple Steps to fix ipython notebook error on MAC OS X

ipython notebook debuggingI thought I did something stupid to my installation of python on mac and python notebook stopped working. ipython works but ipython notebook went haywire.

Tried reinstalling python but the problem remained. Everytime got this error when ipython notebook was invoked.

return _parse_localename(localename)
File "/Users/millerc/anaconda/lib/python2.7/locale.py", line 435, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8

Today, the lazy me corrected the problem with help from this post. It turns out the error occurs after installing the latest Mac OSX 64-bit python.

Fortunately the solution is simple. Follow the 3 simple solution steps

Step 1
open your .bash_profile

Step 2
Add these lines to your .bash_profile:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Step 3
Reloaded the profile:

source ~/.bash_profile

Now run ipython notebook

ipython notebook

Done.

GIF animation in Python in 3 Steps

Gif Animation in PythonGIF animation in Matlab in 3 steps is a popular post here on this blog. Learnt the technique while trying to plot the results from the Karhunen-Loeve Expansion in September 2012.

Since then, I have moved from matlab to python.

But I wanted to recreate that same thing in Python. This is where I have to say, Matlab is like iOS and Python is Android. For doing this in Matlab, all ingredients were in place, but in python, this isn’t the case.

So here are the 3 long steps in Python to get a GIF animation.

Step 1:
Download Figtodat and images2gif

Step 2:
Import Figtodat and writeGif

import Figtodat
from images2gif import writeGif

Step 3:
Collect the figures into an array and call writeGif to get the animation


figure = plt.figure()
plot   = figure.add_subplot (111)
plot.hold(False)
images=[]
y = numpy.random.randn(100,5)
for i in range(y.shape[1]):
    plot.plot (numpy.sin(y[:,i]))  
    im = Figtodat.fig2img(figure)
    images.append(im)

writeGif("images.gif",images,duration=0.3,dither=0)

Don’t want to spend time finding the listed modules, download them directly from the link at the end of the post.

Here’s the complete code.
Continue reading

GIF animation in Matlab in 3 steps

matlab gif in 3 steps Videos are cool. But if you need an animation to put in documentation or a PowerPoint then GIF is the way to go.

So building on the previous post of getting a video of your simulation from matlab, this post explains three simple steps to get a GIF animation from mat lab.

The steps are simple.

1. Define the data structure to hold the image data.
2. Plot and get the frame
3. Store the frame image in the data structure
4. Create the GIF animation.

As always lets begin with a data to plot.

x=rand(100,10);

Won’t explain much about the data structures and command as Matlab documentation does a good job at that.

I will explain the how to of doing it without much effort. Leave a comment if you are stuck, will try to resolve it.

1. Define the data structure or for shortcut let Matlab provide it.

plot(x(:,k))
figure,
f = getframe;
[im,map] = rgb2ind(f.cdata,256,'nodither');

2. Store and get the frame image in the data Structure.

im is the data structure.

for k=1:10,
plot(x(:,k);
f=getframe;
im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither');
end

rgb2ind gets the pixel data to the im data structure.

3. Write out the image file.

imwrite(im,map,'imagefile.gif','DelayTime',0,'LoopCount',inf);

check Matlab help for other options.

Well that’s it. 3 steps for a gif animation from matlab

 

While I am with Matlab, I like this little bit of interesting history on the birth of Matlab. Shows how little bets bloom into something so big and useful.

Cleve Moler, the chairman of the computer-science department at the University of New Mexico, started developing MATLAB in the late 1970s. He designed it to give his students access to LINPACK and EISPACK without them having to learn Fortran. It soon spread to other universities and found a strong audience within the applied mathematics community. Jack Little, an engineer, was exposed to it during a visit Moler made to Stanford University in 1983. Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote MATLAB in C and founded MathWorks in 1984 to continue its development. These rewritten libraries were known as JACKPAC. In 2000, MATLAB was rewritten to use a newer set of libraries for matrix manipulation, LAPACK.

From wikipedia

3 steps tutorial to quickly create movie with Matlab

Animation with matlab

Matlab is ubiquitous in today’s field of engineering . The quick prototyping and convenience of plotting has made it a must tool for engineers.

And many a time, we need to create animation animation or video visualization demos of a simulation, Matlab comes very handy… and with three lines we can get a movie of the simulation.

Here’s a quick 3 step tutorial to get an avi file video of your simulation using Matlab.

But before that lets generate some random data to display and capture as a movie.

x=rand(100,10);

Created x random data with 10 columns.

Movie consists of frames and this are stored in a specific format. So let’s define how many frame we want.

nframe=10;

Step 1:

Allocate the movie structure that hold info from the frames. We call it mov.

mov(1:nframe)=struct('cdata',[],'colormap',[]);

Now the real capturing of frames. we will plot 10 data columns of x and get them into a movie.

Step 2:

We use a do loop to capture the frame.

for k=1:nframe
plot(x(:,k))
mov(k)=getframe(gcf);
end

Step 3:

This done. Now we have all figures in mov structure, lets create the movie

movie2avi(mov, 'moviename.avi', 'compression', 'None');

Done. You should have an AVI file in your current working dir.

But if you look at the video… It feels like flickering. That’s easy to correct just add this before the for loop to prevent flickering.

set(gca,'nextplot','replacechildren') 

That done.

Here’s the full Matlab script

nframe=10;
x=rand(100,nframe);
mov(1:nframe)= struct('cdata',[],'colormap',[]);
set(gca,'nextplot','replacechildren')
for k=1:nframe
  plot(x(:,k))
  mov(k)=getframe(gcf);
end
movie2avi(mov, '1moviename.avi', 'compression', 'None');

Meshing Fortran and Matlab together with Mex

matlab fortran A few days back colleague of mine needed a help in calling Fortran from matlab. At that time I knew about the call system command.

But as we tried solving his problem, I learnt about Mex. I knew Mex was matlab executable but had never explored them or used or created them.

My colleague’s problem was solved once we got matlab and the c compiler. That day and few days after, I continued exploring the dark alleys of Mex.

Learnt a lot.  Here’s the list of links that were great at explaining the meshing of fortran and c with matlab with Mex.

Perhaps someday I will use them in some project, but till then let them be kept here.