uns_info.cc 9.3 KB
Newer Older
jclamber's avatar
jclamber committed
1
// ============================================================================
2
// Copyright Jean-Charles LAMBERT - 2010-2016
jclamber's avatar
jclamber committed
3 4 5
//           Centre de donneeS Astrophysiques de Marseille (CeSAM)
// e-mail:   Jean-Charles.Lambert@lam.fr                                      
// address:  Aix Marseille Universite, CNRS, LAM 
jclamber's avatar
jclamber committed
6 7 8 9
//           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
10
//           CNRS UMR 7326                                       
jclamber's avatar
jclamber committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// ============================================================================
#include <iostream>                                   // C++ I/O     
#include <fstream>                                    // C++ file I/O
#include <sstream>
#include <cstdio>  
#include <cstdlib> 
#include <assert.h>
#include <cmath>
#include <nemo.h>
#include <iomanip>

// ------------------------------------------------------------
// Include file
#include "uns.h"

// ------------------------------------------------------------
// Nemo variable
const char * defv[] = {
  "in=???\n		      UNS input snapshot",  
  "select=all\n               select particles (range, or component name)\n"
  "                   component: gas,halo,disk,bulge,stars,bndry",
  "display=t\n                display array's content (t|f)",
  "bits=\n         (default: all , but you cat set \"mxvpXRIUMAHT\" physicals quantities that you want to display\n",
34
  "float=t\n        real format used, t=float, f=double",
jclamber's avatar
jclamber committed
35 36 37 38 39 40 41 42
  "maxline=2\n                max lines per components",  
  "times=all\n		      selected time",
  "verbose=f\n                verbose on/off",
  "VERSION=2.1\n              compiled on <"__DATE__"> JCL  ",
  NULL,
};
const char * usage="Print information about an UNS file";
using namespace std;
43
template <class T> void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn2<T> * uns);
jclamber's avatar
jclamber committed
44
template <class T> void displayFormat(int maxlines,std::string text, T * array, int dim, int size, int np);
jclamber's avatar
jclamber committed
45
bool component_exist=false;
jclamber's avatar
jclamber committed
46 47
// ------------------------------------------------------------
//  displayInfo
48
template <class T> void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn2<T> * uns)
jclamber's avatar
jclamber committed
49
{
50
  T * pos, * vel, * mass, * pot , *acc, *eps;
jclamber's avatar
jclamber committed
51 52 53 54
  int * id;
  int nbody=0;
  bool ok=false;

55
  T * nullp;
jclamber's avatar
jclamber committed
56
  ok = uns->snapshot->getData(comp,"nbody" ,&nbody,&nullp);
jclamber's avatar
jclamber committed
57
  if (ok) component_exist=true;
jclamber's avatar
jclamber committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  if (ok) {
    std::cout << setw(50) << setfill('=') << ""<<"\n";
    std::cout<< setfill(' ');
    std::cout<< left<< setw(8) << comp << ":" << setw(9) << right << nbody << "\n";
  }
  ok = uns->snapshot->getData(comp,"mass",&nbody,&mass);
  if (ok && display) {
    displayFormat(maxlines,"mass[1] = ",mass,1,nbody, 3);
  }  
  ok = uns->snapshot->getData(comp,"pos" ,&nbody,&pos );
  if (ok && display) {
    displayFormat(maxlines,"pos [3] = ",pos ,3,nbody, 1);
  }
  ok = uns->snapshot->getData(comp,"vel" ,&nbody,&vel );
  if (ok && display) {
    displayFormat(maxlines,"vel [3] = ",vel ,3,nbody, 1);
  }
  ok = uns->snapshot->getData(comp,"pot" ,&nbody,&pot );
  if (ok && display) {
    displayFormat(maxlines,"pot [1] = ",pot ,1,nbody, 3);
  }
79 80 81 82
  ok = uns->snapshot->getData(comp,"eps" ,&nbody,&eps );
  if (ok && display) {
    displayFormat(maxlines,"eps [1] = ",eps ,1,nbody, 3);
  }
jclamber's avatar
jclamber committed
83 84 85 86 87 88 89 90 91
  ok = uns->snapshot->getData(comp,"acc" ,&nbody,&acc );
  if (ok && display) {
    displayFormat(maxlines,"acc [3] = ",acc ,3,nbody, 1);
  }
  ok = uns->snapshot->getData(comp,"id"  ,&nbody,&id);
  if (ok && display) {
    displayFormat(maxlines,"id  [1] = ",id  ,1,nbody, 3);
  }  
  //if (comp == "gas") {
92
  T * rho, * u, * hsml, * temp, * metal;
jclamber's avatar
jclamber committed
93
  ok = uns->snapshot->getData(comp,"rho" ,&nbody,&rho );
94

jclamber's avatar
jclamber committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  if (ok && display) {
    displayFormat(maxlines,"rho [1] = ",rho ,1,nbody, 3);
  }
  ok = uns->snapshot->getData(comp,"u"   ,&nbody,&u );
  if (ok && display) {
    displayFormat(maxlines,"u   [1] = ",u   ,1,nbody, 3);
  }
  ok = uns->snapshot->getData(comp,"hsml",&nbody,&hsml);
  if (ok && display) {
    displayFormat(maxlines,"hsml[1] = ",hsml,1,nbody, 3);
  }
  ok = uns->snapshot->getData(comp,"temp",&nbody,&temp);
  if (ok && display) {
    displayFormat(maxlines,"temp[1] = ",temp,1,nbody, 3);
  }
  ok = uns->snapshot->getData(comp,"metal",&nbody,&metal);
  if (ok && display) {
    displayFormat(maxlines,"metal[1] = ",metal,1,nbody, 3);
  }    
  //}
  //if (comp == "stars") {
116
  T * age;//, * metal;
jclamber's avatar
jclamber committed
117 118 119 120
  ok = uns->snapshot->getData(comp,"age" ,&nbody,&age );
  if (ok && display) {
    displayFormat(maxlines,"age [1] = ",age,1,nbody, 3);
  } 
121
  T * im;
jclamber's avatar
jclamber committed
122 123 124 125
  ok = uns->snapshot->getData(comp,"im" ,&nbody,&im );
  if (ok && display) {
    displayFormat(maxlines,"im [1] = ",im,1,nbody, 3);
  }
126
  T * ssl;
jclamber's avatar
jclamber committed
127 128 129 130
  ok = uns->snapshot->getData(comp,"ssl" ,&nbody,&ssl );
  if (ok && display) {
    displayFormat(maxlines,"ssl [1] = ",ssl,1,nbody, 3);
  }
131
  T * zs,* zsmt;
jclamber's avatar
jclamber committed
132 133 134 135 136 137 138 139 140 141 142 143 144
  int czs, czsmt, nzs, nzsmt;
  ok = uns->snapshot->getData(comp,"zs" ,&nzs,&zs );
  if (ok && display) {
    ok = uns->snapshot->getData("czs"   ,&czs );
    std::cerr << "nzs="<< nzs<<" czs ="<<czs<<"\n";
    displayFormat(maxlines,"zs [1] = ",zs,1,nzs, 3);
  }
  ok = uns->snapshot->getData(comp,"zsmt" ,&nzsmt,&zsmt );
  if (ok && display) {
    ok = uns->snapshot->getData("czsmt"   ,&czsmt );
    std::cerr << "nzsmt="<< nzsmt<<"  czsmt ="<<czsmt<<"\n";
    displayFormat(maxlines,"zsmt[1] = ",zsmt,1,nzsmt, 3);
  }
145
  T * cm; int ncm;
jclamber's avatar
jclamber committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  ok = uns->snapshot->getData(comp,"cm" ,&ncm,&cm );
  if (ok && display) {
    displayFormat(maxlines,"cm [1] = ",cm,1,ncm, 3);
  }
//  ok = uns->snapshot->getData(comp,"metal" ,&nbody,&metal );
//  if (ok && display) {
//    displayFormat(maxlines,"metal[1] = ",metal,1,nbody, 3);
//  } 
  //}
}
// ------------------------------------------------------------
// displayFormat
template <class T>  void displayFormat(int maxlines,std::string text, T * array, int dim, int size, int np)
{
  std::cout << scientific << left << setw(11) << text;
  // First line
  for (int k=0;k<std::min(size,np);k++) {
    for (int j=0;j<dim;j++) {
      std::cout << array[k*dim+j] << " ";
    }
  }
  std::cout << "\n";
  // other lines
jclamber's avatar
jclamber committed
169
  for (int i=1; i<std::min(maxlines,size/min(size,np)); i+=min(size,np)) {
jclamber's avatar
jclamber committed
170 171 172 173 174 175 176 177 178 179 180
    std::cout << left << setw(11) << "";
    for (int k=0;k<std::min(size,np);k++) {
      for (int j=0;j<dim;j++) {
        std::cout << array[(k+(i*np))*dim+j] << " ";
      }
    }
    std::cout << "\n";
  }  
  std::cout << left << setw(11) << "" << ". . .\n";
}
// ------------------------------------------------------------
181 182
// start program
template <class T> void start()
jclamber's avatar
jclamber committed
183
{
184

jclamber's avatar
jclamber committed
185 186 187 188 189 190 191 192 193 194 195
  // get input parameters
  char * simname     = getparam ((char *) "in"      );
  char * select_c    = getparam ((char *) "select"  );
  bool   display     = getbparam((char *) "display" );
  std::string bits   =(getparam ((char *) "bits"    ));
  int    maxlines    = getiparam((char *) "maxline" );
  char * select_t    = getparam ((char *) "times"   );
  bool   verbose     = getbparam((char *) "verbose" );  
  
  // -----------------------------------------------
  // instantiate a new UNS input object (for reading)
196 197
  uns::CunsIn2<T> * uns = new uns::CunsIn2<T>(simname,select_c,select_t,verbose);

jclamber's avatar
jclamber committed
198 199 200 201 202 203 204 205
  if (!display) bits="none"; // we don't read anything
  if (uns->isValid()) { // input file is known by UNS lib        
    while(uns->snapshot->nextFrame(bits)) { // there is a new frame
      std::string stype = uns->snapshot->getInterfaceType();
      std::string file_structure=uns->snapshot->getFileStructure();
      std::cout << setw(50) << setfill('*') << ""<<"\n";
      std::cout << "File name : "<<uns->snapshot->getFileName()<<"\n";
      std::cout << "File type : "<<stype<<"\n";
206 207
      int nbody; T time;
      //std::vector<T> vrect=uns->snapshot->getData("","/PartType1/Coordinates");
jclamber's avatar
jclamber committed
208 209 210 211 212 213
      // get the input number of bodies according to the selection
      uns->snapshot->getData("nsel",&nbody);
      // get the simulation time
      uns->snapshot->getData("time",&time);

      std::cout << "Nbody selected = " << nbody << "\nTime="<<time <<"\n";
214 215 216

      if (nbody >0) {
        if (0 && file_structure=="range") {
jclamber's avatar
jclamber committed
217
          displayInfo(display,maxlines,"all",uns);
218 219 220 221 222 223 224 225 226 227 228
        } else {
          component_exist=false;
          displayInfo(display,maxlines,"gas"  ,uns);
          displayInfo(display,maxlines,"halo" ,uns);
          displayInfo(display,maxlines,"disk" ,uns);
          displayInfo(display,maxlines,"bulge",uns);
          displayInfo(display,maxlines,"stars",uns);
          displayInfo(display,maxlines,"bndry",uns);
          if (!component_exist) { // no comp, diplay all
            displayInfo(display,maxlines,"all",uns);
          }
jclamber's avatar
jclamber committed
229
        }
jclamber's avatar
jclamber committed
230 231 232 233
      }
    }
  }
  delete uns;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

}
//------------------------------------------------------------------------------
//                             M   A   I   N
//------------------------------------------------------------------------------
int main(int argc, char ** argv )
{
  //   start  NEMO
  initparam(const_cast<char**>(argv),const_cast<char**>(defv));
  if (argc) {;} // remove compiler warning :)
  bool single  =(getbparam ((char *) "float"  ));

  if (single) start<float>();
  else        start<double>();

jclamber's avatar
jclamber committed
249 250
  //   finish NEMO
  finiparam();
251

jclamber's avatar
jclamber committed
252 253
}
// ------------------------------------------------------------