Pages

Showing posts with label computer modeling. Show all posts
Showing posts with label computer modeling. Show all posts

Thursday, February 24, 2011

Testing the ∇⋅ B = 0 condition in numeric solvers

When considering magnetic fields in computer simulations using the equations of magnetohydrodynamics (MHD) we have to take into account one of Maxwell's equations, namely:
∇⋅ B = 0
This is the condition that there is no divergence in the magnetic field, which in physical terms means that there are no magnetic charges. That is, we don't have particles floating about that create magnetic field lines. All of the magnetic flux that enters a given volume must leave the volume.

Mathematically this is usually straight forward (well sort of, except for those that have had to suffer through Jackson), but when it comes to computer simulations it turns out that this condition is very hard to preserve. There are several different methods used to preserve this condition and for those that are interested there is an excellent paper by Gábor Tóth that describes many of these methods (Gábor Tóth, Journal of Computational Physics, 161, 2, 2000, Pages 605-652).

While I am not going to discuss how these methods work, I will note that they are all imperfect. Because of the fundamental way simulations and computers work, there will always be some error, which means the divergence condition will never be met. The way of dealing with this usually involves making the error from the numerical solver to be as small as possible. While some ways of solving the MHD equations can make the error very small, it comes at the cost of making the code run slower (for example the projection method may be better than other methods in almost all situations, but it comes at the cost of a 20% increase in the time needed compared to the base method for they hydro equations. For reference, of the seven methods investigated by Tóth the next slowest only added 7% to the time).

But the question is, how do we test the accuracy of these different methods. Well, the easiest way is to have them solve a problem that we already know the answer for analytically and then see how well the code does at getting the correct answer. While many codes will do well with a simple problem, the idea is to get a very difficult problem and see how the code handles it.

One such problem is the Smooth Alfvén Wave test. This test takes a polarized magnetic field and has it propagate across the grid. Using this special set up it will create standing Alfvén waves. Because of the nature of the Alfvén waves and how they interact with the material, for this particular test the density should not change and the wave should not dissipate. That is, after one period the entire system should return to its original state. Any change in the density, energy, velocity or magnetic field is an indication of the errors introduced by the numeric solver. By knowing the initial conditions we can then calculate the change and therefore the error.

So in a real simulation, what would this look like? Well something like this:



This is a video of the Smooth Alfvén Wave test, with a circularly polarized magnetic field. This is a 2D simulation (well, 2.5D but anyway...) with a constant density slab of material with constant pressure. The wave propagates from the top right corner towards the bottom left. The color of the slab indicates the density. The initial density is a grey color and thus any other color you see in the slab indicates an error in the solver (there is a color legend in the video). The arrows indicate the velocity (and the color of the arrows does not mean anything, I just made them pink so that you could see them). The velocity points in the same direction as the magnetic field, so the arrows also show magnetic field. The test runs for five periods.

As I mentioned, any color, red or blue, you see in the slab indicates a numerical error, but while it make look like this was a bad simulation, note that the difference is only 4.3 e -4. This is still small and acceptable for most applications. If I ran it for longer, the errors would grow. If I started adding in other things, again the error would grow. In this case I can compare the result to the analytic solution, but in the case where there is no analytic solution we have to rely of either proven methods, better resolution or in the worst case, just assume that the errors are small (shudder).

This simulation was run in Athena, which uses a constrained transport method to address the ∇⋅ B = 0 problem, and it took about 3 minutes to run on my laptop. I rendered it in Paraview, which after taking a few minutes to set it up, it only took 2 or 3 minuets to render.

Sunday, May 9, 2010

Random Basketball Shooting (Explains High Nerd Score)

Today, after enjoying watching the Lakers go up 3-0 on the Jazz, I decided to go to the gym.  At UC Irvine, we have a nice track that borders a few basketball courts.  While running around the track, I watched the players miss some pretty lousy shots.

Anyways, I began wondering how often you would make a basket if you literally shot randomly.  I decided I had to know the answer so I went home and wrote a Monte Carlo code (in Python of course).  (Now I know why I got such a high nerd score.)

Here is the code:


As you can see from the code I put a player on a random spot on the floor from which he/she shoots a random shot.   I used this to get the details of the court size.  The units are in feet except the angle of the shot which is in radians.

I assume that the average random shot a person would make would have the distance of a typical "free throw" shot as it passes the the "10 foot plane" the rim sits at with a standard deviation of half that distance.    Furthermore, I don't take into account the advantage of the backboard or the infrequent shot off the wall into the rim.  However, this is deficiency (hopefully) is compensated by the rim being square instead of round (and therefore having more surface area) plus not allowing ball to "bounce out".

I then ran this code 100 times to get an error estimate.

And here is the results:  If you were to randomly shoot a ball in a basketball court a million times you should expect to make 681 +/- 26 (0.068%) out of the million in either basket and 342 +/- 19 (0.034%) in your own basket.

Therefore, if you are playing basketball and are not making ~ one out of every 1468 (unblocked) shots, (in either basket) there is something really wrong with you.  

Tuesday, December 15, 2009

Thoughts on “Climategate” from a Computational Scientist

If there is a more controversial topic in science than climate change I don’t know what it is. Even then most of the controversy isn’t really about the science - it’s about the politics, economics, and ideology related to the science. I have previously posted on how people’s opinions about climate change are influenced by non-scientific factors. For those of you that follow the news, you have heard a lot about the recent controversy over copies of stolen e-mails from the University of East Anglia, Britain’s main climate research center. These e-mails contain unprofessional comments about climate change skeptics, comments about data analysis “tricks”, and even a couple remarks about denying, delaying, or deleting public data requests. For a good summary of the whole mess, see this story from the New York times.

Dubbed “climategate” by conservatives, many have pointed to these e-mails as evidence that the science behind climate change is false. One writer has gone so far as to say that this is “the final nail in the coffin for global warming”. I am not a climate scientist, but I do work with climate scientists and I do work on large computer simulations of the sun which are somewhat analogous to the global climate models (GCM’s) upon which many future predictions about climate change are based. So here are my thoughts on this whole mess.
  1. Nothing that I have seen in these e-mails suggests that the science behind global warming has been faked. Anybody who has ever taken a math class has learned “tricks” for working problems. Computational science is full of simplified approximations that make these simulations possible. There are a lot of simplified approximations in climate science, but the overall consensus between many different models using many different approximations is that humanity is driving up atmospheric concentrations of CO2, which it turn warms the planet. Currently all of the major computational models agree that observed warming trends are largely due to anthropogenic forcing, so while one model would be questionable, the fact that all of them agree on this point is a pretty good indication that they’ve got it right.
  2. Most people have no idea how science is done. Science classes are filled with tidy little experiments that produce tidy little results that are always consistent with other experiments and never unexpected. Real science is full of ambiguous individual results that on their own lead to uncertain conclusions. But when real scientists see strong statistical trends in data over many different models and experiments, they can confidently draw correct conclusions. The general public (and many with political agendas) are often uncomfortable with the idea that unresolved details can be a part of a well-supported theory, so they either discard the unresolved details or the well-supported theory.
  3. Like many people, some scientists have big egos and lack social skills. On top of that, scientists have spent years learning the intricacies of their specific research topic. Add to that the fact that a large portion of the general public - including many politicians - are essentially scientifically illiterate and some scientists can at times feel justified in draconian measures like repressing dissension or limiting access to data to get the general public to agree with scientific facts. It’s not right, but at the same time I think think it comes more from frustration than some sort of politically motivated conspiracy.
  4. One thing that no one in science will stand for is bad science. If climate change could be disproved through rigorous scientific tests it would be the sort of result that would make the person that accomplished it an instant celebrity in the climate community. To borrow an example from astronomy, in the late 1990’s two independent teams of highly qualified astronomers released independent data showing that the expansion of the universe was accelerating. This destroyed 40 years of work in cosmology that assumed a decelerating universe - in some cases entire careers’ worth of work was rendered invalid overnight. There were a lot of skeptics, but the data was solid and the two teams measurements were independent. When the results were announced at a meeting of the American Astronomical Society they were met with a standing ovation. The point is the scientists as a whole are interested in the truth even if its not on their side of the argument. If there were conclusive evidence showing the global climate was not changing climatologists would embrace it. The reason most of them fight skeptics as hard as they do is that the data disproving climate change simply isn’t there. Instead all sorts of tests indicate that the climate is changing due to human activity.
So there are my thoughts. The e-mails don't disprove the science behind climate change, but they do lay bare some of the short-comings of both the general public and the scientific community.

Friday, September 18, 2009

Athena Magnetohydrodynamics (MHD) Code

Some of you (one of you) requested a little more information about the hydrodynamics code that I mentioned in a previous post. The code written in C is called Athena after the Greek goddess. The best basic overview of the code comes from the authors themselves:

"Athena is a grid-based code for astrophysical gas dynamics, based on higher-order Godunov methods, with nested and adaptive mesh capabilities. The original development efforts were supported by the NSF Information Technology Research (ITR) program. As part of the project, Athena will be made freely available to the astrophysics community, along with complete documentation and web-based training material.

The current version of the code (v3.1) implements algorithms for:

  • compressible hydrodynamics and ideal MHD in one-, two-, or three-spatial dimensions,
  • ideal gas equation of state with arbitrary γ (including γ = 1, an isothermal EOS).
  • first-, second- or third-order characteristic interpolation in the primitive variables,
  • numerical fluxes computed using a variety of approximate and/or linearized Riemann solvers,
  • source terms due to a static gravitational potential,
  • self-gravity computed using FFTs,
  • an arbitrary number of passive scalars advected with the flow,
  • parallelization using domain decomposition with MPI.
The code is being developed by Jim Stone (Princeton University), Tom Gardiner (Cray Research), Peter Teuben (University of Maryland), and John Hawley (University of Virginia). Code development began in 2000, and is on-going.

The primary motivations for developing Athena are to (1) adapt higher-order Godunov schemes for astrophysical MHD, (2) use a single-step Eulerian update so the solver can be used with adaptive mesh refinement, (3) optimize and parallelize the implementation on modern, distributed memory processors, and (4) distribute the code to the astrophysics community with documentation and web-based training tools. The first three motivations are driven by our own research needs, the last is done in the hope the community will find the resulting code useful for their own research."

So that is how the authors of the code describe the it and now onto my opinion of the code. I found it very easy to pick up and learn how to use. For those who download the code it comes with several preworked examples that demonstrate different aspects of the code. The examples include several standard test problems (spherical blast wave, linear waves and others with more obscure names, usually after the people who first published a paper on that problem). The code comes with a user's guide which contains most of what is needed to get the code up and running. I think that it took me less than a week from when I downloaded the code to when I got the first examples running. Keep in mind that I had also just bought a laptop two days before I downloaded the code and I was in the process of making a Linux partition and making sure that I also had all of the necessary libraries and compilers necessary to run the code. So I was not just dealing with getting the code up and running but also getting my computer up and running for that first week. After that it was a matter of working through some of the examples, changing the inputs and figuring out how it all worked.

On a basic level the code is very simple to run. It only takes three commands to make it run and in some cases it only takes one. For example if I wanted to run my particular problem that I wrote, which I named Starburst, from the command line I would type:

configure --with-problem=starburst --with-gas=hydro

make all

./athena -i ../tst/2D-hydro/starburst.c

The first line sets up the problem and in general has the form: "configure [--enable-feature] [--disable-feature] [--with-package=choice]". The second compiles the code (and does a few more things). The last line actually runs the code and calls the input file "starburst.c". The individual problem files include all of the code needed to make the initial conditions (density, pressure etc.) as well as functions to include a gravitational potential, and functions to update certain cells every cycle (to be used, for example, to inject matter and energy into the center of a galaxy), along with several more useful functions designed for other specific purposes to control output etc.

The code is very versatile and can run on many processors (or just one like my laptop). The individual features available which are used with the configure command are given in the user's manual (snapshot shown below, the bold options are the defaults, click to see a larger and easier to read version):

As you may notice it includes an MHD option which is what makes Athena so powerful, and well known. Most people I have talked to about Athena view it a primarily an MHD code though it is also used for normal hydro stuff. Right now I am using it as a hydro code, but the plan is to eventually use it with MHD.

So overall the code is very easy to learn how to use and to get working. It is another matter (haha) to make it do something different, new and useful (preferably all three), and above all publishable. I would recommend using this code if you are looking into using a standard, ready to run out of the box code for hydrodynamics or MHD. I will now take questions.

Thursday, August 13, 2009

What I have been doing this summer

So I started out this summer by being a TA which went well (I haven't received any death threats from my students). The rest of my summer has been focused on research, specifically learning how to model things on the computer. As of right now I have two co-advisors, one who focuses on the computation aspect of our project, and the other on the observation aspect. I am supposed to be the one that translates between the two and keeps them both up to speed on what the other is doing. In the process I learn both computation and observation (and maybe some instrumentation). So far I am learning a lot and the project I am working on seems fun.

We are focusing on two galaxies to get a good idea of what is happening in their cores in order to understand AGN and galactic blowout. What is galactic blowout? That can best be explained with a picture. This picture was taken using Hubble Space Telescope by my advisor Gerald Cecil a while back. It is an image of central region of NGC 3079.You can see the expanding bubble of gas in the middle. It is this expanding bubble that we are interested in. Notability the curvature of the filaments the source of the bubble and the circumstances that made it form. My job is to figure out a way to model this as realistically as possible.

For that I have been learning how to use a hydrodynamics code called Athena. There were other codes I could have used (like VH-1 for example) but my other advisor Fabian Heitsch recommended Athena for this type of project. So I downloaded Athena and went to work figuring out how to use it. I would say that it is a very well written code (of the little that I have used it) and that it is very intuitive and easy to learn (it's written in C). Right now I have been making up some toy models to get a good sense of how it works and what I can do with it. Recently (this week) I have been working on getting the data out and into an interesting format (one that I can show people and wow them with). On that note I have a short video for you guys.

What I have here is an extremely simple (emphasis on extremely, and simple) model of the disk of a galaxy (a constant density disk with exponential fall off to halo densities, not very realistic but for now it works). I have insterted a "starburst" in the center with an over-pressure region (luminosity/supernova) along with some wind (i.e. kinetic energy, matter outflow). The computation is only doing 2D, and is small enough that I am running it on my laptop.

Here the x-y plane represents the computaion grid (250 grid points in the x, 500 in the y). The z axis is density. I took the output from Athena and fed it into MATLAB and turned it into a movie that I am posting here. I hope you enjoy. When I have real stuff (with good physical interpretation) I will post about that.