Matlab data save to VTK format

I was looking for some function to save matlab array to VTK data format, and found just what I wanted in this page. They are shown below with some modification on my own:

Scalar data:

function savevtk(array, filename)
%  savevtk Save a 3-D scalar array in VTK format.
%  savevtk(array, filename) saves a 3-D array of any size to
%  filename in VTK format.
    [nx, ny, nz] = size(array);
    fid = fopen(filename, 'wt');
    fprintf(fid, '# vtk DataFile Version 2.0\n');
    fprintf(fid, 'Comment goes here\n');
    fprintf(fid, 'ASCII\n');
    fprintf(fid, '\n');
    fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
    fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
    fprintf(fid, '\n');
    fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
    fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
    fprintf(fid, '\n');
    fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
    fprintf(fid, 'SCALARS scalars double\n');
    fprintf(fid, 'LOOKUP_TABLE default\n');
    fprintf(fid, '\n');
    for a=1:nz
        for b=1:ny
            for c=1:nx
                fprintf(fid, '%d ', array(c,b,a));
            end
            fprintf(fid, '\n');
        end
    end
    fclose(fid);
return

Vector data:

function savevtkvector(X, Y, Z, filename)
%  savevtkvector Save a 3-D vector array in VTK format
%  savevtkvector(X,Y,Z,filename) saves a 3-D vector of any size to
%  filename in VTK format. X, Y and Z should be arrays of the same
%  size, each storing speeds in the a single Cartesian directions.
    if ((size(X) ~= size(Y)) | (size(X) ~= size(Z)))
        fprint('Error: velocity arrays of unequal size\n'); return;
    end
    [nx, ny, nz] = size(X);
    fid = fopen(filename, 'wt');
    fprintf(fid, '# vtk DataFile Version 2.0\n');
    fprintf(fid, 'Comment goes here\n');
    fprintf(fid, 'ASCII\n');
    fprintf(fid, '\n');
    fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
    fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
    fprintf(fid, '\n');
    fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
    fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
    fprintf(fid, '\n');
    fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
    fprintf(fid, 'VECTORS vectors double\n');
    fprintf(fid, '\n');
    for a=1:nz
        for b=1:ny
            for c=1:nx
                fprintf(fid, '%f ', X(c,b,a));
                fprintf(fid, '%f ', Y(c,b,a));
                fprintf(fid, '%f ', Z(c,b,a));
            end
            fprintf(fid, '\n');
        end
    end
    fclose(fid);
return
Advertisements

Installing MPB

I wanted to try MPB also, it’s a photonic bandgap simulator by MIT. So I downloaded the package and tried to install in my office workstation. The config stage couldn’t pass since it didn’t find the FFTW library, though I have one installed. It turns out that mine is fftw3 while MPB is not compatible with fftw3 yet. So I have to installed fftw2 into my local directory. The installation for fftw2 is pretty straightforward. Now I need to specify both fftw2, libctl and HDF5 location into the configure of MPB (you may want to refer to Meep installation also, it’s quite similar). This is what I typed to configure MPB.


./configure --prefix=/scratch/kurniawano/local LDFLAGS="-L/scratch/kurniawano/local/fftw2/lib -L/scratch/kurniawano/Download/hdf5/lib" CPPFLAGS="-I/scratch/kurniawano/local/fftw2/include -I/scratch/kurniawano/Download/hdf5/include" --with-libctl=/scratch/kurniawano/local/share/libctl

However, when I typed make, it gives me an error, from this page, I found out that the problem is that I am using HDF5 version 1.8, so I needed to pass through the option -DH5_USE_16_API=1 in the CPPFLAGS. After adding this, my installation works fine 🙂

Drude model in Meep

I tried to simulate metal with Drude-Lorentz model. However, the expression given in Meep docs for the Drude term is slightly different, check this page. In Meep the dispersion relation is given by

\epsilon(\omega) = \epsilon_\infty + \frac{\omega_D^2 \sigma_D}{\omega_D^2-\omega^2-\imath \omega \gamma_D} + \frac{\Omega_L^2 \sigma_L}{\Omega_L^2-\omega^2-\imath \omega \Gamma_L}

where as the usual Drude-Lorentz model is given by

\epsilon(\omega) = \epsilon_\infty - \frac{\omega_D^2 }{\omega^2+\imath \omega \gamma_D} - \frac{\Omega_L^2 \sigma_L}{(\omega^2-\Omega_L^2)+\imath \omega \Gamma_L}

The last term, Lorentz term, is of course the same. The drude term, however, is slightly different. In the Meep-discuss forum, the following suggestion was given.

  • Use a very small value for \omega_D, in this way, the \omega_D in the denominator of the Drude term can be ignored.
  • The sigma value is computed to give the right resonance frequency for the Drude term.

Viewing animated gif in Linux

I was trying out Meep and it outputs animated gif from several png files. To view the animated gif, simply type

animate filename.gif

that’s it 🙂

Meep: No Match

I encountered this error “Meep: No Match” when running the tutorial for file bend-flux.ctl, From the forum, it turns out that I need to add \ before the ? when typing no-bend? variable the shell. So the correct command line is:

$ meep no-bend\?=true bend-flux.ctl | tee bend0.out
$ meep bend-flux.ctl | tee bend.out

Installing Meep 1.1.1

I tried to install Meep 1.1.1 in my CentOS workstation. It turns out that it needs some more packages to be installed. First Meep requires guile and libctl, So after downloading them, I tried to install guile. But it failed since guile requires GNU MP (sigh). So I downloaded GNU MP 4.3.1 and did configure, make, make install. I successfully installed in in a local folder. Then in configure guile, I need to specify the location of the GNU MP as well as the libltdl, so I typed:

./configure --prefix=/scratch/kurniawano/local LDFLAGS="-L/scratch/kurniawano/local/lib/" CPPFLAGS="-I/usr/share/libtool/libltdl/ -I/scratch/kurniawano/local/include"

I also need to specify the gmp.h location using CPPFLAGS. Otherwise, I will get an error “At least GNU MP 4.1 is required, see README”. After that I successfully installed guile 1.8.7. Once this is done I configure libctl and specifying the location of guile using LDFLAGS in the configure command. libctl was successfully installed also 🙂

During configure, a warning message shows that I didn’t have hdf5.h in my system. So I downloaded hdf5 and included the header files in the CPPFLAGS when installing meep 1.1.1. Before installing hdf5, I need to install SZIP library first. The installation for SZIP library is pretty simple and no problem encountered. However, it requires me to rebuild libctl again. After I installed hdf5, meep configure gives no error. But when I did make, it gives me error undefined reference to some hdf5 libraries. The problem is that some interface has changed in hdf5 1.8.x version. In one of the forum, it was suggested to install h5utils version 1.11.1.

To overcome this problem, I installed h5utils version 1.12.1 from MIT website, and configure it as follows:

./configure --prefix=/scratch/kurniawano/local LDFLAGS="-L/scratch/kurniawano/Download/hdf5/lib -L/scratch/kurniawano/local/lib" CPPFLAGS="-I/scratch/kurniawano/Download/hdf5/include -I/scratch/kurniawano/local/include" LIBS="-lhdf5"

If you use the shared library, you need to add the hdf5/lib into your LD_LIBRARY_PATH to make the configure successful. Besides that to create png, jpg image from hdf5 output of Meep, you need to install libpng, libjpeg. Another library that is required by h5utils is matheval.

To configure meep 1.1.1, I typed:

./configure --prefix=/scratch/kurniawano/local LDFLAGS="-L/scratch/kurniawano/Download/hdf5/lib -L/scratch/kurniawano/local/lib" CPPFLAGS="-I/scratch/kurniawano/Download/hdf5/include" LIBS="-lhdf5" --with-libctl=/scratch/kurniawano/local/share/libctl

This configures fine, but when I run make, it gives me this error:


libtool: link: warning: library `/scratch/kurniawano/Download/hdf5/lib/libhdf5.l
a' was moved.
grep: /mnt/hdf/packages/szip/shared/encoder/Linux2.6-x86_64-gcc/lib/libsz.la: No
such file or directory
/bin/sed: can't read /mnt/hdf/packages/szip/shared/encoder/Linux2.6-x86_64-gcc/l
ib/libsz.la: No such file or directory
libtool: link: `/mnt/hdf/packages/szip/shared/encoder/Linux2.6-x86_64-gcc/lib/li
bsz.la' is not a valid libtool archive

though, I have installed szip library, it seems that hdf5 looks at a different location. So I needed to edit the libhdf5.la file under the hdf5/lib folder, and comment the line for dependency_libs then add the line for szip lib location:


# Libraries that this one depends upon.
#dependency_libs=' -L/mnt/hdf/packages/szip/shared/encoder/Linux2.6-x86_64-gcc/l
ib /mnt/hdf/packages/szip/shared/encoder/Linux2.6-x86_64-gcc/lib/libsz.la -lz -l
m'
dependency_libs='/scratch/kurniawano/local/lib/libsz.la -lz -lm'

After this, the make and make install runs fine.

If you want to build Meep with MPI enabled for parallel computation, add –with-mpi during the configure stage.

How to upload photos from IPhoto to Flickr

Someone has created a great plugin to easily export photos in your iPhoto album to Flickr. You can install it from this link:
http://www.dustin.li/Publish/Software/Entries/2007/12/26_Free_Flickr_eXport_iPhoto_Plugin.html

Simply install the dmg file, and then restart iPhoto. To export, click “File->Export”, choose the tab FFXporter, and upload your photos 🙂