unsfwrapper.cc 34.8 KB
Newer Older
jcl's avatar
jcl committed
1
// ============================================================================
2
// Copyright Jean-Charles LAMBERT - 2008-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 
jcl's avatar
jcl 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                                       
jcl's avatar
jcl committed
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
// ============================================================================

#include "unsidentifier.h"
#include "userselection.h"
#include "ctools.h"
#include <iostream>
#include <assert.h>
#include <cstdlib>

#define DEBUG 0
#include "unsdebug.h"

namespace uns {
  extern "C" {
    // load protocol
    int uns_init_(const char *,const char *, const char * , const int l1, const int l2, const int l3);
    int uns_load_(const int *);
    int uns_load_opt_(const int *, const char * bits, const int l1);
    int uns_close_(const int *);
    int uns_close_out_(const int *);
    // load data
    int uns_get_nbody_( const int * id, int   * nbody            );
    int uns_get_time_ ( const int * id, float * timu             );
    int uns_get_redshift_ ( const int * id, float * rs           );
    int uns_get_pos_  ( const int * id, float * pos   , int * size);
    int uns_get_vel_  ( const int * id, float * vel   , int * size);
    int uns_get_mass_ ( const int * id, float * mass  , int * size);
    int uns_get_age_  ( const int * id , float * age  , int * size);
    int uns_get_metal_( const int * id , float * metal, int * size);
    int uns_get_metal_gas_( const int * id , float * metal, int * size);
    int uns_get_metal_stars_( const int * id , float * metal, int * size);
    int uns_get_range_( const int * id, const char * comp ,
			int * nbody, int * first, int * last, const int l1);
    int uns_get_eps_  ( const int * id, const char * comp, float * eps, const int l1);
    int uns_sim_type_ ( const int * id);
    void uns_sim_dir_ ( const int * id, char * simdir, const int l1);
    int uns_get_u_    ( const int * id , float * u  , int * size);
    int uns_get_temp_ ( const int * id , float * temp  , int * size);
    int uns_get_rho_  ( const int * id , float * rho  , int * size);
    int uns_get_hsml_ ( const int * id , float * hsml , int * size);
    int uns_get_cod_  ( const int * id, const char * select, const float * time,
		       float * tcod, const int l1);
    int uns_get_array_f_( const int * id, const char * comp, const char * tag, float * array , int * size, const int l1, const int l2);
    int uns_get_array_i_( const int * id, const char * comp, const char * tag, int   * array , int * size, const int l1, const int l2);
    int uns_get_value_f_ ( const int * id, const char * tag, float *  data, const int l1);
    int uns_get_value_i_ ( const int * id, const char * tag, int   *  data, const int l1);
57 58 59 60 61

    void uns_get_file_structure_(const int * id, char * filestruct, const int l1);
    void uns_get_interface_type_(const int * id, char * inter_type, const int l1);
    void uns_get_file_name_(const int * id, char * file_name, const int l1);

jcl's avatar
jcl committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    // save protocol
    int uns_save_init_(const char *,const char *, const int l1, const int l2);
    // save data
    int uns_set_nbody_  ( const int * id, int   * nbody            );
    int uns_set_time_   ( const int * id, float * timu             );
    int uns_set_array_f_( const int * id, const char * comp, const char * tag, float * array , int * size, const int l1, const int l2);
    int uns_set_array_i_( const int * id, const char * comp, const char * tag, int   * array , int * size, const int l1, const int l2);
    int uns_set_value_f_ ( const int * id, const char * tag, float *  data, const int l1);
    int uns_set_value_i_ ( const int * id, const char * tag, int   *  data, const int l1);
    int uns_set_pos_    ( const int * id, float * pos   , int * size);
    int uns_set_vel_    ( const int * id, float * vel   , int * size);
    int uns_set_mass_   ( const int * id, float * mass   , int * size);
    int uns_set_ncomp_  ( const int * id, const char * comp, int * nbody, const int l1);
    int uns_save_       ( const int * id);
  }

void getCrv(const int ident);
int getUnsvIndex(const int ident);
void checkFArray(const int size, const int nbody);
// ----------------------------------------------------------------------------
// C++ / Fortran interface implementation                                      
// ----------------------------------------------------------------------------
  uns::CunsIdentifierVector unsv;

static bool first=true;
static int ident=10;
ComponentRangeVector * crv;
uns::UserSelection user_select;

// ----------------------------------------------------------------------------
// uns_init_:                                                                  
// initialyse UNS object with a new snapshot                                   
// Input :                                                                     
// simname     -> snapshot's name or simulation's name                         
// select_comp -> selected components or range                                 
// select_time -> selected time                                                
// Output :                                                                    
int uns_init_(const char * _name,const char * _comp, const char * _time, const int l1, const int l2, const int l3 )
{
  first=false;
  int ret=0;
  std::string simname  = tools::Ctools::fixFortran(_name,l1,false);
  std::string sel_comp = tools::Ctools::fixFortran(_comp,l2,false);
  std::string sel_time = tools::Ctools::fixFortran(_time,l3,false);
LAMBERT Jean-charles's avatar
LAMBERT Jean-charles committed
106
  uns::CunsIn2<float> * uns = new uns::CunsIn2<float>(simname,sel_comp,sel_time,true);
jcl's avatar
jcl committed
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
  bool valid = uns->isValid();
  
  if (valid) {
    uns::CunsIdentifier * unsi = new  uns::CunsIdentifier();
    unsi->ident = ident++;
    unsi->obj   = uns;
    unsv.push_back(*unsi);
    ret = unsi->ident;
  }
  return ret;
}
// ----------------------------------------------------------------------------
// uns_load                                                                    
// load the next time steps of the snapshot with the idendity "ident           
// return value:                                                               
// -1 snapshot was not open                                                    
//  0 end of snapshot reached                                                  
//  1 sucessfull                                                               
int uns_load_(const int * ident)
{
  PRINT("uns_load ident requested : "<< *ident << "\n";);
  // look for "ident" exist in the already opened snapshots
  // return the index in the vector of -1 if false         
  int index=uns::CunsIdentifier::getUnsvIndex(*ident,&unsv);
  PRINT("index in UNSV ="<< index << "\n";);

  if (index >= 0) {
134
    uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    index = snap->nextFrame("");
  }

  return index;
}
// ----------------------------------------------------------------------------
// uns_load_opt
// this is an optimized version of uns_load.  From the second parameter you specify
// which data you want to load. It can speed up the loading
// load the next time steps of the snapshot with the idendity "ident           
// return value:                                                               
// -1 snapshot was not open                                                    
//  0 end of snapshot reached                                                  
//  1 sucessfull                                                               
int uns_load_opt_(const int * ident, const char * _bits, const int l1)
{
  PRINT("uns_load ident requested : "<< *ident << "\n";);
  // look for "ident" exist in the already opened snapshots
  // return the index in the vector of -1 if false         
  int index=uns::CunsIdentifier::getUnsvIndex(*ident,&unsv);
  PRINT("index in UNSV ="<< index << "\n";);
  std::string bits="";
  bits  = tools::Ctools::fixFortran(_bits,l1,false);

jclamber's avatar
jclamber committed
159
  //std::cerr << "uns_load_opt = [" << bits << "] index=["<<index<<"]\n";
jcl's avatar
jcl committed
160
  if (index >= 0) {
161
    uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    index = snap->nextFrame(bits);
  }

  return index;
}
// ----------------------------------------------------------------------------
// uns_close                                                                    
// close the UNS file with the identifier ident 
int uns_close_(const int * ident)
{
  PRINT("uns_close INPUT file ident requested : "<< *ident << "\n";)
  // look for "ident" exist in the already opened snapshots
  // return the index in the vector of -1 if false         
  int index=uns::CunsIdentifier::getUnsvIndex(*ident,&unsv);
  PRINT("index in UNSV ="<< index << "\n";)

  if (index >= 0) {
179
    uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
180
    snap->close();
181
    delete ((CunsIn2<float> *)unsv[index].obj);
jcl's avatar
jcl committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  }

  return index;
}
// ----------------------------------------------------------------------------
// uns_close_out                                                                    
// close the UNS (out) file with the identifier ident 
int uns_close_out_(const int * ident)
{
  PRINT("uns_close OUTPUT file ident requested : "<< *ident << "\n";)
  // look for "ident" exist in the already opened snapshots
  // return the index in the vector of -1 if false         
  int index=uns::CunsIdentifier::getUnsvIndex(*ident,&unsv);
  PRINT("index in UNSV ="<< index << "\n";)

  if (index >= 0) {
198
    uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
199
    snap->close();
200
    delete ((CunsOut2<float> *)unsv[index].obj);
jcl's avatar
jcl committed
201 202 203 204 205 206 207 208 209 210
  }

  return index;
}
// ----------------------------------------------------------------------------
// uns_get_array_f:                                                                
// return component's array belonging to the simulation with the identifier ident         
int uns_get_array_f_( const int * id, const char * _comp, const char * _tag, float * array , int * size, const int l1, const int l2)
{
  int index=getUnsvIndex(*id);
211
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  // populate user_select object according to the selection
  // and the object
  std::string tag  = tools::Ctools::fixFortran(_tag,l2);
  std::string comp = tools::Ctools::fixFortran(_comp,l1);  
  float *data;
  int nbody;
  bool ok=snap->getData(comp,tag,&nbody,&data);
  int status=0;
  if (ok) {
    int dim=1;
    if (tag=="pos" || tag == "vel" || tag == "acc") dim=3;
    checkFArray(*size*dim,nbody*dim);
    memcpy(array,data,sizeof(float)*nbody*dim);
    status=nbody;
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_get_array_i:                                                                
// return component's array belonging to the simulation with the identifier ident         
int uns_get_array_i_( const int * id, const char * _comp, const char * _tag, int * array , int * size, const int l1, const int l2)
{
  int index=getUnsvIndex(*id);
235
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  // populate user_select object according to the selection
  // and the object
  std::string tag  = tools::Ctools::fixFortran(_tag,l2);
  std::string comp = tools::Ctools::fixFortran(_comp,l1);  
  int *data;
  int nbody;
  bool ok=snap->getData(comp,tag,&nbody,&data);
  int status=0;
  if (ok) {
    checkFArray(*size,nbody);
    memcpy(array,data,sizeof(float)*nbody);
    status=nbody;
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_get_value_f_:                                                               
// return tag's value belonging to the simulation with the identifier ident        
int uns_get_value_f_( const int * id, const char * _tag, float   *  data, const int l1)
{
  int index=getUnsvIndex(*id);
257
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  // populate user_select object according to the selection
  // and the object
  std::string tag = tools::Ctools::fixFortran(_tag,l1);
  int status=0;
  bool ok=snap->getData(tag,data);
  if (ok) {
    status=1;
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_get_value_i_:                                                               
// return tag's value belonging to the simulation with the identifier ident        
int uns_get_value_i_( const int * id, const char * _tag, int   *  data, const int l1)
{
  int index=getUnsvIndex(*id);
274
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  // populate user_select object according to the selection
  // and the object
  std::string tag = tools::Ctools::fixFortran(_tag,l1);
  int status=0;
  bool ok=snap->getData(tag,data);
  if (ok) {
    status=1;
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getNbody:                                                               
// return #bodies belonging to the simulation with the identifier ident        
int uns_get_nbody_( const int * id, int   * nbody)
{
  int index=getUnsvIndex(*id);
291
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
292 293 294 295 296 297 298 299 300 301 302
  // populate user_select object according to the selection
  // and the object
  *nbody = snap->getNSel();//user_select.getNSel(); 
  return *nbody;
}
// ----------------------------------------------------------------------------
// uns_getTime:                                                                
// return time belonging to the simulation with the identifier ident           
int uns_get_time_( const int * id, float  * time)
{
  int index=getUnsvIndex(*id);
303
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
304 305 306 307 308 309 310 311 312
  snap->getData("time",time);
  return 1;
}
// ----------------------------------------------------------------------------
// uns_get_redshift:                                                                
// return redshift belonging to the simulation with the identifier ident           
int uns_get_redshift_( const int * id, float  * rs)
{
  int index=getUnsvIndex(*id);
313
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
314 315 316 317 318 319 320 321 322 323 324
  if (snap->getData("redshift",rs))
    return 1;
  else
    return 0;
}
// ----------------------------------------------------------------------------
// uns_getPos:                                                                 
// return positions belonging to the simulation with the identifier ident      
int uns_get_pos_( const int * id, float  * pos, int * size)
{
  int index=getUnsvIndex(*id);
325
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
326 327 328 329 330
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("pos",&nbody,&data);
331
  if (!ok) assert(0);
jcl's avatar
jcl committed
332 333 334 335 336 337 338 339 340 341
  checkFArray(*size,nbody);
  memcpy(pos,data,sizeof(float)*3*nbody);
  return 1;
}
// ----------------------------------------------------------------------------
// uns_getVel:                                                                 
// return velocities belonging to the simulation with the identifier ident     
int uns_get_vel_( const int * id, float  * vel, int * size)
{
  int index=getUnsvIndex(*id);
342
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
343 344 345 346 347
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("vel",&nbody,&data);
348
  if (!ok) assert(0);
jcl's avatar
jcl committed
349 350 351 352 353 354 355 356 357 358
  checkFArray(*size,nbody);
  memcpy(vel,data,sizeof(float)*3*nbody);
  return 1;
}
// ----------------------------------------------------------------------------
// uns_getMass:                                                                
// return masses belonging to the simulation with the identifier ident         
int uns_get_mass_( const int * id, float  * mass, int * size)
{
  int index=getUnsvIndex(*id);
359
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
360 361 362 363 364
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("mass",&nbody,&data);
365
  if (!ok) assert(0);
jcl's avatar
jcl committed
366 367 368 369 370 371 372 373 374 375 376 377
  checkFArray(*size,nbody);
  memcpy(mass,data,sizeof(float)*nbody);
  return 1;
}
// ----------------------------------------------------------------------------
// uns_getAge:                                                                 
// give ages of the stars belonging to the simulation with the identifier ident
// return #stars                                                               
int uns_get_age_( const int * id, float  * age, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
378
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("age",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(age,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getMetal:                                                               
// give metallicity of the gas+stars belonging to the simulation with the      
// identifier ident                                                            
// return #(gas+stars)
int uns_get_metal_( const int * id, float  * metal, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
400
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("metal",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(metal,data,sizeof(float)*nbody);
  }

  return status;
}
// ----------------------------------------------------------------------------
// uns_getMetalGas:                                                            
// give metallicity of the gas belonging to the simulation with the            
// identifier ident                                                            
// return #(gas+stars)
int uns_get_metal_gas_( const int * id, float  * metal, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
423
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("gas_metal",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(metal,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getMetalStars:                                                          
// give metallicity of the stars belonging to the simulation with the          
// identifier ident                                                            
// return #(gas+stars)
int uns_get_metal_stars_( const int * id, float  * metal, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
445
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("stars_metal",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(metal,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getU:                                                                   
// give Internal energy of the gas belonging to the simulation with the        
// identifier ident                                                            
// return #(gas)
int uns_get_u_( const int * id, float  * u, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
467
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("u",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(u,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getTemp:                                                                
// give temperature of the gas belonging to the simulation with the            
// identifier ident                                                            
// return #(gas)                                                               
int uns_get_temp_( const int * id, float  * temp, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
489
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("temp",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(temp,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getRho:                                                                 
// give density of the gas belonging to the simulation with the                
// identifier ident                                                            
// return #(gas)                                                               
int uns_get_rho_( const int * id, float  * rho, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
511
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("rho",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(rho,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_getHsml:                                                                 
// give Hsml of the gas belonging to the simulation with the                
// identifier ident                                                            
// return #(gas)                                                               
int uns_get_hsml_( const int * id, float  * hsml, int * size)
{
  int status=0;
  int index=getUnsvIndex(*id);
533
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  // populate user_select object according to the selection
  // and the object
  int nbody;
  float * data;
  bool ok=snap->getData("hsml",&nbody,&data);
  if (ok) {
    status=nbody;
    checkFArray(*size,nbody);
    memcpy(hsml,data,sizeof(float)*nbody);
  }
  return status;
}
// ----------------------------------------------------------------------------
// uns_get_range:                                                              
// return nbody, first and last particles of the component selected and        
// belonging to the simulation with the identifier ident                       
int uns_get_range_( const int * id, const char * _comp ,
			int * nbody, int * first, int * last, const int l1)
{
  int index=getUnsvIndex(*id);
  std::string comp = tools::Ctools::fixFortran(_comp,l1);
555
  int status=((CunsIn2<float>*)unsv[index].obj)->snapshot->getRangeSelect(comp.c_str(),nbody,first,last,true);
jcl's avatar
jcl committed
556 557 558 559 560 561 562 563 564 565 566
  return status;
}
// ----------------------------------------------------------------------------
// uns_get_eps:                                                                
// return softening of the component selected and                              
// belonging to the simulation with the identifier ident                       
int uns_get_eps_( const int * id, const char * _comp ,
		    float * eps, const int l1)
{
  int index=getUnsvIndex(*id);
  std::string comp = tools::Ctools::fixFortran(_comp,l1);
567
  *eps=((CunsIn2<float>*)unsv[index].obj)->snapshot->getEps(comp);
jcl's avatar
jcl committed
568 569 570 571 572 573 574 575 576 577
  return (((*eps)>0.)?1:0);
}
// ----------------------------------------------------------------------------
// uns_get_cod:                                                                
// return time+cod of the components selected and                              
// belonging to the simulation with the identifier ident                       
int uns_get_cod_ ( const int * id, const char * _select, const float * time,
		   float * tcod, const int l1)
{
  int index=getUnsvIndex(*id);
578
  uns::CSnapshotInterfaceIn<float> * snap = ((CunsIn2<float>*)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
579 580 581 582 583 584 585 586 587 588 589 590
  std::string select = tools::Ctools::fixFortran(_select,l1);
  int status = snap->getCod(select,*time,tcod);
  return status;
}

// ----------------------------------------------------------------------------
// uns_sim_type:                                                               
// return simulation type (nemo|gadget etc....) of the requested simulation    
// and belonging to the simulation with the identifier ident                   
int uns_sim_type_(const int * id)
{
  int index=getUnsvIndex(*id);
591
  return(((CunsIn2<float>*)unsv[index].obj)->snapshot->getInterfaceIndex());
jcl's avatar
jcl committed
592 593 594 595 596 597 598 599
}
// ----------------------------------------------------------------------------
// uns_sim_dir:                                                               
// return simulation dirname of the requested simulation    
// and belonging to the simulation with the identifier ident                   
void uns_sim_dir_(const int * id, char * simdir, int lenstring)
{
  int index=getUnsvIndex(*id);
600
  std::string dir= ((CunsIn2<float>*)unsv[index].obj)->snapshot->getSimDir();
jcl's avatar
jcl committed
601 602 603 604 605 606 607
  assert(dir.length() <= (unsigned int) lenstring);
  strcpy(simdir,dir.c_str());
  for (int i=strlen(simdir); i<lenstring; i++) {
    simdir[i] = ' ';
  }
}
// ----------------------------------------------------------------------------
608 609 610 611 612 613
// uns_get_file_structure:
// return snapshot file structure of the requested simulation
// and belonging to the simulation with the identifier ident
void uns_get_file_structure_(const int * id, char * dest, int lenstring)
{
  int index=getUnsvIndex(*id);
614
  std::string source= ((CunsIn2<float>*)unsv[index].obj)->snapshot->getFileStructure();
615 616 617 618 619 620 621 622 623 624 625 626 627
  assert(source.length() <= (unsigned int) lenstring);
  strcpy(dest,source.c_str());
  for (int i=strlen(dest); i<lenstring; i++) {
    dest[i] = ' ';
  }
}
// ----------------------------------------------------------------------------
// uns_get_file_name:
// return snapshot file name of the requested simulation
// and belonging to the simulation with the identifier ident
void uns_get_file_name_(const int * id, char * dest, int lenstring)
{
  int index=getUnsvIndex(*id);
628
  std::string source= ((CunsIn2<float>*)unsv[index].obj)->snapshot->getFileName();
629 630 631 632 633 634 635 636 637 638 639 640 641
  assert(source.length() <= (unsigned int) lenstring);
  strcpy(dest,source.c_str());
  for (int i=strlen(dest); i<lenstring; i++) {
    dest[i] = ' ';
  }
}
// ----------------------------------------------------------------------------
// uns_get_interface_type:
// return interface type of the requested simulation
// and belonging to the simulation with the identifier ident
void uns_get_interface_type_(const int * id, char * dest, int lenstring)
{
  int index=getUnsvIndex(*id);
642
  std::string source= ((CunsIn2<float>*)unsv[index].obj)->snapshot->getInterfaceType();
643 644 645 646 647 648 649
  assert(source.length() <= (unsigned int) lenstring);
  strcpy(dest,source.c_str());
  for (int i=strlen(dest); i<lenstring; i++) {
    dest[i] = ' ';
  }
}
// ----------------------------------------------------------------------------
jcl's avatar
jcl committed
650 651 652 653
// getCrv:                                                                     
// return the Component Range Vector belonging to the UNS's object "index"     
void getCrv(int index) 
{
654
  assert((unsigned int)index<unsv.size());
655
  crv = ((CunsIn2<float>*)unsv[index].obj)->snapshot->getSnapshotRange();
jcl's avatar
jcl committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  //ComponentRange::list(crv);
}
// ----------------------------------------------------------------------------
// getUnsvIndex:                                                               
// return the UNS's object index of the ident simulation                       
int getUnsvIndex(const int ident) 
{
  // look for "ident" exist in the already opened snapshots
  // return the index in the vector of -1 if false         
  int index=uns::CunsIdentifier::getUnsvIndex(ident,&unsv);
  if (index < 0) {
    std::cerr << "\n\nIdentifier #["<<ident<<"] does not exist\n\n";
    std::cerr << "Aborting..........\n\n";
    std::exit(1);
  }
  return index;
}
// ----------------------------------------------------------------------------
// checkFArray                                                                 
// check if fortran array are big enough to store data                         
void checkFArray(const int size, const int nbody)
{
  if (nbody > size) {
    std::cerr << "Your fortran array size["<<size<<"] is too small to handle\n"
	      << "all the nbody["<<nbody<<"] particles.\n"
	      << "\nprogram aborted....\n";
    std::exit(1);
  }
}

// ----------------------------------------------------------------------------
//             S A V I N G         O P E R A T I O N
// ----------------------------------------------------------------------------



// ----------------------------------------------------------------------------
// uns_save_init_:                                                                  
// initialyse UNS object for saving with a new snapshot                                   
// Input :                                                                     
// simname     -> snapshot's output file name
// simtype     -> simulation type
// Output :                                                                    
int uns_save_init_(const char * _name,const char * _type, const int l1, const int l2)
{
  int ret=0;
  std::string simname  = tools::Ctools::fixFortran(_name,l1,false);
  std::string simtype  = tools::Ctools::fixFortran(_type,l2,false);
704
  uns::CunsOut2<float> * uns = new uns::CunsOut2<float>(simname,simtype,false);
jcl's avatar
jcl committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    
  uns::CunsIdentifier * unso = new  uns::CunsIdentifier();
  unso->ident = ident++;
  unso->obj   = uns;
  unsv.push_back(*unso);
  ret = unso->ident;

  return ret;
}

// ----------------------------------------------------------------------------
// uns_set_pos_:                                                                 
// set positions belonging to the simulation with the identifier ident      
int uns_set_pos_( const int * id, float  * pos, int * size)
{
  int index=getUnsvIndex(*id);
721
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
722 723 724 725 726 727 728 729 730 731 732

  // save positions
  int status=snap->setData("pos",*size,pos,false);
  return status;
}
// ----------------------------------------------------------------------------
// uns_set_time_:                                                                 
// set time belonging to the simulation with the identifier ident      
int uns_set_time_( const int * id, float  * time)
{
  int index=getUnsvIndex(*id);
733
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
734 735 736 737 738 739 740 741 742 743 744

  // save positions
  int status=snap->setData("time",*time);
  return status;
}
// ----------------------------------------------------------------------------
// uns_set_array_f_:                                                                 
// set comp's float array belonging to the simulation with the identifier ident      
int uns_set_array_f_( const int * id, const char * _comp, const char * _array, float * data , int * size, const int l1, const int l2)
{
  int index=getUnsvIndex(*id);
745
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759

  std::string comp  = tools::Ctools::fixFortran(_comp,l1);
  std::string array = tools::Ctools::fixFortran(_array,l2);
    
  // set data
  int status=snap->setData(comp,array,*size,data,true);  
  return status;
}
// ----------------------------------------------------------------------------
// uns_set_array_i_:                                                                 
// set comp's integer array belonging to the simulation with the identifier ident      
int uns_set_array_i_( const int * id, const char * _comp, const char * _array, int * data , int * size, const int l1, const int l2)
{
  int index=getUnsvIndex(*id);
760
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774

  std::string comp  = tools::Ctools::fixFortran(_comp,l1);
  std::string array = tools::Ctools::fixFortran(_array,l2);
  
  // set data
  int status=snap->setData(comp,array,*size,data,true);
  return status;
}
// ----------------------------------------------------------------------------
// uns_set_value_f_:                                                                 
// set comp's float data belonging to the simulation with the identifier ident      
int uns_set_value_f_( const int * id, const char * tag, float * data, const int l1)
{
  int index=getUnsvIndex(*id);
775
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
776 777 778 779 780 781 782 783 784 785 786
  std::string comp  = tools::Ctools::fixFortran(tag,l1);
  // set data
  int status=snap->setData(comp,*data);
  return status;
}
// ----------------------------------------------------------------------------
// uns_set_value_i_:                                                                 
// set comp's integer data belonging to the simulation with the identifier ident      
int uns_set_value_i_( const int * id, const char * tag, int * data, const int l1)
{
  int index=getUnsvIndex(*id);
787
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
788 789 790 791 792 793 794 795 796 797 798 799

  std::string comp  = tools::Ctools::fixFortran(tag,l1);  
  // set data
  int status=snap->setData(comp,*data);
  return status;
}
// ----------------------------------------------------------------------------
// uns_save_:                                                                 
// save data belonging to the simulation with the identifier ident      
int uns_save_     ( const int * id) 
{
  int index=getUnsvIndex(*id);
800
  uns::CSnapshotInterfaceOut<float> * snap = ((CunsOut2<float> *)unsv[index].obj)->snapshot;
jcl's avatar
jcl committed
801 802 803 804 805 806 807 808
  
  snap->save();
  return 1;
}
} // namespace uns
// ----------------------------------------------------------------------------
// End of file                                                                 
// ----------------------------------------------------------------------------