| 1 |
#include <iostream> |
| 2 |
#include <cmath> |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <unistd.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
|
| 11 |
#include "ReadWrite.hpp" |
| 12 |
|
| 13 |
|
| 14 |
InitializeFromFile :: InitializeFromFile( char *in_name ){ |
| 15 |
|
| 16 |
c_in_file = fopen(in_name, "r"); |
| 17 |
if(c_in_file == NULL){ |
| 18 |
printf("Cannot open file: %s\n", in_name); |
| 19 |
exit(8); |
| 20 |
} |
| 21 |
|
| 22 |
strcpy( c_in_name, in_name); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
InitializeFromFile :: ~InitializeFromFile( ){ |
| 27 |
|
| 28 |
int error; |
| 29 |
error = fclose( c_in_file ); |
| 30 |
if( error ){ |
| 31 |
fprintf( stderr, "Error closing %s\n", c_in_name ); |
| 32 |
} |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
void InitializeFromFile :: read_xyz( SimInfo* entry_plug ){ |
| 38 |
|
| 39 |
int i; // loop counter |
| 40 |
|
| 41 |
int n_atoms; // the number of atoms |
| 42 |
char read_buffer[2000]; //the line buffer for reading |
| 43 |
char *eof_test; // ptr to see when we reach the end of the file |
| 44 |
char *foo; // the pointer to the current string token |
| 45 |
|
| 46 |
double rx, ry, rz; // position place holders |
| 47 |
double vx, vy, vz; // velocity placeholders |
| 48 |
double q[4]; // the quaternions |
| 49 |
double jx, jy, jz; // angular velocity placeholders; |
| 50 |
double qSqr, qLength; // needed to normalize the quaternion vector. |
| 51 |
|
| 52 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 53 |
if( eof_test == NULL ){ |
| 54 |
std::cerr << "error reading 1st line of" << c_in_name << "\n"; |
| 55 |
} |
| 56 |
|
| 57 |
(void)sscanf(read_buffer, "%d", &n_atoms); |
| 58 |
|
| 59 |
Atom **atoms = entry_plug->atoms; |
| 60 |
DirectionalAtom* dAtom; |
| 61 |
|
| 62 |
if( n_atoms != entry_plug->n_atoms ){ |
| 63 |
fprintf( stderr, |
| 64 |
"Initialize from File error. %s n_atoms, %d, " |
| 65 |
"does not match the BASS file's n_atoms, %d.\n", |
| 66 |
c_in_name, n_atoms, entry_plug->n_atoms ); |
| 67 |
exit(8); |
| 68 |
} |
| 69 |
|
| 70 |
//read and toss the comment line |
| 71 |
|
| 72 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 73 |
if(eof_test == NULL){ |
| 74 |
printf("error in reading commment in %s\n", c_in_name); |
| 75 |
exit(8); |
| 76 |
} |
| 77 |
|
| 78 |
for( i=0; i < n_atoms; i++){ |
| 79 |
|
| 80 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 81 |
if(eof_test == NULL){ |
| 82 |
printf("error in reading file %s\n" |
| 83 |
"natoms = %d; index = %d\n" |
| 84 |
"error reading the line from the file.\n", |
| 85 |
c_in_name, n_atoms, i ); |
| 86 |
exit(8); |
| 87 |
} |
| 88 |
|
| 89 |
foo = strtok(read_buffer, " ,;\t"); |
| 90 |
|
| 91 |
// check the atom name to the current atom |
| 92 |
|
| 93 |
if( strcmp( foo, atoms[i]->getType() ) ){ |
| 94 |
fprintf( stderr, |
| 95 |
"Initialize from file error. Atom %s at index %d " |
| 96 |
"in file %s does not" |
| 97 |
" match the BASS atom %s.\n", |
| 98 |
foo, i, c_in_name, atoms[i]->getType() ); |
| 99 |
exit(8); |
| 100 |
} |
| 101 |
|
| 102 |
// get the positions |
| 103 |
|
| 104 |
foo = strtok(NULL, " ,;\t"); |
| 105 |
if(foo == NULL){ |
| 106 |
printf("error in reading postition x from %s\n" |
| 107 |
"natoms = %d, index = %d\n", |
| 108 |
c_in_name, n_atoms, i ); |
| 109 |
exit(8); |
| 110 |
} |
| 111 |
(void)sscanf( foo, "%lf", &rx ); |
| 112 |
|
| 113 |
foo = strtok(NULL, " ,;\t"); |
| 114 |
if(foo == NULL){ |
| 115 |
printf("error in reading postition y from %s\n" |
| 116 |
"natoms = %d, index = %d\n", |
| 117 |
c_in_name, n_atoms, i ); |
| 118 |
exit(8); |
| 119 |
} |
| 120 |
(void)sscanf( foo, "%lf", &ry ); |
| 121 |
|
| 122 |
foo = strtok(NULL, " ,;\t"); |
| 123 |
if(foo == NULL){ |
| 124 |
printf("error in reading postition z from %s\n" |
| 125 |
"natoms = %d, index = %d\n", |
| 126 |
c_in_name, n_atoms, i ); |
| 127 |
exit(8); |
| 128 |
} |
| 129 |
(void)sscanf( foo, "%lf", &rz ); |
| 130 |
|
| 131 |
// get the velocities |
| 132 |
|
| 133 |
foo = strtok(NULL, " ,;\t"); |
| 134 |
if(foo == NULL){ |
| 135 |
printf("error in reading velocity x from %s\n" |
| 136 |
"natoms = %d, index = %d\n", |
| 137 |
c_in_name, n_atoms, i ); |
| 138 |
exit(8); |
| 139 |
} |
| 140 |
(void)sscanf( foo, "%lf", &vx ); |
| 141 |
|
| 142 |
foo = strtok(NULL, " ,;\t"); |
| 143 |
if(foo == NULL){ |
| 144 |
printf("error in reading velocity y from %s\n" |
| 145 |
"natoms = %d, index = %d\n", |
| 146 |
c_in_name, n_atoms, i ); |
| 147 |
exit(8); |
| 148 |
} |
| 149 |
(void)sscanf( foo, "%lf", &vy ); |
| 150 |
|
| 151 |
foo = strtok(NULL, " ,;\t"); |
| 152 |
if(foo == NULL){ |
| 153 |
printf("error in reading velocity z from %s\n" |
| 154 |
"natoms = %d, index = %d\n", |
| 155 |
c_in_name, n_atoms, i ); |
| 156 |
exit(8); |
| 157 |
} |
| 158 |
(void)sscanf( foo, "%lf", &vz ); |
| 159 |
|
| 160 |
|
| 161 |
// get the quaternions |
| 162 |
|
| 163 |
if( atoms[i]->isDirectional() ){ |
| 164 |
|
| 165 |
foo = strtok(NULL, " ,;\t"); |
| 166 |
if(foo == NULL){ |
| 167 |
printf("error in reading quaternion 0 from %s\n" |
| 168 |
"natoms = %d, index = %d\n", |
| 169 |
c_in_name, n_atoms, i ); |
| 170 |
exit(8); |
| 171 |
} |
| 172 |
(void)sscanf( foo, "%lf", &q[0] ); |
| 173 |
|
| 174 |
foo = strtok(NULL, " ,;\t"); |
| 175 |
if(foo == NULL){ |
| 176 |
printf("error in reading quaternion 1 from %s\n" |
| 177 |
"natoms = %d, index = %d\n", |
| 178 |
c_in_name, n_atoms, i ); |
| 179 |
exit(8); |
| 180 |
} |
| 181 |
(void)sscanf( foo, "%lf", &q[1] ); |
| 182 |
|
| 183 |
foo = strtok(NULL, " ,;\t"); |
| 184 |
if(foo == NULL){ |
| 185 |
printf("error in reading quaternion 2 from %s\n" |
| 186 |
"natoms = %d, index = %d\n", |
| 187 |
c_in_name, n_atoms, i ); |
| 188 |
exit(8); |
| 189 |
} |
| 190 |
(void)sscanf( foo, "%lf", &q[2] ); |
| 191 |
|
| 192 |
foo = strtok(NULL, " ,;\t"); |
| 193 |
if(foo == NULL){ |
| 194 |
printf("error in reading quaternion 3 from %s\n" |
| 195 |
"natoms = %d, index = %d\n", |
| 196 |
c_in_name, n_atoms, i ); |
| 197 |
exit(8); |
| 198 |
} |
| 199 |
(void)sscanf( foo, "%lf", &q[3] ); |
| 200 |
|
| 201 |
// get the angular velocities |
| 202 |
|
| 203 |
foo = strtok(NULL, " ,;\t"); |
| 204 |
if(foo == NULL){ |
| 205 |
printf("error in reading angular momentum jx from %s\n" |
| 206 |
"natoms = %d, index = %d\n", |
| 207 |
c_in_name, n_atoms, i ); |
| 208 |
exit(8); |
| 209 |
} |
| 210 |
(void)sscanf( foo, "%lf", &jx ); |
| 211 |
|
| 212 |
foo = strtok(NULL, " ,;\t"); |
| 213 |
if(foo == NULL){ |
| 214 |
printf("error in reading angular momentum jy from %s\n" |
| 215 |
"natoms = %d, index = %d\n", |
| 216 |
c_in_name, n_atoms, i ); |
| 217 |
exit(8); |
| 218 |
} |
| 219 |
(void)sscanf( foo, "%lf", &jy ); |
| 220 |
|
| 221 |
foo = strtok(NULL, " ,;\t"); |
| 222 |
if(foo == NULL){ |
| 223 |
printf("error in reading angular momentum jz from %s\n" |
| 224 |
"natoms = %d, index = %d\n", |
| 225 |
c_in_name, n_atoms, i ); |
| 226 |
exit(8); |
| 227 |
} |
| 228 |
(void)sscanf( foo, "%lf", &jz ); |
| 229 |
|
| 230 |
dAtom = ( DirectionalAtom* )atoms[i]; |
| 231 |
|
| 232 |
// check that the quaternion vector is normalized |
| 233 |
|
| 234 |
qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); |
| 235 |
|
| 236 |
qLength = sqrt( qSqr ); |
| 237 |
q[0] = q[0] / qLength; |
| 238 |
q[1] = q[1] / qLength; |
| 239 |
q[2] = q[2] / qLength; |
| 240 |
q[3] = q[3] / qLength; |
| 241 |
|
| 242 |
dAtom->setQ( q ); |
| 243 |
|
| 244 |
// add the angular velocities |
| 245 |
|
| 246 |
dAtom->setJx( jx ); |
| 247 |
dAtom->setJy( jy ); |
| 248 |
dAtom->setJz( jz ); |
| 249 |
} |
| 250 |
|
| 251 |
// add the positions and velocities to the atom |
| 252 |
|
| 253 |
atoms[i]->setX( rx ); |
| 254 |
atoms[i]->setY( ry ); |
| 255 |
atoms[i]->setZ( rz ); |
| 256 |
|
| 257 |
atoms[i]->set_vx( vx ); |
| 258 |
atoms[i]->set_vy( vy ); |
| 259 |
atoms[i]->set_vz( vz ); |
| 260 |
|
| 261 |
} |
| 262 |
} |