You may wonder why I chose the Basic computer language for writing the BFlow fluid library in. Basic isn’t exactly a mainstream language in computational modeling. The first version of Numerical Recipes supported it, both in text and CD-ROM, but later editions have not. A recent check of SourceForge found 11726 projects listed under Science and Engineering. Of those, only 30 were in Basic. Here is the breakdown:
Lang. Num Projs --------------------- Fortran 224 ( 1.9%) C 2477 (21%) C++ 3665 (31%) Python 1103 ( 9.4%) Basic 30 ( 0.3%) --------------------- Total 11726
I didn’t check all languages and some projects use more than one language. That is why the numbers don’t total up.
In addition to being little used in science, Basic suffers from a PR problem. The common view is that Basic is…well, basic. It is a toy language suitable for people unable or incapable of mastering “real” languages like C, C++ or Java. On top of all that, Basic is typically an interpreted language and thus is dog slow.
Given that dismal preamble, how did I end up choosing this language? It was historical. I generally use Excel for data analysis and quick calculations. Around 1995, Excel version 5 was released. This version introduced Visual Basic for Applications (VBA) to replace the Excel macro language. I found it incredibly easy to code in VBA. The code just flowed. And debugging was a joy. You just hold the cursor over any variable in scope and its value would pop up. In a very interactive way, you could write programs.
I began writing a flow solver in VBA. I knew it would be slow, but I wanted to experiment with some ideas I had, and thought it would be cool to have Excel solve some flow problems. Eventually I got the thing working, and sure enough, it was very slow. For many problems it was intolerably slow.
PowerBasic
In 2001, I looked around for alternatives. I didn’t want to rewrite everything, so I was hoping for a Basic compiler that was similar to VBA. I considered Microsoft Visual Basic, but Microsoft products tend to be big and bloated and overly complex. It turns out there are many other Basics out there. I came across the PowerBasic compiler. They emphasized the speed of their code and the easy porting from Visual Basic. So I sent in my $200 and gave it a try. In short order, I had my code ported over and was enjoying a 20x speedup.
I have been using PowerBasic since 2001 and find it to be a simple, robust compiler. The debugger isn’t as nice as the VBA debugger, but I can live with it. As implemented by PowerBasic, this language is quite powerful, having pretty much all the features found in C: arrays, structures, wide range of looping constructs, pointers, function pointers. It offers full access to the Windows API, for creating Windows applications. It comes with tools for creating user interfaces, doing TCP communication, talking to COM objects, inline assembler and many other things. All in all it is very capable.
I have found a few things that are unattractive for scientific computing. The compiler is optimized more for file size than speed. With care, you can make programs as fast as C or Fortran. With less care, you code can easily be 2x – 5x slower than an optimized C or Fortran program. Another problem: PowerBasic doesn’t allow forward declaration of user defined types (structs). This makes it hard to create several structures that reference each other.
In this blog, I will mostly use C for my experiments. It is widely used and understood and is fast. Over time, I may migrate BFlow to C if it makes sense to do so.