unsio_demo_1.cc 7.64 KB
Newer Older
jcl's avatar
jcl committed
1 2
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2010                                       
jclamber's avatar
jclamber committed
3 4
// e-mail:   Jean-Charles.Lambert@lam.fr                                      
// address:  Aix Marseille Universite, CNRS, LAM 
jcl's avatar
jcl committed
5 6 7 8
//           Laboratoire d'Astrophysique de Marseille                          
//           Pole de l'Etoile, site de Chateau-Gombert                         
//           38, rue Frederic Joliot-Curie                                     
//           13388 Marseille cedex 13 France                                   
jclamber's avatar
jclamber committed
9
//           CNRS UMR 7326                                       
jcl's avatar
jcl committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// ============================================================================
#include <iostream>                                   // C++ I/O     
#include <fstream>                                    // C++ file I/O
#include <sstream>
#include <cstdio>                   
#include <cstdlib>                  
#include <assert.h>
#include <cstdio>
#include <iomanip>
#include "uns.h"

#define _vectmath_h // put this statement to avoid conflict with C++ vector class
extern "C" {
#include <nemo.h>                                     // NEMO basics
  int io_nemo(const char *, const char *,...);
}

using namespace std; // prevent writing statment like 'std::cerr'

//------------------------------------------------------------------------------
//                             M   A   I   N                                    
//------------------------------------------------------------------------------
// NEMO parameters
const char * defv[] = {  // use `::'string because of 'using namespace std'
  "in=???\n           input file (gadget|nemo)        ",
  "out=???\n          output file                     ",
  "select=???\n       component selected (disk,stars,halo,gas,range)",
  "type=gadget2\n     type of the output file  (gadget2|nemo)",
38
  "single=t\n         t=float, f=double",
jcl's avatar
jcl committed
39 40 41 42 43 44 45 46 47 48 49 50
  "time=all\n         selected time                   ",
  "verbose=f\n        verbose mode                    "
  "VERSION=1.0\n       compiled on <"__DATE__"> JCL   ",
  NULL
};
const char * usage="test unsio library";

//------------------------------------------------------------------------------
// processComponent
// read pos,vel,mass of the components
// if component exist AND it has been selected, then respecting comp's data are
// prepared to be saved
51
template <class T> void processComponent(std::string comp, uns::CunsIn2<T> * uns,uns::CunsOut2<T> * unsout)
jcl's avatar
jcl committed
52
{
53
  T * pos, * vel, * mass;
jcl's avatar
jcl committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  int * id;
  int n1,n2,n3,n4;
  bool ok1,ok2,ok3,ok4;


  ok1 = uns->snapshot->getData(comp,"pos" ,&n1,&pos );
  ok2 = uns->snapshot->getData(comp,"vel" ,&n2,&vel );
  ok3 = uns->snapshot->getData(comp,"mass",&n3,&mass);
  
  if (ok1 && ok2 && ok3) {
    assert(n1==n2);
    assert(n1==n3);
    std::cerr << "--> "<< std::left << std::setfill('.')<<
        std::setw(8) << comp << ":" << std::setfill(' ')<<
        std::right   << std::setw(10) << n1 <<"\n";
    unsout->snapshot->setData(comp,n1,mass,pos,vel,false);
    
  }
  ok4 = uns->snapshot->getData(comp,"id"  ,&n4,&id  );
  if (ok4) unsout->snapshot->setData(comp,"id",n4,id,false);
74
  T * rho;
jcl's avatar
jcl committed
75 76
  ok4 = uns->snapshot->getData(comp,"rho"  ,&n4,&rho  );
  if (ok4) unsout->snapshot->setData(comp,"rho",n4,rho,false);
77
  T * hsml;
jcl's avatar
jcl committed
78 79 80 81
  ok4 = uns->snapshot->getData(comp,"hsml"  ,&n4,&hsml  );
  if (ok4) unsout->snapshot->setData(comp,"hsml",n4,hsml,false);
  
}
82 83 84 85 86 87 88 89 90
std::string simname;
std::string outname;
std::string type    ;
std::string select_c;
std::string select_t;
bool        single;
bool        verbose;


jcl's avatar
jcl committed
91
//------------------------------------------------------------------------------
92 93
//template <class T> void start(uns::CunsIn2<T> * uns,uns::CunsOut2<T> * unsout)
template <class T> void start()
jcl's avatar
jcl committed
94 95 96
{
  // -----------------------------------------------
  // instantiate a new UNS input object (for reading)
97 98
  uns::CunsIn2<T> * unsin = new uns::CunsIn2<T>(simname,select_c,select_t,verbose);
  if (unsin->isValid()) { // input file is known by UNS lib
jcl's avatar
jcl committed
99 100 101 102 103 104
    int cpt=0;
    while(unsin->snapshot->nextFrame()) { // there is a new frame
      std::string itype = unsin->snapshot->getInterfaceType();
      std::cerr << "Input file is of type :"<<itype<<"\n";
      std::cerr << "Sim dir :"<<unsin->snapshot->getSimDir()<<"\n";
      bool ok;
105 106
      int cnbody,cfirst,clast,nbody;
      T time;
jcl's avatar
jcl committed
107 108 109 110 111 112 113 114 115 116 117
      // get the input number of bodies according to the selection
      ok=unsin->snapshot->getData("nsel",&nbody);
      // get the simulation time
      ok=unsin->snapshot->getData("time",&time);
      //
      std::cerr << "nbody=" << nbody << " time="<<time <<"\n";
      // OUTPUT operations
      // create an output filename : basename +  integer
      // example : myoutput.0 myoutput.1 ...... etc
      stringstream number;
      number << cpt++;
118
      std::string out_name = std::string(outname)+"."+number.str();;
jcl's avatar
jcl committed
119 120
      // -----------------------------------------------
      // Instantiate a UNS output snapshot (for writing)
121
      uns::CunsOut2<T> * unsout = new uns::CunsOut2<T>(out_name,type,verbose);
jcl's avatar
jcl committed
122 123 124 125 126 127 128 129 130 131 132
      // save time
      unsout->snapshot->setData("time",time);

      // processing
      processComponent("halo" ,unsin,unsout);
      processComponent("disk" ,unsin,unsout);
      processComponent("bulge",unsin,unsout);
      processComponent("stars",unsin,unsout);
      processComponent("bndry",unsin,unsout);
      processComponent("gas"  ,unsin,unsout);
      processComponent("all"  ,unsin,unsout); // only all particles selected
133

134
      T * extra=new T[10];
135 136 137 138
      for (int i=0; i<10; i++) {
        extra[i]=i*10.0;
      }
      unsout->snapshot->setData("EXTRA","bug",10,extra);
jcl's avatar
jcl committed
139
      // according to user's input request ("select" parameter)
140
      // check if gas component exist from input snapshot
jcl's avatar
jcl committed
141 142 143 144 145 146
      bool is_gas=unsin->snapshot->getRangeSelect("gas",&cnbody,&cfirst,&clast);
      if (( is_gas                    &&         // gas exist
          type      == "gadget2")     ||         // AND outype is gadget2
          (select_c == "gas" && type=="nemo" &&  // OR only gas and nemoout selected
           is_gas)) {                            // AND gas exist
        // Density
147
        T * rho;
jcl's avatar
jcl committed
148 149 150 151
        ok=unsin->snapshot->getData("rho",&cnbody,&rho);
        if (ok)  unsout->snapshot->setData("rho",cnbody,rho,false);

        // Hydro Smooth Length
152
        T * hsml;
jcl's avatar
jcl committed
153 154
        ok=unsin->snapshot->getData("hsml",&cnbody,&hsml);
        if (ok)   unsout->snapshot->setData("hsml",cnbody,hsml,false);
155

jcl's avatar
jcl committed
156
        // internal energy
157
        T * u;
jcl's avatar
jcl committed
158 159 160 161 162 163
        ok=unsin->snapshot->getData("u",&cnbody,&u);
        if (ok)  unsout->snapshot->setData("u",cnbody,u,false);
      }
      // for NEMO out only
      if (itype=="Nemo") {
        //processComponent("all" ,unsin,unsout);
164
      }
jcl's avatar
jcl committed
165 166
      // save snapshot
      unsout->snapshot->save();
167
      delete unsout; // remove object
jcl's avatar
jcl committed
168 169 170 171 172 173
    }
  } else {
    std::cerr << "Unknown UNS file format["<<simname<<"]\n";
  }
  delete unsin;
  //   finish NEMO
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

//------------------------------------------------------------------------------
// main
int main(int argc, char ** argv )
{
  //   start  NEMO
  initparam(const_cast<char**>(argv),const_cast<char**>(defv));
  if (argc) {;} // remove compiler warning :)
  
  // Get input parameters
  simname =(getparam ((char *) "in"      ));
  outname =(getparam ((char *) "out"     ));
  type    =(getparam ((char *) "type"    ));
  single  =(getbparam ((char *) "single"  ));
  select_c=(getparam ((char *) "select"  ));
  select_t=(getparam ((char *) "time"    ));
  verbose =getbparam((char *) "verbose" );
  
  if (single) start<float>();
  else        start<double>();
  

jcl's avatar
jcl committed
197 198 199 200 201
  finiparam();
}
// ----------- End Of [unsio_demo.cc] ------------------------------------