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
|
/*
*_________________________________________________________________________*
* POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE *
* DESCRIPTION: SEE READ-ME *
* FILE NAME: workspace.cpp *
* AUTHORS: See Author List *
* GRANTS: See Grants List *
* COPYRIGHT: (C) 2005 by Authors as listed in Author's List *
* LICENSE: Please see License Agreement *
* DOWNLOAD: Free at www.rpi.edu/~anderk5 *
* ADMINISTRATOR: Prof. Kurt Anderson *
* Computational Dynamics Lab *
* Rensselaer Polytechnic Institute *
* 110 8th St. Troy NY 12180 *
* CONTACT: anderk5@rpi.edu *
*_________________________________________________________________________*/
#include "workspace.h"
#include "system.h"
#include "solver.h"
#include "SystemProcessor.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void Workspace::allocateNewSystem() {
currentIndex++;
if(currentIndex < maxAlloc)
{
system[currentIndex].system = new System;
}
else
{
maxAlloc = (maxAlloc + 1) * 2;
SysData * tempPtrSys = new SysData[maxAlloc];
for(int i = 0; i < currentIndex; i++)
{
tempPtrSys[i] = system[i];
}
delete [] system;
system = tempPtrSys;
system[currentIndex].system = new System;
}
}
Workspace::Workspace(){
currentIndex = -1;
maxAlloc = 0;
system = nullptr;
}
Workspace::~Workspace(){
for(int i = 0; i <= currentIndex; i++){
delete system[i].system;
}
delete [] system;
}
bool Workspace::LoadFile(char* filename){
bool ans;
ifstream file;
file.open(filename, ifstream::in);
if( !file.is_open() ){
cerr << "File '" << filename << "' not found." << endl;
return false;
}
allocateNewSystem();
ans = system[currentIndex].system->ReadIn(file);
file.close();
return ans;
}
void Workspace::SetLammpsValues(double dtv, double dthalf, double tempcon){
Thalf = dthalf;
Tfull = dtv;
ConFac = tempcon;
}
bool Workspace::MakeSystem(int& nbody, double *&masstotal, double **&inertia, double **&xcm, double **&vcm, double **&omega, double **&ex_space, double **&ey_space, double **&ez_space, int &njoint, int **&jointbody, double **&xjoint, int& nfree, int*freelist, double dthalf, double dtv, double tempcon, double KE){
SetLammpsValues(dtv, dthalf, tempcon);
if(njoint){
SystemProcessor sys;
sys.processArray(jointbody, njoint);
List<POEMSChain> * results = sys.getSystemData();
int numsyschains = results->GetNumElements();
int headvalue = 0;
List<POEMSChain> * newresults = results;
ListElement<POEMSChain> * tempNode = results->GetHeadElement();
int stop = 1;
int counter = 1;
for(int n = 1; n<=numsyschains; n++){
while(stop){
if ( (*(tempNode->value->listOfNodes.GetHeadElement()->value) == (headvalue+1) ) || (*(tempNode->value->listOfNodes.GetTailElement()->value) == (headvalue+1) ) ) {
newresults->Append(tempNode->value);
headvalue = headvalue + tempNode->value->listOfNodes.GetNumElements();
tempNode = results->GetHeadElement();
stop = 0;
counter ++;
}
else{
tempNode = tempNode->next;
}
}
stop=1;
}
ListElement<POEMSChain> * TNode = newresults->GetHeadElement();
ListElement<POEMSChain> * TTNode = newresults->GetHeadElement();
for(int kk=1; kk<=numsyschains; kk++){
if(kk!=numsyschains)
TTNode = TNode->next;
newresults->Remove(TNode);
if(kk!=numsyschains)
TNode = TTNode;
}
ListElement<POEMSChain> * NodeValue = newresults->GetHeadElement();
int count = 0;
int * array;
int ** arrayFromChain;
int numElementsInSystem;
int ttk = 0;
while(NodeValue != nullptr) {
array = new int[NodeValue->value->listOfNodes.GetNumElements()];
arrayFromChain = NodeValue->value->listOfNodes.CreateArray();
numElementsInSystem = NodeValue->value->listOfNodes.GetNumElements();
for(counter = 0; counter < numElementsInSystem; counter++){
array[counter] = *arrayFromChain[counter];
}
SetKE(1,KE);
allocateNewSystem();
system[currentIndex].system->Create_System_LAMMPS(nbody,masstotal,inertia,xcm,xjoint,vcm,omega,ex_space,ey_space,ez_space, numElementsInSystem, array, count);
system[currentIndex].solver = ONSOLVER;
ttk = ttk + 1;
count = ttk;
delete [] array;
delete [] arrayFromChain;
NodeValue= NodeValue->next;
}
}
if(nfree){
MakeDegenerateSystem(nfree,freelist,masstotal,inertia,xcm,vcm,omega,ex_space,ey_space,ez_space);
}
return true;
}
bool Workspace::MakeDegenerateSystem(int& nfree, int*freelist, double *&masstotal, double **&inertia, double **&xcm, double **&vcm, double **&omega, double **&ex_space, double **&ey_space, double **&ez_space){
allocateNewSystem();
system[currentIndex].system->Create_DegenerateSystem(nfree,freelist,masstotal,inertia,xcm,vcm,omega,ex_space,ey_space,ez_space);
system[currentIndex].solver = ONSOLVER;
return true;
}
bool Workspace::SaveFile(char* filename, int index){
if(index < 0){
index = currentIndex;
}
ofstream file;
file.open(filename, ofstream::out);
if( !file.is_open() ){
cerr << "File '" << filename << "' could not be opened." << endl;
return false;
}
if(index >= 0 && index <= currentIndex){
system[index].system->WriteOut(file);
}
else {
cerr << "Error, requested system index " << index << ", minimum index 0 and maximum index " << currentIndex << endl;
}
file.close();
return true;
}
System* Workspace::GetSystem(int index){
if(index <= currentIndex){
if(index >= 0){
return system[index].system;
}
else{
return system[currentIndex].system;
}
}
else{
return nullptr;
}
}
void Workspace::AddSolver(Solver* s, int index){
if(currentIndex >= index){
if(index >= 0){
system[index].solver = (int)s->GetSolverType();
}
else{
system[currentIndex].solver = (int)s->GetSolverType();
}
}
else{
cout << "Error adding solver to index " << index << endl;
}
}
int Workspace::getNumberOfSystems(){
return currentIndex + 1;
}
void Workspace::SetKE(int temp, double SysKE){
KE_val = SysKE;
FirstTime =temp;
}
void Workspace::LobattoOne(double **&xcm, double **&vcm,double **&omega,double **&torque, double **&fcm, double **&ex_space, double **&ey_space, double **&ez_space){
int numsys = currentIndex;
int numbodies;
double time = 0.0;
int * mappings;
double SysKE=0.0;
for (int i = 0; i <= numsys; i++){
mappings = system[i].system->GetMappings();
numbodies = system[i].system->GetNumBodies() - 1;
Matrix FF(6,numbodies);
FF.Zeros();
for (int j=1; j<=numbodies; j++){
FF(1,j) = torque[mappings[j - 1]-1][0]*ConFac;
FF(2,j) = torque[mappings[j - 1]-1][1]*ConFac;
FF(3,j) = torque[mappings[j - 1]-1][2]*ConFac;
FF(4,j) = fcm[mappings[j - 1]-1][0]*ConFac;
FF(5,j) = fcm[mappings[j - 1]-1][1]*ConFac;
FF(6,j) = fcm[mappings[j - 1]-1][2]*ConFac;
}
//------------------------------------//
// Get a solver and solve that system.
Solver * theSolver = Solver::GetSolver((SolverType)system[i].solver);
theSolver->SetSystem(system[i].system);
theSolver->Solve(time, FF);
theSolver->Solve(time, FF);
ColMatrix tempx = *((*theSolver).GetState());
ColMatrix tempv = *((*theSolver).GetStateDerivative());
ColMatrix tempa = *((*theSolver).GetStateDerivativeDerivative());
for(int numint =0 ; numint<3; numint++){
theSolver->Solve(time, FF);
tempa = *((*theSolver).GetStateDerivativeDerivative());
*((*theSolver).GetStateDerivative())= tempv + Thalf*tempa;
}
ColMatrix TempV= *((*theSolver).GetStateDerivative());
*((*theSolver).GetState())= tempx + Tfull*TempV;
int numjoints = system[i].system->joints.GetNumElements();
for(int k = 0; k < numjoints; k++){
system[i].system->joints(k)->ForwardKinematics();
}
for(int k = 0; k < numbodies; k++){
Vect3 temp1 =system[i].system->bodies(k+1)->r;
Vect3 temp2 =system[i].system->bodies(k+1)->v;
Vect3 temp3 =system[i].system->bodies(k+1)->omega;
Mat3x3 temp4 =system[i].system->bodies(k+1)->n_C_k;
for(int m = 0; m < 3; m++){
xcm[mappings[k]-1][m] = temp1(m+1);
vcm[mappings[k]-1][m] = temp2(m+1);
omega[mappings[k]-1][m] = temp3(m+1);
ex_space[mappings[k]-1][m] = temp4(m+1,1);
ey_space[mappings[k]-1][m] = temp4(m+1,2);
ez_space[mappings[k]-1][m] = temp4(m+1,3);
}
SysKE = SysKE + system[i].system->bodies(k+1)->KE;
}
delete theSolver;
}
}
void Workspace::LobattoTwo(double **&vcm,double **&omega,double **&torque, double **&fcm){
int numsys = currentIndex;
int numbodies;
double time = 0.0;
int * mappings;
double SysKE =0.0;
for (int i = 0; i <= numsys; i++){
mappings = system[i].system->GetMappings();
numbodies = system[i].system->GetNumBodies() - 1;
Matrix FF(6,numbodies);
for (int j=1; j<=numbodies; j++){
FF(1,j) = torque[mappings[j - 1]-1][0]*ConFac;
FF(2,j) = torque[mappings[j - 1]-1][1]*ConFac;
FF(3,j) = torque[mappings[j - 1]-1][2]*ConFac;
FF(4,j) = fcm[mappings[j - 1]-1][0]*ConFac;
FF(5,j) = fcm[mappings[j - 1]-1][1]*ConFac;
FF(6,j) = fcm[mappings[j - 1]-1][2]*ConFac;
}
//------------------------------------//
// Get a solver and solve that system.
Solver * theSolver = Solver::GetSolver((SolverType)system[i].solver);
theSolver->SetSystem(system[i].system);
theSolver->Solve(time, FF);
ColMatrix tempv = *((*theSolver).GetStateDerivative());
ColMatrix tempa = *((*theSolver).GetStateDerivativeDerivative());
*((*theSolver).GetStateDerivative()) = tempv + Thalf*tempa;
int numjoints = system[i].system->joints.GetNumElements();
for(int k = 0; k < numjoints; k++){
system[i].system->joints(k)->ForwardKinematics();
}
for(int k = 0; k < numbodies; k++){
Vect3 temp1 =system[i].system->bodies(k+1)->r;
Vect3 temp2 =system[i].system->bodies(k+1)->v;
Vect3 temp3 =system[i].system->bodies(k+1)->omega;
SysKE = SysKE + system[i].system->bodies(k+1)->KE;
for(int m = 0; m < 3; m++){
vcm[mappings[k]-1][m] = temp2(m+1);
omega[mappings[k]-1][m] = temp3(m+1);
}
}
delete theSolver;
}
}
void Workspace::RKStep(double **&xcm, double **&vcm,double **&omega,double **&torque, double **&fcm, double **&ex_space, double **&ey_space, double **&ez_space){
double a[6];
double b[6][6];
double c[6];
//double e[6];
a[1] = 0.2;
a[2] = 0.3;
a[3] = 0.6;
a[4] = 1.0;
a[5] = 0.875;
b[1][0] = 0.2;
b[2][0] = 0.075; b[2][1] = 0.225;
b[3][0] = 0.3; b[3][1] = -0.9; b[3][2] = 1.2;
b[4][0] = -11.0/54.0; b[4][1] = 2.5; b[4][2] = -70.0/27.0; b[4][3] = 35.0/27.0;
b[5][0] = 1631.0/55296.0; b[5][1] = 175.0/512.0; b[5][2] = 575.0/13824.0; b[5][3] = 44275.0/110592.0; b[5][4] = 253.0/4096.0;
c[0] = 37.0/378.0;
c[1] = 0.0;
c[2] = 250.0/621.0;
c[3] = 125.0/594.0;
c[4] = 0.0;
c[5] = 512.0/1771.0;
int numsys = currentIndex;
int numbodies;
double time = 0.0;
int * mappings;
double SysKE =0.0;
for (int i = 0; i <= numsys; i++){
mappings = system[i].system->GetMappings();
numbodies = system[i].system->GetNumBodies() - 1;
Matrix FF(6,numbodies);
for (int j=1; j<=numbodies; j++){
FF(1,j) = ConFac*torque[mappings[j - 1]-1][0];
FF(2,j) = ConFac*torque[mappings[j - 1]-1][1];
FF(3,j) = ConFac*torque[mappings[j - 1]-1][2];
FF(4,j) = ConFac*fcm[mappings[j - 1]-1][0];
FF(5,j) = ConFac*fcm[mappings[j - 1]-1][1];
FF(6,j) = ConFac*fcm[mappings[j - 1]-1][2];
}
//------------------------------------//
// Get a solver and solve that system.
Solver * theSolver = Solver::GetSolver((SolverType)system[i].solver);
theSolver->SetSystem(system[i].system);
theSolver->Solve(time, FF);
ColMatrix initial_x;
ColMatrix initial_xdot;
ColMatMap* x;
ColMatMap* xdot;
ColMatMap* xdotdot;
x = theSolver->GetState();
xdot = theSolver->GetStateDerivative();
xdotdot=theSolver->GetStateDerivativeDerivative();
initial_x = *x;
initial_xdot = *xdot;
ColMatrix f[6];
ColMatrix ff[6];
ff[0] = initial_xdot;
f[0] = *xdotdot;
for(int ii=1;ii<6;ii++){
time = a[ii] * Tfull;
(*x) = initial_x;
(*xdot) = initial_xdot;
for(int j=0;j<ii;j++){
(*x) = (*x) + (b[ii][j]*Tfull)*ff[j];
(*xdot) = (*xdot) + (b[ii][j]*Tfull)*f[j];
}
theSolver->Solve(time,FF);
f[ii] = (*xdotdot);
ff[ii] = (*xdot);
}
(*x) = initial_x + (c[0]*Tfull)*ff[0] + (c[2]*Tfull)*ff[2] + (c[3]*Tfull)*ff[3] + (c[5]*Tfull)*ff[5];
(*xdot) = initial_xdot + (c[0]*Tfull)*f[0] + (c[2]*Tfull)*f[2] + (c[3]*Tfull)*f[3] + (c[5]*Tfull)*f[5];
int numjoints = system[i].system->joints.GetNumElements();
for(int k = 0; k < numjoints; k++){
system[i].system->joints(k)->ForwardKinematics();
}
for(int k = 0; k < numbodies; k++){
Vect3 temp1 =system[i].system->bodies(k+1)->r;
Vect3 temp2 =system[i].system->bodies(k+1)->v;
Vect3 temp3 =system[i].system->bodies(k+1)->omega;
Mat3x3 temp4 =system[i].system->bodies(k+1)->n_C_k;
SysKE = SysKE + system[i].system->bodies(k+1)->KE;
for(int m = 0; m < 3; m++){
xcm[mappings[k]-1][m] = temp1(m+1);
vcm[mappings[k]-1][m] = temp2(m+1);
omega[mappings[k]-1][m] = temp3(m+1);
ex_space[mappings[k]-1][m] = temp4(m+1,1);
ey_space[mappings[k]-1][m] = temp4(m+1,2);
ez_space[mappings[k]-1][m] = temp4(m+1,3);
}
}
delete theSolver;
}
}
void Workspace::WriteFile(char * filename){
int numsys = currentIndex;
int numbodies;
for (int i = 0; i <= numsys; i++){
numbodies = system[i].system->GetNumBodies() - 1;
ofstream outfile;
outfile.open(filename,ofstream::out | ios::app);
outfile << numbodies<<endl;
outfile << "Atoms "<<endl;
for(int k = 0; k < numbodies; k++){
Vect3 temp1 =system[i].system->bodies(k+1)->r;
outfile<<1<<"\t"<<temp1(1)<<"\t"<<temp1(2)<<"\t"<<temp1(3)<<endl;
}
outfile.close();
}
}
|