| 1 | 
#include <iostream> | 
| 2 | 
 | 
| 3 | 
#include <cstdlib> | 
| 4 | 
#include <cstdio> | 
| 5 | 
 | 
| 6 | 
#include "simError.h" | 
| 7 | 
#include "SimState.hpp" | 
| 8 | 
 | 
| 9 | 
 | 
| 10 | 
SimState::SimState(){ | 
| 11 | 
 | 
| 12 | 
  nElements = 0; | 
| 13 | 
  arraysAllocated = false; | 
| 14 | 
 | 
| 15 | 
  pos  = NULL; | 
| 16 | 
  vel  = NULL; | 
| 17 | 
  frc  = NULL; | 
| 18 | 
  trq  = NULL; | 
| 19 | 
  Amat = NULL; | 
| 20 | 
  mu   = NULL; | 
| 21 | 
  ul   = NULL; | 
| 22 | 
} | 
| 23 | 
 | 
| 24 | 
 | 
| 25 | 
SimState::~SimState(){ | 
| 26 | 
 | 
| 27 | 
  if(pos  != NULL) delete[] pos; | 
| 28 | 
  if(vel  != NULL) delete[] vel; | 
| 29 | 
  if(frc  != NULL) delete[] frc; | 
| 30 | 
  if(trq  != NULL) delete[] trq; | 
| 31 | 
  if(Amat != NULL) delete[] Amat; | 
| 32 | 
  if(mu   != NULL) delete[] mu; | 
| 33 | 
  if(ul   != NULL) delete[] ul;   | 
| 34 | 
 | 
| 35 | 
} | 
| 36 | 
 | 
| 37 | 
 | 
| 38 | 
void SimState::createArrays (int the_nElements) { | 
| 39 | 
  int i, index3, index9; | 
| 40 | 
 | 
| 41 | 
  if( arraysAllocated ){ | 
| 42 | 
 | 
| 43 | 
    sprintf( painCave.errMsg, | 
| 44 | 
             "Someone has attempted to allocate SimState arrays before " | 
| 45 | 
             "deallocating the previous arrays.\n" ); | 
| 46 | 
    painCave.isFatal = 1; | 
| 47 | 
    simError(); | 
| 48 | 
  } | 
| 49 | 
 | 
| 50 | 
  nElements = the_nElements; | 
| 51 | 
 | 
| 52 | 
  pos  = new double[nElements*3]; | 
| 53 | 
  vel  = new double[nElements*3]; | 
| 54 | 
  frc  = new double[nElements*3]; | 
| 55 | 
  trq  = new double[nElements*3]; | 
| 56 | 
  Amat = new double[nElements*9]; | 
| 57 | 
  mu   = new double[nElements]; | 
| 58 | 
  ul   = new double[nElements*3]; | 
| 59 | 
   | 
| 60 | 
  // init directional values to zero | 
| 61 | 
   | 
| 62 | 
  for( i=0; i<nElements; i++){ | 
| 63 | 
    index3 = i*3; | 
| 64 | 
    index9 = i*9; | 
| 65 | 
     | 
| 66 | 
    trq[index3+0] = 0.0; | 
| 67 | 
    trq[index3+1] = 0.0; | 
| 68 | 
    trq[index3+2] = 0.0; | 
| 69 | 
     | 
| 70 | 
    Amat[index9+0] = 1.0; | 
| 71 | 
    Amat[index9+1] = 0.0; | 
| 72 | 
    Amat[index9+2] = 0.0; | 
| 73 | 
     | 
| 74 | 
    Amat[index9+3] = 0.0; | 
| 75 | 
    Amat[index9+4] = 1.0; | 
| 76 | 
    Amat[index9+5] = 0.0; | 
| 77 | 
     | 
| 78 | 
    Amat[index9+6] = 0.0; | 
| 79 | 
    Amat[index9+7] = 0.0; | 
| 80 | 
    Amat[index9+8] = 1.0; | 
| 81 | 
     | 
| 82 | 
    mu[i] = 0.0;     | 
| 83 | 
     | 
| 84 | 
    ul[index3+0] = 1.0; | 
| 85 | 
    ul[index3+1] = 0.0; | 
| 86 | 
    ul[index3+2] = 0.0; | 
| 87 | 
  } | 
| 88 | 
 | 
| 89 | 
  arraysAllocated = true; | 
| 90 | 
} | 
| 91 | 
 | 
| 92 | 
void SimState::destroyArrays( void ){ | 
| 93 | 
 | 
| 94 | 
  if(pos  != NULL) delete[] pos; | 
| 95 | 
  if(vel  != NULL) delete[] vel; | 
| 96 | 
  if(frc  != NULL) delete[] frc; | 
| 97 | 
  if(trq  != NULL) delete[] trq; | 
| 98 | 
  if(Amat != NULL) delete[] Amat; | 
| 99 | 
  if(mu   != NULL) delete[] mu; | 
| 100 | 
  if(ul   != NULL) delete[] ul;   | 
| 101 | 
 | 
| 102 | 
  pos  = NULL; | 
| 103 | 
  vel  = NULL; | 
| 104 | 
  frc  = NULL; | 
| 105 | 
  trq  = NULL; | 
| 106 | 
  Amat = NULL; | 
| 107 | 
  mu   = NULL; | 
| 108 | 
  ul   = NULL; | 
| 109 | 
 | 
| 110 | 
  arraysAllocated = false; | 
| 111 | 
  nElements = 0; | 
| 112 | 
} | 
| 113 | 
 | 
| 114 | 
void SimState::getAtomPointers( int index, | 
| 115 | 
                                double** the_pos, | 
| 116 | 
                                double** the_vel, | 
| 117 | 
                                double** the_frc, | 
| 118 | 
                                double** the_trq, | 
| 119 | 
                                double** the_Amat, | 
| 120 | 
                                double** the_mu,  | 
| 121 | 
                                double** the_ul ){ | 
| 122 | 
  int index3, index9; | 
| 123 | 
 | 
| 124 | 
  if( arraysAllocated ){ | 
| 125 | 
     | 
| 126 | 
    index3 = index*3; | 
| 127 | 
    index9 = index*9; | 
| 128 | 
     | 
| 129 | 
    *the_pos  = &(pos[index3]); | 
| 130 | 
    *the_vel  = &(vel[index3]);  | 
| 131 | 
    *the_frc  = &(frc[index3]);  | 
| 132 | 
    *the_trq  = &(trq[index3]);  | 
| 133 | 
    *the_Amat = &(Amat[index9]); | 
| 134 | 
    *the_mu   = &(mu[index]);  | 
| 135 | 
    *the_ul   = &(ul[index3]); | 
| 136 | 
  } | 
| 137 | 
  else{ | 
| 138 | 
 | 
| 139 | 
    sprintf( painCave.errMsg, | 
| 140 | 
             "Atom %d attempted to access its arrays before they had been" | 
| 141 | 
             " allocated\n", | 
| 142 | 
             index ); | 
| 143 | 
    painCave.isFatal = 1; | 
| 144 | 
    simError(); | 
| 145 | 
  } | 
| 146 | 
} |