Skip to content

Addition of facility to read and write the entire grid to FITS file.#92

Merged
allegroLeiden merged 18 commits intolime-rt:masterfrom
allegroLeiden:grid2fits
Nov 8, 2016
Merged

Addition of facility to read and write the entire grid to FITS file.#92
allegroLeiden merged 18 commits intolime-rt:masterfrom
allegroLeiden:grid2fits

Conversation

@allegroLeiden
Copy link
Copy Markdown
Contributor

@allegroLeiden allegroLeiden commented Feb 19, 2016

Much of the additional material in this PR is in the module grid2fits.c. In this module are comprehensive and tested routines to read and write the data in the list of grid structs from/to a FITS file. One problem with doing this is that the grid list contains different amounts of information at differing places in the code. It may not always be desired to wait the full processing time before outputting the grid - for example if the user just wants to look at the distribution of the points. For this reason the various scenarios have been divided into 4 stages in which grid contains differing amounts of information. The details of this are described in a long comment at the start of grid2fits.c.

For the time being I have included only some fairly rudimentary support for actually writing the grid (i.e., making use of the new functionality), and none at all for reading it. A more comprehensive service should probably wait on discussion about the future design of LIME's i/o.

17 June 2016: I reworked this PR substantially. I've now done the best job I can to (i) make the IO interface generic and not tied absolutely to FITS, and (ii) to implement fully read-and-write capability for the grid data. For the 2nd of these I have not yet been able to read the data at stage 4 (i.e. after all the level populations have values for them) and feed that either to further iterations of solver or direct to ray-tracing. Before that can be done, there needs to be a much more orderly succession of operations preparatory and during the level solving, which I felt was a bit beyond the scope of the present PR.

A new module grid2fits.c has been added which contains code to do this. There
is also a new function processFitsError() in messages.c. The appropriate
necessary additions have been made to lime.h and the makefile.

Note I also made some cosmetic changes to the Makefile, mostly evening out
the tab counts in SRCS and OBJS lines.

So far I have not added any machinery by which the user can access the new
FITS i/o routines. The functionality of the task is unaltered. I just wanted
to add the bulk code first (which has already been tested elsewhere) and make
sure it all compiled before proceeding further.
Here I have added some places where the grid is written to file. Everything seems to work
ok, and the output values look sensible, but I have not yet done any exhaustive tests of
the values in the fits files.

Some new elements of the struct inputPars have been necessary. The default values of these
cause no fits output to be written. In fact there should be no changes in the way the
software functions with the standard model.c file.

I also added a new function printMessage() to messages.c and some lines in main.c which
print the number of threads used when this is more than 1.

The fits-read functionality remains unused. Adding the ability to read the grid from file
should perhaps wait on discussion among the developers about the future of the pregrid etc
machinery.
This commit covers numerous minor issues to get them out of the way before the big one
(or ones). A summary is as follows:

- Includes fix for issue lime-rt#131: where nmol was not being set for sink points in LTE.
- C++->C style comments in aux.c:levelPops().
- conv renamed nItersDone in aux.c:levelPops().
- Replaced 'if(fileName)' with 'if(fileName!=NULL)' in a few places in aux.c:levelPops().
- Replaced 'g' by 'gp' and 'm' by 'md' in numerous places.
- Number of density values returned is now counted (and used to set par->collPart) in
aux.c:parseInput() rather than grid.c:gridAlloc().
- Added warning in aux.c:parseInput() if could not find moldata file.
- Grid velocity now set at the same time as dens, abun etc in grid.c:readOrBuildGrid()
rather than in getVelosplines().
- New header file gridio.h with a couple of macros relating to file types.
- New structs molInfoType, gridInfoType to contain sizes of various fields in grid info
which has been read from file. (Ultimately these should be merged into a single struct which
contains all the grid data.)
- messages.c:done() renamed to printDone().
- photon.c:photon(): unused variable x[] removed.
- In smooth.c:smooth(): qhull() and distCalc() were called at the end of each iteration; now
they are called at the beginning of each. In grid.c:buildGrid() the calls to qhull() and
distCalc() which preceded the call to smooth() now succeed it. The functionality is
unchanged but it allows one to read a grid file at stage 1 and have the triangulation
performed by qhull() without perturbation of the grid point locations.
Velocity polynomial interpolation coefficients are now calculated with respect to an
independent variable d/ds-0.5 instead of d. The new variable is symmetrical about the
midpoint of the distance ds between grid points and so allows easy conversion of the
coefficients from one member of a pair of connected points to the other.

I took the opportunity to rename and rewrite functions as follows:
  - velospline.c:getVelosplines() renamed to calcInterpCoeffs() (same for _lin() versions).
  - photon.c:velocityspline() renamed to calcAvRelLineAmp() (same for _lin() versions).
  - Added photon.c:interpolate().
I replaced the NITERATIONS macro by a user-settable parameter par->nSolveIters. Mostly this
is to make it easier to iterate a previous population solution further, when this can
eventually be implemented.
This is a substantial commit, but it is hard to see how it could have been easily broken
into smaller ones. Here I have reworked the previous facility for IO to and from FITS-format
files. There is another, generic IO layer now implemented in a new module gridio.c, with
FITS relegated to a choie of file format. Hopefully this will make it easier if anybody else
wants to write an interface to HDF5 or a DB or whatever.

Some detail on the changes:
  - New pars par->gridInFile, par->gridOutFiles[] (user-settable) and par->writeGridAtStage[], par->dataStageI (not intended to be user settable).
  - New function grid.c:mallocAndSetDefaultGrid().
  - grid.c:buildGrid() rewritten and renamed readOrBuildGrid().
  - main.c changed to include the fix to lime-rt#130.
@smaret smaret self-assigned this Jul 6, 2016
@smaret smaret added this to the Release 1.6 milestone Jul 6, 2016
@smaret smaret modified the milestones: Release 1.7, Release 1.6 Jul 6, 2016
Also added data-stage flag macros. None of this stuff is used yet, I am just clearing the decks
before the big change.
There are two main changes here, although there are a lot of changed lines of code.

  - A generic i/o layer has been added, with choice of FITS just one of the (currently the
only) possibilities for the format of the disk files. Hopefully this will make it easier for
people to add interfaces to HDF5 or whatever at a later date.

  - The information which can legally be read from or written to the files has now been made
much more flexible. When reading the file, if a particular element of the disk struct, say
density, was not supplied, the task will automatically seek to fill in these missing values via
the standard route. The completion state of the grid struct is now indicated by bit flags
rather than a single 'stage' integer.
Code still seg faults. I'll commit this now and squash it after I fix things.
In addition, I further straightened out the basic structure. There is more work still to be
done, but it is becoming more acceptable.

I am not yet able to read in pre-calculated popluations and go straight to ray-tracing. This
will require reform to the way dust/knu values are stored (see lime-rt#151).
I also moved some IO-specific structs and function prototypes out of lime into a new header file grid2fits.h.

Some bug fixes also done.
@allegroLeiden
Copy link
Copy Markdown
Contributor Author

As soon as this completes its Travis check it will be ready to merge. I'll do that tomorrow if nobody objects.

@allegroLeiden allegroLeiden merged commit 91b891f into lime-rt:master Nov 8, 2016
@allegroLeiden allegroLeiden deleted the grid2fits branch February 1, 2017 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants