-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
571 lines (543 loc) · 18.5 KB
/
test.cpp
File metadata and controls
571 lines (543 loc) · 18.5 KB
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
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include "matrixops.h"
#include "node.h"
/*
#define DTau (2.5e-5)
#define DTSQUARED (6.25e-10)
#define DAMPING 1
#define STIFFNESS 100
*/
using namespace std;
//Global Variables
double DTau;
double TFinal;
double DTauSqd;
double TwiceDTau;
double *CurDisplacement;
double *LastDisplacement;
double *NextDisplacement;
struct ForceSinusoid
{
public:
double frequency;
double amplitude;
};
struct Force
{
public:
int type; //0 for constant force, 1 for sinusoidal, 2 for ramp
ForceSinusoid FS;
double curval;
double multiplier;
};
void ForcePrint(Force*array,int num)
{
cout << endl;
for(int i = 0;i<num;i++)
{
cout << array[i].curval << endl;
}
}
//Select is used to choose double derivative, derivative, or proportional (2,1, or 0)
void selfrefAssemble(Node*list,double**m,int select, int NCNT, int size, int DOF)
{
zero(m,size);
for(int i=0;i<NCNT;i++)
{
if(!(list[i].getselffactor(select) <= 1e-5))
{
for(int j=0; j<DOF;j++)
{
m[i*DOF+j][i*DOF+j]=list[i].getselffactor(select);
}
}
}
}
//Select is used to choose double derivative, derivative, or proportional (2,1, or 0)
//It is assumed that the dependant variable (ex. force) is only transferred axially
//transformation matrix is passed as a parameter so that it can be used multiple
//times without recalculating
void coupledAssemble(Node*list,double**c,int select, int NCNT, int DoF)
{
zero(c,DoF*NCNT);
double**transformation = CreateMatrix(DoF*2);
for(int cnt=0;cnt<NCNT;cnt++)
{
for(int cnum=0;cnum<list[cnt].getconn();cnum++)
{
if(list[cnt].getconnfactor(cnum,select) >= 1e-5)
{
list[cnt].gettransformation(list[list[cnt].connto(cnum)], transformation);
addLocToGlo(c,transformation,cnt,list[cnt].connto(cnum),DoF,list[cnt].getconnfactor(cnum,select));
}
}
}
DeleteMatrix(DoF*2,transformation);
}
// matrices named for M, C, and K for visualization purposes
void GAssemble(int size,double**M,double**C,double**K,double*G,Force*forces)
{
for(int i=0;i<size;i++)
{
G[i] = forces[i].curval;
for(int j=0;j<size;j++)
{
G[i] += ((2*M[i][j]/DTauSqd)-K[i][j])*CurDisplacement[j] + ((C[i][j]/TwiceDTau) - (M[i][j]/DTauSqd))*LastDisplacement[j];
}
}
}
void AAssemble(int size,double**M,double**C,double**A)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
A[i][j] = (M[i][j]/DTauSqd+C[i][j]/TwiceDTau);
}
}
}
int main()
{
//Initialization.
int NCNT, DoF, numFixed = 0;
int scratch;
int *nodesfixed;
Node*nodes = NULL;
Force*exforce = NULL;
bool prob3 = false, prob4 = false, freaksweep = false, grav = false;
int size;
ofstream output ("Output.txt");
//set up timestep values
cout << "This Program solves a system of 2nd order (no bending allowed) constant term ODEs using FEM" << endl;
cout << "Enter the timestep in seconds." << endl;
cin >> DTau;
cout << "Enter last time to solve for." << endl;
cin >> TFinal;
cout << "Enter 1 for gravity, 0 for no gravity"
<< endl << "(Gravity is assumed to act in the negative y direction)" << endl;
cin >> grav;
cout<<"Enter 4 to automatically set up the system in problem 4"<<endl;
cin >> scratch;
if(scratch == 4)
{
prob4 = true;
NCNT = 7;
DoF = 2;
size = 14;
nodes = new Node[NCNT];
for (int i=0;i<NCNT;i++)
{
nodes[i].initProb4(i);
}
exforce = new Force[14];
cout << "Enter 1 for sin force, 2 for ramp," << endl << "and 3 to perform a frequency sweep" << endl;
cin >> scratch;
for(int i=0;i<14;i++)
{
exforce[i].type = 0;
exforce[i].curval = 0;
}
if(scratch == 1)
{
exforce[12].type = 1;
exforce[12].FS.amplitude = 10;
cout << "Input Frequency" << endl;
cin >> exforce[12].FS.frequency;
}
else if(scratch == 2)
{
exforce[12].type = 2;
exforce[12].multiplier = 20;
}
else if(scratch == 3)
{
freaksweep = true;
exforce[12].type = 1;
exforce[12].FS.amplitude = 10;
}
numFixed = 2;
nodesfixed = new int[4];
nodesfixed[0] = 0;
nodesfixed[1] = 1;
nodesfixed[2] = 8;
nodesfixed[3] = 9;
scratch = 4;
}
else
{
cout << "enter # of nodes: ";
cin >> NCNT;
DoF = 2;
nodes = new Node[NCNT];
for (int i=0;i<NCNT;i++)
{
nodes[i].init(i,DoF);
}
size = NCNT*DoF;
exforce = new Force[size];
for(int i = 0;i<size;i++)
{
cout << "Input Type of Forced Dependant for Node" << i << endl << "0 for constant" << endl << "1 for sinusoidally varying" << endl << "2 for ramp" << endl;
cin >> exforce[i].type;
if(exforce[i].type == 0)
{
cout<< "Input Constant term" << endl;
}
else if(exforce[i].type == 1)
{
cout << "Input Amplitude" << endl;
cin >> exforce[i].FS.amplitude;
cout << "Input Frequency" << endl;
cin >> exforce[i].FS.frequency;
}
else
{
cout << "Input Factor" <<endl;
cin >> exforce[i].multiplier;
}
}
//if time will reorganize this so it's not so inefficient
for (int cnt = 0;cnt<NCNT;cnt++)
{
if(nodes[cnt].isFixed())
{
numFixed++;
}
}
nodesfixed = new int[numFixed*2];
for (int cnt = 0;cnt<NCNT;cnt++)
{
int cntr = 0;
if(nodes[cnt].isFixed())
{
nodesfixed[cntr] = cnt*DoF;
nodesfixed[cntr+1] = cnt*DoF + 1;
}
}
}
//allocate and zero out
CurDisplacement = new double[size];
LastDisplacement = new double[size];
NextDisplacement = new double[size];
double *G = new double[size];
double **A = CreateMatrix(size);
double **Ks = CreateMatrix(size);
double **Cs = CreateMatrix(size);
double **Ms = CreateMatrix(size);
double **Kc = CreateMatrix(size);
double **Cc = CreateMatrix(size);
double **Mc = CreateMatrix(size);
double updatevector[DoF];
double **submatrix = CreateMatrix(size-(DoF*numFixed));
double *subG = new double[size-(DoF*numFixed)];
double *subnextdis = new double[size-(DoF*numFixed)];
double *velocity = new double[size];
double *acceleration = new double[size];
double *amplitude = new double[size];
double *mean = new double[size];
double *peak = new double[size];
for(int i=0;i<size;i++)
{
CurDisplacement[i] = 0;
LastDisplacement[i] = 0;
NextDisplacement[i] = 0;
velocity[i] = 0;
acceleration[i] = 0;
amplitude[i] = 0;
mean[i] = 0;
peak[i] = -1e20;
if(grav && ((i&1) == 1))
{
exforce[i].curval -= 9.80665*nodes[(i/DoF)].getselffactor(2); //exploiting integer division here
}
}
//first the selfreferential elements
//they don't change
selfrefAssemble(nodes,Ks,0,NCNT,size,DoF);
selfrefAssemble(nodes,Cs,1,NCNT,size,DoF);
selfrefAssemble(nodes,Ms,2,NCNT,size,DoF);
int fixindx;
int lj;
//set up time
int cycles = ceil(TFinal/DTau)+1;
int plotinterval = cycles/5000; //plotting millions of points is insane
if(plotinterval<1)
{
plotinterval = 1;
}
DTauSqd = DTau*DTau; //Precalculate repeatedly used values
TwiceDTau = DTau*2;
double T1;
//calculate the maximum value that may be used to get a frequency amplitude
if(prob4)
{
if(exforce[12].type == 1)
{
T1 = TFinal - (2*pi)/exforce[12].FS.frequency;
}
}
if(!freaksweep)
{
for (int i = 0;i<cycles;i++)
{
//now the coupled elements
coupledAssemble(nodes,Kc,0,NCNT,DoF);
coupledAssemble(nodes,Cc,1,NCNT,DoF);
coupledAssemble(nodes,Mc,2,NCNT,DoF);
addm(Kc,Ks,Kc,size,size);
addm(Cc,Cs,Cc,size,size);
addm(Mc,Ms,Mc,size,size);
fixindx = 0;
for(int j=0;j<size;j++)
{
if(exforce[j].type != 0) //if the force is external varying
{
if(exforce[j].type == 1) //sinusoid
{
if(j&1)
{
exforce[j].curval = (exforce[j].FS.amplitude)*sin((exforce[j].FS.frequency)*((i)*DTau)) - grav*(9.80665*nodes[(j/DoF)].getselffactor(2));
}
else
{
exforce[j].curval = (exforce[j].FS.amplitude)*sin((exforce[j].FS.frequency)*((i)*DTau));
}
}
else //ramp
{
if(j&1)
{
exforce[j].curval = (exforce[j].multiplier)*((i)*DTau) - grav*(9.80665*nodes[(j/DoF)].getselffactor(2));
}
else
{
exforce[j].curval = (exforce[j].multiplier)*((i)*DTau);
}
}
}
}
GAssemble(size,Mc,Cc,Kc,G,exforce);
AAssemble(size,Mc,Cc,A);
createsubmatrix(A, submatrix,size,nodesfixed);
createsubG(G,subG,size,nodesfixed);
lud(submatrix,subG,(size-(DoF*numFixed)),subnextdis);
//put next displacements into main vector
fixindx = 0;
lj = 0;
for(int j=0;j<(size-(DoF*numFixed));j++)
{
while(nodesfixed[fixindx] == lj)
{
lj++;
fixindx++;
if(lj >= size)
{
continue;
}
}
NextDisplacement[lj] = subnextdis[j];
lj++;
}
//update velocity and acceleration
for(int j=0;j<size;j++)
{
velocity[j] = (NextDisplacement[j]-LastDisplacement[j])/TwiceDTau;
acceleration[j] = ((NextDisplacement[j]-2*CurDisplacement[j]+LastDisplacement[j])/DTauSqd);
}
//check for peak and sum for mean if needed
if(prob4)
{
if((exforce[12].type == 1) && (i*DTau >= T1))
{
for(int k = 0;k<size;k++)
{
if(peak[k] < CurDisplacement[k])
{
peak[k] = CurDisplacement[k];
}
}
freqsweep(CurDisplacement,LastDisplacement,mean,DTau,exforce[12].FS.frequency,size);
}
}
if((i%plotinterval) == 0)
{
if(prob4)
{
if(exforce[12].type == 1)
{
if(i)
output << CurDisplacement[4] << " , " << CurDisplacement[5] << " , "
<< velocity[4] << " , " << velocity[5] << " , "
<< acceleration[4] << " , " << acceleration[5]
<< " , " << (i*DTau) << endl;
}
else
{
for(int p = 0;p<NCNT;p++)
{
for(int d = 0; d<DoF;d++)
{
output << CurDisplacement[p*DoF+d] << " , " ;
}
for(int d = 0; d<DoF;d++)
{
output << velocity[p*DoF+d] << " , ";
}
for(int d = 0; d<DoF;d++)
{
output << acceleration[p*DoF+d] << " , ";
}
}
output << (i*DTau) << endl;
}
}
else if (prob3)
{
output << CurDisplacement[1] << " , "<< CurDisplacement[2]
<< " , " << CurDisplacement[4] << " , " << (i*DTau) << endl;
}
}
//Shift Indices
for(int j=0;j<NCNT;j++)
{
for(int d=0;d<DoF;d++)
{
updatevector[d] = NextDisplacement[(DoF*j+d)];
LastDisplacement[(j*DoF + d)] = CurDisplacement[(j*DoF + d)];
CurDisplacement[(j*DoF + d)] = NextDisplacement[(j*DoF + d)];
}
nodes[j].UpdateDelta(updatevector);
}
}
for(int i=0; i<NCNT;i++)
{
for(int d=0;d<DoF;d++)
{
amplitude[i*DoF+d] = abs(peak[i*DoF+d]-mean[i*DoF+d]);
cout << "Amplitude for Node " << i << ", dim " << d << " is " << amplitude[i*DoF+d] << endl
<< "Mean for Node " << i << ", dim " << d << " is " << mean[i*DoF+d] << endl
<< "Peak for Node " << i << ", dim " << d << " is " << peak[i*DoF+d] << endl << endl;
}
}
}
else
{
for(exforce[12].FS.frequency = 1;exforce[12].FS.frequency < 150.1;exforce[12].FS.frequency += 0.50)
{
TFinal = 25 + (2*pi)/exforce[12].FS.frequency;
cycles = ceil(TFinal/DTau)+1;
DTauSqd = DTau*DTau; //Precalculate repeatedly used values
TwiceDTau = DTau*2;
for (int i = 0;i<cycles;i++)
{
//now the coupled elements
coupledAssemble(nodes,Kc,0,NCNT,DoF);
coupledAssemble(nodes,Cc,1,NCNT,DoF);
coupledAssemble(nodes,Mc,2,NCNT,DoF);
addm(Kc,Ks,Kc,size,size);
addm(Cc,Cs,Cc,size,size);
addm(Mc,Ms,Mc,size,size);
fixindx = 0;
for(int j=0;j<size;j++)
{
if(exforce[j].type != 0) //if the force is external varying
{
if(exforce[j].type == 1) //sinusoid
{
if(j&1)
{
exforce[j].curval = (exforce[j].FS.amplitude)*sin((exforce[j].FS.frequency)*((i)*DTau)) - grav*(9.80665*nodes[(j/DoF)].getselffactor(2));
}
else
{
exforce[j].curval = (exforce[j].FS.amplitude)*sin((exforce[j].FS.frequency)*((i)*DTau));
}
}
else //ramp
{
if(j&1)
{
exforce[j].curval = (exforce[j].multiplier)*((i)*DTau) - grav*(9.80665*nodes[(j/DoF)].getselffactor(2));
}
else
{
exforce[j].curval = (exforce[j].multiplier)*((i)*DTau);
}
}
}
}
GAssemble(size,Mc,Cc,Kc,G,exforce);
AAssemble(size,Mc,Cc,A);
createsubmatrix(A, submatrix,size,nodesfixed);
createsubG(G,subG,size,nodesfixed);
lud(submatrix,subG,(size-(DoF*numFixed)),subnextdis);
//put next displacements into main vector
fixindx = 0;
lj = 0;
for(int j=0;j<(size-(DoF*numFixed));j++)
{
while(nodesfixed[fixindx] == lj)
{
lj++;
fixindx++;
if(lj >= size)
{
continue;
}
}
NextDisplacement[lj] = subnextdis[j];
lj++;
}
//update velocity and acceleration
for(int j=0;j<size;j++)
{
velocity[j] = (NextDisplacement[j]-LastDisplacement[j])/TwiceDTau;
acceleration[j] = ((NextDisplacement[j]-2*CurDisplacement[j]+LastDisplacement[j])/DTauSqd);
}
if(i*DTau >= 25)
{
for(int k = 0;k<size;k++)
{
if(peak[k] < CurDisplacement[k])
{
peak[k] = CurDisplacement[k];
}
}
freqsweep(CurDisplacement,LastDisplacement,mean,DTau,exforce[12].FS.frequency,size);
}
//Shift Indices
for(int j=0;j<NCNT;j++)
{
for(int d=0;d<DoF;d++)
{
updatevector[d] = NextDisplacement[(DoF*j+d)];
LastDisplacement[(j*DoF + d)] = CurDisplacement[(j*DoF + d)];
CurDisplacement[(j*DoF + d)] = NextDisplacement[(j*DoF + d)];
}
nodes[j].UpdateDelta(updatevector);
}
}
for(int i=0; i<NCNT;i++)
{
for(int d=0;d<DoF;d++)
{
amplitude[i*DoF+d] = abs(peak[i*DoF+d]-mean[i*DoF+d]);
output<< amplitude[i*DoF+d] << " , ";
mean[i*DoF+d] = 0;
peak[i*DoF+d] = -1e20;
CurDisplacement[i*DoF+d] = 0;
LastDisplacement[i*DoF+d] = 0;
NextDisplacement[i*DoF+d] = 0;
}
}
output << exforce[12].FS.frequency << endl;
}
}
cout << "no seg faults" << endl;
cin.get();
return 0;
}