Thoughts on Programming Languages for Scientific Computing

7 #

I have a friend who works in chemistry / protein folding and thus does a lot of computational stuff within that field. Coming from an application/web programming background, I was utterly shocked when she mentioned that FORTRAN is widely used in computational simulations. The great programmer Nietzsche once declared that “FORTRAN is dead”, and I held that belief too, thinking that FORTRAN was only used in the days when C and C++ weren’t developed and programmers didn’t have anything else. I didn’t know a single person who uses FORTRAN.

But I was wrong. It turns out that FORTRAN is still actively used in scientific computing. In addition, it is not an “outdated language”. FORTRAN actually has quite a few versions which have evolved and added on modern programming languages features over the years.

The biggest draw of FORTRAN is speed. It’s one of the lowest level out of all the high level programming languages (like C, C++, Pascal, Java, etc.). The language, intended for scientific computing, was designed to be fast. In comparison, languages like C and C++ were designed as general purpose languages that could be used for a variety of purposes.

But I wasn’t totally sold on FORTRAN. I don’t like the syntax and rather code in C++ any day. I didn’t believe that C++ was all that much slower than FORTRAN so I decided to do some research into this matter. The following comes from an email I wrote:

  1. FORTRAN is widely cited to be “20% to a factor of ten” faster than C++. I didn’t find anything for C, but I think the speed of C is about the same as C++.
  2. However, the data in #1 was published in 1997. In the modern day, C and C++ compilers have been heavily optimized to produce speeds comparable to FORTRAN. Some modern benchmark results are here:

    (These two benchmarks show FORTRAN to be about the same as C/C++
    depending on how the code is written and compiled)
    http://dan.corlan.net/bench.html (maybe around the year 2000)
    http://dan.corlan.net/amd64_dual_core_benchmarks.html (newer version)

    (This benchmark shows FORTRAN to be much slower than C/C++. Java is
    even faster!)
    http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all

    (This benchmark shows C++ being slightly faster than FORTRAN)
    http://pauloherrera.blogspot.com/2006/12/introduction-ive-read-lot-of-things.html

  3. In addition, there is a library for C++, called Blitz++, which allows C++ programs using the library to approach the speed of FORTRAN. Benchmarks are here:
    http://www.oonumerics.org/blitz/benchmarks/
    (Note the percentages, ie. 95.7%, is the speed relative to the
    comparable FORTRAN program. So 100% is the same speed.)

    Also note that the benchmarks for Blitz++ were completed in like the year 2000. I expect if the same benchmarks were run today with modern compilers, the results would be even better.

  4. Therefore, I conclude that C/C++ is comparable to the speed of FORTRAN and depending on what you are doing and how you write the code can be faster than FORTRAN. So it depends on your preference of the programming language. I would probably always pick C++ over FORTRAN since I am more familiar with C++ and I think C++ code is easier to write and read.
  5. Surprisingly, python *can* be really fast. See:
    http://scipy.org/PerformancePython
    Of course, it will never approach the speed of C++ or FORTRAN, but Python code can be very easy to write and for medium sized projects, you can write the code much faster.
  6. Surprisingly, Java *can* be faster than C/C++. This is largely attributed to compiler optimizations that Java can do that C/C++ can’t do. See:

    http://www.idiom.com/~zilla/Computer/javaCbenchmark.html

    http://www.kano.net/javabench/ (see links at bottom of the page for more java vs c++ benchmarks)
    http://www.kano.net/javabench/data (good graphs)

    In my opinion, Java code is easier to write than C++ code. You might also get advantages of portability since Java code can pretty much run on any computer unmodified.

  7. Unsurprisingly, Matlab is slow. Even when Matlab code is optimized, it is at least 2 times as slow as comparable C/C++ code. Unoptimized, it can be 30 times or more slower! Of course, the good thing about Matlab is that you can easily write programs that perform math — much faster than with C/C++, Java, Python, or FORTRAN. So Matlab might be good to test an idea or implement something on a small scale.

Overall conclusion: C++ speed is about the same or only a little bit slower than FORTRAN. As C++ compilers become more optimized, the difference decreases. Java seems pretty fast, so I would probably do more research on it. If it *really* comparable to C/C++ speeds, I would probably use Java. For medium scale applications, I would use Python. For small scale applications, I would use Python or Matlab.

Created Wikipedia Article: N-Version Programming

1 #

I’ve been trying to contribute to Wikipedia more often these days, particularly in the sciences since I’m supposed to be knowledgeable in that field. Today, when I was writing a paper on electronic voting systems for a humanities class (PS 12), I stumbled upon a paper about a e-voting system that used N-Version Programming (NVP). Now, what is NVP? You ask. That’s a good question. I went over to Wikipedia to learn about it, but Wikipedia didn’t have an article on N-Version Programming! No way!

Well, I found out more about NVP through reading a few papers and decided to contribute back to Wikipedia by creating the article: N-Version Programming. That’s right, everything on that page right now should be my work (check the history to see if other people have modified it). It took me over about an hour to write the article and to reference the heck out of it. But overall, I’m glad I finally created an article in Wikipedia.

Most of the time, however, I’m making small edits here and there. You can view my list of contributions here.

Edit: Maybe in my paper, I should cite the wikipedia article and see if the grader thinks I’m plagiarizing off of the article :).

Tags: |

Caltech/Avery Quotes

2 #

A fellow house-person has been keeping track of quotes from people around her. I convinced her to put up the quotes on the Avery website. They are really funny to me, but I suppose you might have had to be around to fully appreciate some of them (certain quotes are really reflective of the people who uttered them). I appear in there a few times (mine aren’t really that funny though). Here’s one:

Michael: “So I got the right answer for number five, but I think I did it the wrong way. Doesn’t it break symmetry or something?”
Susan: “Um, no, because it’s over a function that isn’t symmetrical.”
Michael: “Ohhhh….okay, thanks!” (walks away)
(I hear him mumbling to himself down the hallway joyfully):
“So I’m not crazy! I’m still sane!”

Tags: | |

Analyzing the Effectiveness of Brita® Water Filters

6 #

This was my final paper for my independent research project in a laboratory class, Ch 15, at Caltech. I’m currently in finals week so I have about 4 finals to take and 1 more assignment to finish.

Abstract:

Brita(R) water filters are popular personal and home water filtration systems that claim to reduce, among many impurities, the concentration of copper in tap water by at least 74% and chlorine by at least 94%. These claims and the performance of the filters were tested by filtering residential tap water and then collecting filtrated samples at various points along the filtration process up to 40 gallons (Brita(R) ’s claim of the filter’s lifespan). By using mercury electrode differential pulse cyclic voltammetry and uv-vis spectrometry (on DPD colorimetric reaction), the concentrations of copper and chlorine, respectively, were determined in unfiltrated and filtrated tap water. The results showed that there was an average of 36.10% +/- 2.43% reduction of copper and 72.49% +/- 0.03% reduction of chlorine throughout the 151 L (40 gallons) filtering of the Brita(R) water filter. There was also a detectable difference (5.48% change for copper and 17.12% change for chlorine), albeit small, between new filters and filters due to be replaced after 151 L. Under real world usage of the Brita(R) water filter (and not in prepared laboratory samples), Brita(R) ’s claims about percent reduction are overestimated by ~43% for copper and 13% for chlorine.

Full Paper: Analyzing the Effectiveness of Brita® Water Filters (PDF, 565 KB).

Tags: |

My SURF Presentation in Video Form

0 #

I’m still alive…just in finals week at Caltech. I managed to obtain a digital copy of my SURF presentation from last month thanks to Eric Tai so I uploaded it to Google Video:
Enhancing conductivities in (porphyinato)metal based materials with alkyl-ethylene glyco substituents, SURF 2006 (Caltech) Presentation

Tags: |

Chemistry Research at UPenn

0 #

I’m somewhat 6-7 weeks into my SURF (summer undergraduate research fellowship) at the University of Pennsylvania (although I got the SURF from Caltech) so I figured I should post something.

The project I’m working on is slightly different than from my project proposal that I posted a few months ago. Essentially, I’m more focused on making porphyrin for conducting polymers now instead of for supercapacitors. In short, a conductive polymer is like a wire but made with organic elements (like carbon, oxygen, nitrogen atoms) instead of with metals (like copper, silver, iron, although a little bit of metal can be added). Conductive polymers are cool because they can be used to create molecular electronics (imagine wire being constructed piece by piece on a computer chip), organic electronics (such as OLEDs), supercapacitors, and generally cheaper electronics.

However, a problem that they suffer from is that their conductivities are not very high. For instance, the electrical conductivity of copper is 59.6*10^6 S-m^-1 whereas polyacetylene, a conducting polymer, has a conductivity of around 10^-7 S-m^-1 which is like 18 orders of magnitude smaller. However, a better comparison would be with silicon (which conducting polymers have a good chance of replacing) which has a conductivity of around 10^-5 S-m^-1 (I’m not too sure on this number so don’t quote me on this).

Porphyrin can be made into conductive polymers (although we are working with oligmers, really short polymers with only a few units length), and recent research (by my mentor Paul and the group) has shown that it has the potential to have conductivities at or even better than amorpous silicon!

My project consists of working with a certain class of porphyrin and synthesizing a new variant of them. This new variant will have a flatter chemical structure that will allow the porphyrin oligmers to pack more closely to each other. When they are closer to each other, then they can interact with each other better (pi-pi bonding is increased) and pass electrons (and holes) more effectively.

Well, at least that’s what my mentor and I think so far. I’m still in the process of making the porphyrin. The synthesis process is proving to be really tedious since it has around 12-15 steps which, after each step, requires one or more purification processes (mostly column chromatography). In total, I have over 24-28 steps to complete. Each step takes around a whole day so this is pretty much why I’m still in the synthesis process.

The yield is also very low (around 15-20%). The amount of material I’m currently working with is about 1 gram (I started with around 4.5g). Probably by the end of the all the steps, I’ll end up with maybe 0.2 grams of porphyrin. But those 0.2 grams are the result of over 6 weeks of labor and hundreds of dollars of materials. This is partially the reason why my mentor in recent days keeps reminding me to guard these samples (not fully synthesized yet though, but very close) with my life. Although he’s kind of joking, pretty much, if I screw up (which I’m very prone to do), I should just like give up and go into computer science or something.

The idea of manipulating matter at will (aka. Chemistry) is tremendously awesome, but it can be so tedious at times. For instance, if one comes up with an idea in chemistry, it might take weeks just to test it. That’s too long! I’m used to the computer science method is programming things at once and making modifications on the fly—instant feedback.

I actually started writing this post last week or so and didn’t get around to finishing it. Today, my mentor (he helps me do a lot of synthesis work) are almost close to completion. We have two and three these new porphyrin variants connected. Our goal is to make a pentamer (five connected porphyrins). The next reaction should give us a pentamer. After purifications and such, we should have the final product by the end of this week.

Tags: |

Playing with Group 1 elements

0 #

After a big lecture on resonance today in Chemistry 1A where we learned about how to make fertilizer (Ammonium Nitrate) explode (which was what Timothy McVeigh used in the 1995 Oklahoma City Bombings), how to coat chalkboards with an unstable substance so that professors writing on it will cause small explosions, and how the Sarin nerve gas works, Professor Nathan Lewis was on a roll and had a TA take everyone out for Group 1A elements dropping in water demonstrations! I swear that this doesn’t happen usually. The lectures are usually really formal and direct with no fun and games here.

Well, as you may know, when Group 1A elements (Lithium, Sodium, Potassium, Rubidium, etc.) come in contact with water, they have a large exothemic reaction (meaning that lots of energy is given off) where they bond with an OH- group liberating hydrogen in the process. Example:
2Li+ + 2H20 -> 2LiOH + H2
(H2 is the symbol for hydrogen gas)
The energy given off by that reaction ignites the hydrogen gas causing the “explosion”.

So one of the TAs threw in decent sized chunks of Lithium, Sodium, and Potassium. Unfortunately, they didn’t try any of the other elements:

The effect was actually a lot better than these pictures show it. I only captured the beginning of the reaction. Later on, lots of smoke was generated. This was what I always wanted to do in high school chemistry…

Tags: |

First Edition of Copernicus’ De Revolutionibus

0 #

In Physics 1a, the professor, Dr. David Goodstein, brought out a first edition copy of Copernicus’ De Revolutionibus, the book in which he first suggested that the explanation of celestrial bodies would be a lot easier is they revolved around the Sun instead of the Earth. This other page gives more information about the book including the publication date of 1543 (first edition) and a clearer title page image:

Copernicus' De Revolutionibus Title Page

I tried to take a picture of the book (it’s flipped to the title page), but people were mobbing the front, and this was the best shot I could manage:

Copernicus' De Revolutionibus at Caltech

It’s not all that exciting, but it’s still cool to be able to see such an old book that was hand bound and manually typeset. I would estimate that it had about 250 pages which definitely took a lot of work. I think the idea of publishing books so long ago without today’s technology is quite a feat.

Tags: |