snapshotinterface.h 8.43 KB
Newer Older
jcl's avatar
jcl committed
1 2 3 4 5 6 7 8 9 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2008-2010                                       
// e-mail:   Jean-Charles.Lambert@oamp.fr                                      
// address:  Dynamique des galaxies                                            
//           Laboratoire d'Astrophysique de Marseille                          
//           Pole de l'Etoile, site de Chateau-Gombert                         
//           38, rue Frederic Joliot-Curie                                     
//           13388 Marseille cedex 13 France                                   
//           CNRS U.M.R 6110                                                   
// ============================================================================

// ============================================================================
// plugins mecanism
//
// trySnaphot
//    snapshot = new CSnaphot
//    if (snapsht->isValidDtata()) then Ok
//
//    CSnapshot constructor must validate with "valid" variable
//
//
//
//

/**
   @author Jean-Charles Lambert <Jean-Charles.Lambert@oamp.fr>
*/
#ifndef SNAPSHOTINTERFACE_H
#define SNAPSHOTINTERFACE_H
#include "componentrange.h"
#include "userselection.h"
#include "ctools.h"
//#include "uns.h"
#include <vector>
#include <iostream>
#include <cmath>
#include <assert.h>

#define MAX_EPS 5

namespace uns {
  class CSelectTime;
  typedef std::vector<CSelectTime> CSelectTimeVector;

  //                             
  // Class to store selected time
  //                             
  class CSelectTime {
  public:
    CSelectTime(const float _i, const float _s, 
		const float _o, const float _l):inf(_i),sup(_s),offset(_o),lastt(_l) {
    };
    const CSelectTime & operator=(const CSelectTime& m) {
      inf=m.inf;
      sup=m.sup;
      lastt=m.lastt;
      offset=m.offset;
      return *this;
    }
    float inf,sup,offset,lastt;
  };
  //                                     
  // class to store virtual snapshot data
  //                                     
  class CSnapshotInterfaceIn {

  public:
    CSnapshotInterfaceIn() {};
    // READING constrcuctor
    CSnapshotInterfaceIn(std::string _name, const std::string _comp, 
                       const std::string _time, const bool verb=false) {

      filename = _name;
      simdir = "";
      select_part = _comp;
      select_time = _time;
      obj=NULL; pos=NULL; vel=NULL;
      mass=NULL;
      end_of_data=false;
      verbose = verb;
      first=true;
      valid=false;
83 84 85
      req_bits=0;  // data requested
      load_bits=0; // data loaded
      comp_bits=0; // component requested
jcl's avatar
jcl committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      crvs=NULL;
      crv.clear();
      stv.clear();
      parseSelectTime();
      
    };
    virtual ~CSnapshotInterfaceIn() { crv.clear(); stv.clear();};
    // ---------------------------------------------------
    // READING operations
    // Pure Virtual functions, *** MUST be implemented ***
    // ---------------------------------------------------

    virtual ComponentRangeVector * getSnapshotRange() = 0;    
    // index_tab = array of selected indexes (size max=part_data->nbody)
    // nsel      = #particles selected in index_tab                     
    // particles not selected must have the value '-1'                  
    virtual int nextFrame(uns::UserSelection &)= 0;     
    virtual int close() = 0;
    virtual bool getData(const std::string,int *,float **)=0;
    virtual bool getData(const std::string,      float * )=0;
    virtual bool getData(const std::string,int *,int   **)=0;
    virtual bool getData(const std::string,      int   * )=0;
    virtual bool getData(const std::string, const std::string ,int *,float **)=0;
    virtual bool getData(const std::string, const std::string ,int *,int   **)=0;
    
    
    // Virtual function with a default behaviour
    virtual std::string getInterfaceType() { return interface_type;}
    virtual std::string getFileStructure() { return file_structure;}
    virtual bool isEndOfData() const { return end_of_data;}
    virtual std::string getFileName() { return filename;}
    virtual std::string getSimDir() { return simdir;}
    virtual int  getNSel() { return nsel;}
    virtual void setNsel(const int _nsel) { nsel = _nsel;}
    virtual bool isNewFrame() { return true;}
    virtual int nextFrame(std::string bits="");//mxvpaekXRMAHIU");
    virtual ComponentRangeVector * getCrvFromSelection() { return user_select.getCrvFromSelection();}
    virtual bool shift(std::string,const float x, const float y, const float z);
    virtual float   getEps(const std::string) { return -1.;}
    virtual int     getCod(const std::string select, const float time, 
			   float * tcod, const std::string base="ANALYSIS/cod",
			   const std::string ext="cod") { 
      if ((select=="")?0:1) {;};
      if (time) {;}
      if (tcod) {;}
      if ((base=="")?0:1) {;}
      if ((ext=="")?0:1) {;}
      return -1;
    }
    virtual void setReqBits(const unsigned int bits) { req_bits = bits;}
    
    // normal functions        
    bool isValidData() { return valid; }
    void setFileName(std::string _f) { filename = _f;}
    bool getRangeSelect(const char *, int *, int *, int * , bool fortran=false);
    //std::string getFileName() const { return filename;};
    int getInterfaceIndex() { return interface_index; }
    bool isFileExist() { return true; }
    std::string getSelectPart() { return select_part; }
    std::string getSelectTime() { return select_time; }
    int nbody_first;
    float time_first, time;
    ComponentRangeVector crv_first;
    UserSelection user_select; // object to store user component selection
  protected:
    // READING    
    int nsel;
    
    CSnapshotInterfaceIn * obj;
    std::string filename;
    std::string simdir;
    std::string interface_type, file_structure;
    int interface_index;
    mutable bool end_of_data;
    std::string select_part, select_time;
    bool keep_all, load_vel;
    ComponentRangeVector crv, * crvs;
    float * pos, *vel, *mass;
    bool first,valid;
    static std::string sim_db_file;
    static std::string eps_db_file;
    static std::string nemo_range_file;

169 170 171 172 173 174 175 176
    unsigned int load_bits, comp_bits;
    inline bool ckloadBit(unsigned int lb) { return load_bits & lb; }
    template <class T> inline void freeNotLoadedData(T ** data,unsigned int lb) {
      if (!ckloadBit(lb) && *data) {
        delete [] *data;
        *data=NULL;
      }
    }
jcl's avatar
jcl committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    unsigned int req_bits;
    void computeBits(std::string _s="");
    CSelectTimeVector stv;
    void parseSelectTime();
    inline bool diffTime(const float t,const float fuzz=0.000001) {
      return ((fabs(t)<fuzz)?true:false);
    }
    virtual int nextFrameSelect(ComponentRangeVector * crvs);
    void getRangeTime(std::string);
    bool checkRangeTime(const float);
    float eps[MAX_EPS]; // gas, halo, disk, bulge, stars
    
    bool verbose; // display some info if verbose
  };  // end of class CSnapshotInterfaceIn


  class CSnapshotInterfaceOut {
  public:
    // WRITING constructor
    CSnapshotInterfaceOut(const std::string _n, const std::string _t, const bool _verb=false) {
      simname    = _n;
      simtype    = tools::Ctools::tolower(_t);
      verbose    = _verb;
    }
    virtual ~CSnapshotInterfaceOut() {};
    // ---------------------------------------------------
    // WRITING operations
    // Pure Virtual functions, *** MUST be implemented ***
    // ---------------------------------------------------
    virtual int setHeader(void * ) = 0;
    virtual int setNbody(const int) = 0;
    virtual int setData(std::string, float) = 0;
    virtual int setData(std::string, const int,  float *,const bool _addr=false)=0;
    // array by double keys
    // like "halo" "pos" => position of the halo requested
    virtual int setData(std::string, std::string, const int , float *,const bool _addr=false)=0;
    virtual int setData(std::string, std::string, const int , int   *,const bool _addr=false)=0;
    virtual int setData(std::string, const int , 
		float *, float *, float *,const bool _addr=false)=0;
    virtual int setData(std::string, const int, int *,const bool _addr=false)=0;
    virtual int save()=0;
    virtual std::vector<double> moveToCom()=0;
    std::string getInterfaceType() { return interface_type;}
    std::string getFileStructure() { return file_structure;}
    virtual int close() {
      return 1;
    }
  protected:
    // WRITING
    std::string simname, simtype;
    std::string interface_type,file_structure;
    bool verbose;
    
  }; // end of class CSnapshotInterfaceOut
} // namespace



#endif