unsfwrapper.cc 32.4 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 83 84 85 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 169 170 171 172 173 174 175 176 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
// ============================================================================
// 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                                                   
// ============================================================================

#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);
    
    // 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);
  uns::CunsIn * uns = new uns::CunsIn(simname,sel_comp,sel_time,false);
  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) {
    uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
    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);

  std::cerr << "uns_load_opt = [" << bits << "]\n";
  if (index >= 0) {
    uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
    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) {
    uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
    snap->close();
    delete ((CunsIn *)unsv[index].obj);
  }

  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) {
    uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;
    snap->close();
    delete ((CunsOut *)unsv[index].obj);
  }

  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("pos",&nbody,&data);
  assert(ok==true);
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("vel",&nbody,&data);
  assert(ok==true);
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // populate user_select object according to the selection
  // and the object
  float *data;
  int nbody;
  bool ok=snap->getData("mass",&nbody,&data);
  assert(ok==true);
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  // 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);
  int status=((CunsIn *)unsv[index].obj)->snapshot->getRangeSelect(comp.c_str(),nbody,first,last,true);
  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);
  *eps=((CunsIn *)unsv[index].obj)->snapshot->getEps(comp);
  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);
  uns::CSnapshotInterfaceIn * snap = ((CunsIn *)unsv[index].obj)->snapshot;
  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);
  return(((CunsIn *)unsv[index].obj)->snapshot->getInterfaceIndex());
}
// ----------------------------------------------------------------------------
// 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);
  std::string dir= ((CunsIn *)unsv[index].obj)->snapshot->getSimDir();
  assert(dir.length() <= (unsigned int) lenstring);
  strcpy(simdir,dir.c_str());
  for (int i=strlen(simdir); i<lenstring; i++) {
    simdir[i] = ' ';
  }
}
// ----------------------------------------------------------------------------
// getCrv:                                                                     
// return the Component Range Vector belonging to the UNS's object "index"     
void getCrv(int index) 
{
  assert(index<(int)unsv.size());
  crv = ((CunsIn *)unsv[index].obj)->snapshot->getSnapshotRange();
  //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);
  uns::CunsOut * uns = new uns::CunsOut(simname,simtype,false);
    
  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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;

  // 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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;

  // 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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;

  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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;

  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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;
  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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;

  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);
  uns::CSnapshotInterfaceOut * snap = ((CunsOut *)unsv[index].obj)->snapshot;
  
  snap->save();
  return 1;
}
} // namespace uns
// ----------------------------------------------------------------------------
// End of file                                                                 
// ----------------------------------------------------------------------------