CGNS takes the awkwardness out of cfd

CGNS takes the awkwardness out of cfd

Vijai Kumar Suriyababu's photo
Vijai Kumar Suriyababu

Published on Jul 18, 2015

7 min read

Listen to this article

If you are a scientific programmer who writes code related to computational fluid dynamics, you really should rethink your whole stack. I am not trying to push my ideas on you / endorsing any commercial products. I have no interest in doing that unless some company is willing to pay to me do it. But I would like to shed light on something fundamental yet essential in computational fluid dynamics.

I will start with data in general that goes in and comes out of CFD codes. There are a whole bunch of data formats used by CFD code developers. Some of them are as follows.

  • .txt or .dat files (the good old write data in columns)

  • .csv files (relatively good option)

  • Tecplot output files (meh.. never in a million years would I care for Tecplot or take it seriously)

  • NetCDF

  • .npy (you would know this if you are a numpy guy)

  • hdf5

When you are young and starting, you might find Tecplot or the good old DAT/CSV to be very comfortable, and that is okay. But when it comes to learning anything, it is always better if you learn to do things the correct way from the very beginning. Let us say you start writing a two-dimensional unstructured CFD code and for meshing, you use your code. It would be really fun to write for learning. When you are done validating your code for a couple of cases, say a wedge and a cylinder as follows.

The above meshes can be generated using a program and then used for the sake of learning. But if you wanted to study some other complex geometry in future, you would have to go through the trouble of doing things all over again, and it would cost you more time than developing the actual algorithms. This causes a danger of you wasting almost twice as much time simply because you were too stubborn to adopt a new standard for your code. That standard is a library called CFD General Notation System or shortly called CGNS. CGNS is a set of standards developed by NASA based on one of the best scientific data formats known as hdf5. I will not go into detail about hdf5 because that is coming separately in yet another big blog post. But the above graphic gives a bunch of reasons on what draws people to their platform (yes! I stole the picture from there), and i strongly encourage you to read online if you are interested in learning more about hdf5.

So we have established that CGNS is a set of fancy standards that sits on top of hdf5 yet I haven’t explained how it could be useful for you. The great thing about CGNS is that almost every commercial and opensource mesh generator supports it. This means that you can use complex, multiblock, unstructured meshes with easy in your CFD programs with a simple modification in the code. Even stanford’s $SU²$ supports CGNS format. Hence it is straightforward to set up complex simulations based on their aerodynamic optimization tools and benefit at ease. In the end, your time is important than anything. Apart from meshing, you can write the output files in CGNS format too, and that is supported by almost every single commercial and post-processing software available out there.

Now the million-dollar question is if the idea is sold yet? I have a simple example for you to understand things a little better.

      include 'cgnslib_f.h'
c   open CGNS file for read-only  
      call cg_open_f('grid.cgns',CG_MODE_READ,index_file,ier)
      if (ier .ne. CG_OK) call cg_error_exit_f
c   we know there is only one base (real working code would check!)  
c   we know there is only one zone (real working code would check!)  
c   get zone size (and name - although not needed here)  
      call cg_zone_read_f(index_file,index_base,index_zone,zonename,
     + isize,ier)
c   lower range index  
c   upper range index of vertices  
c   read grid coordinates  
      call cg_coord_read_f(index_file,index_base,index_zone,
     + 'CoordinateX',RealSingle,irmin,irmax,x,ier)
      call cg_coord_read_f(index_file,index_base,index_zone,
     + 'CoordinateY',RealSingle,irmin,irmax,y,ier)
      call cg_coord_read_f(index_file,index_base,index_zone,
     + 'CoordinateZ',RealSingle,irmin,irmax,z,ier)
c   close CGNS file  
      call cg_close_f(index_file,ier)

The above code reads a CGNS mesh into a FORTRAN program. Once you have the grid data in your arrays, you can concentrate more on your algorithms and not worry about developing codes for mesh generation (unless your research work is on grid generation). The same goes for the solution too. You need not spend a lot of time getting things to work on your commercial plotting software. Since all post-processors support CGNS, you could call the CGNS API and have it write them as CGNS files using the HDF5 data format.

I will post some useful and real-world examples in a week or so. So please check back on this post after some time and keep an eye on my Github. It is vijaiaeroastro.

Share this
Proudly part of