forked from csatterwhite/CPanel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpCaseVP.cpp
More file actions
2348 lines (2092 loc) · 70.8 KB
/
cpCaseVP.cpp
File metadata and controls
2348 lines (2092 loc) · 70.8 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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// cpCaseVP.cpp
// CPanel
//
// Created by Connor Sousa on 1/31/17.
// Copyright (c) 2017 Chris Satterwhite. All rights reserved.
//
#include "cpCaseVP.h"
void cpCaseVP::run(bool printFlag, bool surfStreamFlag, bool stabDerivFlag){
if(unsteady){
readBodyKinFile();
solnMat.resize(numSteps, 12);
}
bool matrix_convergence = false;
std::string check = "\u2713";
std::setprecision(5);
if(printFlag) std::cout << std::setw(11) << " Time step " << std::to_string(timeStep) + "/" + std::to_string(numSteps) + ". " << "Flow time = " << std::setw(5) << std::to_string(timeStep*dt) + ". " << std::endl;
// First time step
setSourceStrengths();
matrix_convergence = solveMatrixEq();
if (unsteady) compVelocity();
writeFilesVP();
// moveGeometry();
// Initially convect buffer wake and solve
timeStep++;
if(printFlag) std::cout << std::setw(11) << " Time step " << std::to_string(timeStep) + "/" + std::to_string(numSteps) + ". " << "Flow time = " << std::setw(5) << std::to_string(timeStep*dt) + ". " << std::endl;
convectBufferWake();
matrix_convergence = solveVPmatrixEq();
if (unsteady) compVelocity();
writeFilesVP();
// moveGeometry();
// For rest of timesteps
while (timeStep < numSteps){
timeStep++;
if(printFlag) std::cout << std::setw(11) << " Time step " << (std::to_string(timeStep) + "/" + std::to_string(numSteps) + ". ") << "Flow time = " << std::setw(8) << std::to_string(timeStep*dt) + ". " << particles.size() << " particles" << std::endl;
convectParticles();
// Turn second row of wake panels into particles
collapseBufferWake();
// Move Geometry
// moveGeometry();
// Advance first row of wake panels to second row
convectBufferWake();
// Influence of body and particles onto particles
particleStrengthUpdate();
// Build/update particle octree
if(accelerate){
partOctree.removeData();
partOctree.setMaxMembers(10); //Barnes Hut
partOctree.addData(particles);
FMM.build(&partOctree);
}
// Influence of particles onto body
setSourceStrengthsVP();
// Solve system of equations
matrix_convergence = solveVPmatrixEq();
// Check for simulation convergence
if(!matrix_convergence){
std::cout << "*** Warning : Matrix solution did not converge ***" << std::endl;
std::exit(0);
}else{
bool soln_conv = solutionConvergence();
if (soln_conv) {
if(printFlag) std::cout << "Converged..." << std::endl;
break;
}
}
if(unsteady) compVelocity();
writeFilesVP();
}
if(printFlag) std::cout << "\n (\u2713 - Complete, X - Not Requested)\n"<< std::setw(17) << std::left << " Solve System" << std::setw(17) << std::left << "Surface Data" << std::setw(18) << std::left << "Trefftz Plane" << std::setw(14) << std::left << "Streamlines" << std::setw(24) << std::left << "Stability Derivatives" << std::setw(23) << std::left << "Volume Mesh" << std::endl << std::setw(19) << std::left << " " + check << std::flush;
if(!unsteady) compVelocity(); // calculated each time step in unsteady
if(printFlag) std::cout << std::setw(19) << std::left << check << std::flush;
trefftzPlaneAnalysisVP();
if(printFlag) std::cout << std::setw(20) << std::left << check << std::flush;
if (surfStreamFlag)
{
createStreamlines();
if(printFlag) std::cout << std::setw(14) << std::left << check << std::flush;
}
else
{
if(printFlag) std::cout << std::setw(14) << std::left << "X" << std::flush;
}
if (stabDerivFlag)
{
stabilityDerivativesVP();
if(printFlag) std::cout << std::setw(24) << std::left << check << std::flush;
}
else
{
if(printFlag) std::cout << std::setw(24) << std::left << "X" << std::flush;
}
if (params->volMeshFlag) {
createVolMesh();
populateVolMesh();
if (printFlag) std::cout << std::setw(23) << std::left << check << std::endl;
}
else
{
if (printFlag) std::cout << std::setw(23) << std::left << "X" << std::endl;
}
if (!matrix_convergence && printFlag)
{
std::cout << "*** Warning : Solution did not converge ***" << std::endl;
}
if (printFlag) writeFilesVP();
}
void cpCaseVP::convectBufferWake(){
wake2Doublets.resize((*w2panels).size());
for (int i=0; i<(*w2panels).size(); i++)
{
(*w2panels)[i]->setPrevStrength((*w2panels)[i]->getMu());
double parentMu = (*w2panels)[i]->getBufferParent()->getMu();
(*w2panels)[i]->setMu(parentMu);
wake2Doublets[i] = parentMu;
}
}
void cpCaseVP::setSourceStrengthsVP(){
sigmas.resize(bPanels->size());
for (int i=0; i<bPanels->size(); i++)
{
Eigen::Vector3d sumVelInfl = Eigen::Vector3d::Zero();
// Influence from particles
if (accelerate){
sumVelInfl += FMM.barnesHutVel((*bPanels)[i]->getCenter());
}
else
{
for(int j=0; j<particles.size(); j++)
{
sumVelInfl += particles[j]->partVelInflGaussian((*bPanels)[i]->getCenter());
}
}
// Influence from vortex filaments
for(int j=0; j<filaments.size(); j++)
{
sumVelInfl += filaments[j]->velInfl( (*bPanels)[i]->getCenter() );
}
(*bPanels)[i]->setSigma( Vinf((*bPanels)[i]->getCenter()) + sumVelInfl , 0 );
sigmas(i) = (*bPanels)[i]->getSigma();
}
}
bool cpCaseVP::solutionConvergence(){
if(unsteady | (timeStep < 5) | params->stepsSetMaunally){
return false;
}
// Check for simulation convergence. Will use the Trefftz plane because it is so cheap. Medium size mesh takes <1% of time step time.
// Store previous time step values
double prevCD = CD_trefftz;
double prevCL = CL_trefftz;
trefftzPlaneAnalysisVP();
double changeCD = std::abs((CD_trefftz - prevCD)/prevCD);
double changeCL = std::abs((CL_trefftz - prevCL)/prevCL);
double val = 0.0001; // 0.01%
if((changeCL < val) && (changeCD < val)){
return true;
}
return false;
}
bool cpCaseVP::solveMatrixEq(){
bool converged = true;
Eigen::MatrixXd* A = geom->getA();
Eigen::MatrixXd* B = geom->getB();
Eigen::VectorXd RHS = -(*B)*sigmas;
Eigen::VectorXd doubletStrengths(bPanels->size());
Eigen::BiCGSTAB<Eigen::MatrixXd> res;
res.compute((*A));
doubletStrengths = res.solve(RHS);
if (res.error() > pow(10,-10))
{
converged = false;
}
for (int i=0; i<bPanels->size(); i++)
{
(*bPanels)[i]->setMu(doubletStrengths(i));
(*bPanels)[i]->setPotential(Vinf((*bPanels)[i]->getCenter()));
}
for (int i=0; i<wPanels->size(); i++)
{
(*wPanels)[i]->setMu();
(*wPanels)[i]->setPotential(Vinf((*wPanels)[i]->getCenter()));
}
return converged;
}
bool cpCaseVP::solveVPmatrixEq(){
bool converged = true;
Eigen::MatrixXd* A = geom->getA();
Eigen::MatrixXd* B = geom->getB();
Eigen::MatrixXd* C = geom->getC();
Eigen::VectorXd RHS = -(*B)*(sigmas) - (*C)*(wake2Doublets);
Eigen::VectorXd doubletStrengths(bPanels->size());
Eigen::BiCGSTAB<Eigen::MatrixXd> res;
res.compute((*A));
doubletStrengths = res.solve(RHS);
if (res.error() > pow(10,-10))
{
converged = false;
}
for (int i=0; i<bPanels->size(); i++)
{
(*bPanels)[i]->setMu(doubletStrengths(i));
(*bPanels)[i]->setPotential(VinfPlusVecPot((*bPanels)[i]->getCenter()));
}
for (int i=0; i<wPanels->size(); i++)
{
(*wPanels)[i]->setPrevStrength((*wPanels)[i]->getMu());
(*wPanels)[i]->setMu();
(*wPanels)[i]->setPotential(VinfPlusVecPot((*wPanels)[i]->getCenter()));
(*w2panels)[i]->setPotential(VinfPlusVecPot((*w2panels)[i]->getCenter())); // Included in this loop because there are always the same number of w2pans as w1pans
}
return converged;
}
bool cpCaseVP::edgeIsUsed(edge* thisEdge, std::vector<edge*> pEdges){
for(int i=0; i<pEdges.size(); i++){
if(thisEdge == pEdges[i]){
return true;
}
}
return false;
}
void cpCaseVP::collapseBufferWake(){
std::vector<edge*> usedEdges;
for(int i=0; i<(*w2panels).size(); i++)
{
std::vector<edge*> pEdges = (*w2panels)[i]->edgesInOrder();
Eigen::Vector3d strength = Eigen::Vector3d::Zero();
for (int j=1; j<4; j++)
{
if (!edgeIsUsed(pEdges[j],usedEdges))
{
usedEdges.push_back(pEdges[j]);
strength += (*w2panels)[i]->edgeStrength( pEdges[j], j ); // Don't need to pass in pEdges...
}
}
Eigen::Vector3d pos = rungeKuttaStepper((*w2panels)[i]->getCenter());
// Eigen::Vector3d pos = (*w2panels)[i]->getCenter(); // For moving geometry
Eigen::Vector3d ptVel = Vinf(pos);
double radius = (*w2panels)[i]->getPartRadius(ptVel,dt); // VinfLocal
particle* p = new particle(pos, strength, radius, {0,0,0}, {0,0,0}, timeStep); // Zeroes are previous pos and strength values used for Adams-Bashforth scheme
p->parentPanel = (*w2panels)[i];
particles.push_back(p);
}
// Create filament
if(filaments.size() == 0)
{
for(int i=0; i<(*w2panels).size(); i++)
{
vortexFil* fil;
Eigen::Vector3d p1,p2;
p1 = (*w2panels)[i]->pointsInOrder()[2]->getPnt();
p2 = (*w2panels)[i]->pointsInOrder()[3]->getPnt();
fil = new vortexFil(p1, p2,-(*w2panels)[i]->getMu(), (*w2panels)[i]); // Negative strength is because filament is actually the upstream edge being convected which is oriented the opposite direction as downstream edge
filaments.push_back(fil);
(*w2panels)[i]->setVortFil(fil);
}
}
else
{
for(int i=0; i<(*w2panels).size(); i++){
filaments[i]->setStrength(-(*w2panels)[i]->getMu()); // Negative strength is because filament is actually the upstream edge being convected which is oriented the opposite direction as downstream edge
}
}
}
void cpCaseVP::compVelocity(){
// Velocity Survey with known doublet and source strengths
CM.setZero();
Eigen::Vector3d moment;
Fbody = Eigen::Vector3d::Zero();
bodyPanel* p;
for (int i=0; i<bPanels->size(); i++)
{
p = (*bPanels)[i];
p->computeVelocity(PG,Vinf(p->getCenter()));
if (unsteady) {
p->computeCp( Vinf((*bPanels)[i]->getCenter()).norm() , dt ); // Katz 13.169 ///
}else{
p->computeCp( Vmag );
}
Fbody += -p->getCp()*p->getArea()*p->getBezNormal()/params->Sref;
moment = p->computeMoments(params->cg);
CM(0) += moment(0)/(params->Sref*params->bref);
CM(1) += moment(1)/(params->Sref*params->cref);
CM(2) += moment(2)/(params->Sref*params->bref);
}
Fwind = bodyToWind(Fbody);
if(unsteady){
if(timeStep > 3) trefftzPlaneAnalysisVP(); // No convergence check for unsteady sims
// flow_time | CL_tr | CDi_tr | CN | CA | CY | CL | CD | CY | Cm | Cl | Cn |
solnMat(timeStep-1,0) = timeStep*dt;
solnMat(timeStep-1,1) = CL_trefftz;
solnMat(timeStep-1,2) = CD_trefftz;
solnMat(timeStep-1,3) = Fbody.x();
solnMat(timeStep-1,4) = Fbody.y();
solnMat(timeStep-1,5) = Fbody.z();
solnMat(timeStep-1,6) = Fwind.x();
solnMat(timeStep-1,7) = Fwind.y();
solnMat(timeStep-1,8) = Fwind.z();
solnMat(timeStep-1,9) = CM.x();
solnMat(timeStep-1,10) = CM.y();
solnMat(timeStep-1,11) = CM.z();
}
}
void cpCaseVP::trefftzPlaneAnalysisVP(){
std::vector<wake*> wakes = geom->getWakes();
CL_trefftz = 0;
CD_trefftz = 0;
for (int i=0; i<wakes.size(); i++)
{
wakes[i]->trefftzPlaneVP(Vmag,params->Sref, &particles, timeStep);
CL_trefftz += wakes[i]->getCL()/PG;
CD_trefftz += wakes[i]->getCD()/pow(PG,2);
}
}
void cpCaseVP::stabilityDerivativesVP()
{
double delta = 0.5;
double dRad = delta*M_PI/180;
cpCaseVP dA(geom,Vmag,alpha+delta,beta,mach,params);
cpCaseVP dB(geom,Vmag,alpha,beta+delta,mach,params);
dA.run(false,false,false);
dB.run(false,false,false);
Eigen::Vector3d FA = dA.getWindForces();
FA(2) = dA.getCL();
FA(0) = dA.getCD();
Eigen::Vector3d FB = dB.getWindForces();
FB(2) = dB.getCL();
FB(0) = dB.getCD();
Eigen::Vector3d F = Fwind;
F(2) = CL_trefftz;
F(0) = CD_trefftz;
// Finish calculating stability derivatives
dF_dAlpha = (FA-F)/dRad;
dF_dBeta = (FB-F)/dRad;
dM_dAlpha = (dA.getMoment()-CM)/dRad;
dM_dBeta = (dB.getMoment()-CM)/dRad;
}
void cpCaseVP::particleStrengthUpdate(){
// This function uses the update equations found in 'Vortex Methods for DNS of...' by Plouhmhans. It uses the particle strength exchange for the viscous diffusion
std::vector<Eigen::Vector3d> stretchDiffVec; // Creating vector values because the strength change needs to be set after all particle influences have been calculated
for(int i=0; i<particles.size(); i++)
{
Eigen::Vector3d dAlpha_diff = Eigen::Vector3d::Zero();
Eigen::Vector3d dAlpha_stretch = Eigen::Vector3d::Zero();
// FarField2 condition
if((particles[i]->pos-Eigen::Vector3d::Zero()).norm() > 20*params->cref)
{
stretchDiffVec.push_back(Eigen::Vector3d::Zero());
}
// FarField1 condition
else if((particles[i]->pos-Eigen::Vector3d::Zero()).norm() > 12*params->cref)
{
if(accelerate)
{
dAlpha_stretch = FMM.barnesHutStretch(particles[i]); ///
dAlpha_diff = FMM.barnesHutDiff(particles[i]);
}
else
{
// Stretching from particles
for(int j=0; j<particles.size(); j++)
{
if(i!=j) // Kroneger Delta Function
{
dAlpha_diff += particles[i]->viscousDiffusionGaussian(particles[j]);
dAlpha_stretch += particles[i]->vortexStretchingGaussian(particles[j]);
}
}
}
stretchDiffVec.push_back( dAlpha_diff + dAlpha_stretch );
}
else // Treat normally
{
// Influence from Particles
if(accelerate && timeStep > 3){
dAlpha_stretch += FMM.barnesHutStretch(particles[i]);
dAlpha_diff += FMM.barnesHutDiff(particles[i]);
}else
{
for(int j=0; j<particles.size(); j++)
{
if(i!=j){ // Kroneger Delta Function
dAlpha_diff += particles[i]->viscousDiffusionGaussian(particles[j]);
dAlpha_stretch += particles[i]->vortexStretchingGaussian(particles[j]);
}
}
}
// Stretching from body panels
for(int j=0; j<(*bPanels).size(); j++)
{
dAlpha_diff += (*bPanels)[j]->partStretching(particles[i]);
}
// Stretching from wake panels
for(int j=0;j<(*wPanels).size(); j++)
{
dAlpha_diff += (*wPanels)[j]->partStretching(particles[i]);
}
stretchDiffVec.push_back( dAlpha_diff + dAlpha_stretch );
}
}
for(int i=0;i<particles.size();i++)
{
Eigen::Vector3d newStrength;
if(particles[i]->getprevStrengthUpdate().isZero())
{
newStrength = particles[i]->strength + stretchDiffVec[i]*dt;
}else
{ // Adams bashforth
newStrength = particles[i]->strength + dt*(1.5*stretchDiffVec[i] - 0.5*particles[i]->getprevStrengthUpdate());
}
particles[i]->setprevStrengthUpdate(stretchDiffVec[i]);
particles[i]->setStrength(newStrength);
}
}
void cpCaseVP::convectParticles(){
std::vector<Eigen::Vector3d> newPartPositions;
for(int i=0;i<particles.size();i++){
Eigen::Vector3d newPos;
// Adams bashforth scheme
Eigen::Vector3d velOnPart = velocityInflFromEverything(particles[i]);
if(particles[i]->getPrevVelInfl().isZero())
{
newPos = particles[i]->pos + dt*velOnPart;
}
else{
newPos = particles[i]->pos + dt*(1.5*velOnPart - 0.5*particles[i]->getPrevVelInfl());
}
particles[i]->setPrevVelInfl(velOnPart);
newPartPositions.push_back(newPos);
// }
}
for(int i=0;i<particles.size();i++){
particles[i]->setPos(newPartPositions[i]);
}
}
Eigen::Vector3d cpCaseVP::Vinf(Eigen::Vector3d POI){
if (!unsteady)
{
return windToBody( Vmag , alpha , beta );
}
else
{
Eigen::Vector3d localVel;
// U = U3 + (-q*z + r*y)
localVel.x() = bodyKin(timeStep-1, 0) - bodyKin(timeStep-1, 4)*POI.z() + bodyKin(timeStep-1, 5)*POI.y();
// V = V3 + (-r*x + p*z)
localVel.y() = bodyKin(timeStep-1, 1) - bodyKin(timeStep-1, 5)*POI.x() + bodyKin(timeStep-1, 3)*POI.z();
// W = W3 + (-p*y + q*x)
localVel.z() = bodyKin(timeStep-1, 2) - bodyKin(timeStep-1, 3)*POI.y() + bodyKin(timeStep-1, 4)*POI.x();
return localVel;
}
}
Eigen::Vector3d cpCaseVP::VinfPlusVecPot(Eigen::Vector3d POI){
Eigen::Vector3d vInfluence = Vinf(POI);
// Particles
if(accelerate && timeStep > 2)
{
vInfluence += FMM.barnesHutVel(POI);
}else
{
for (int i=0; i<particles.size(); i++) {
vInfluence += particles[i]->partVelInflGaussian(POI);
}
}
// Filaments
for (int i=0; i<filaments.size(); i++)
{
vInfluence += filaments[i]->velInfl(POI);
}
return vInfluence;
}
Eigen::Vector3d cpCaseVP::rungeKuttaStepper( Eigen::Vector3d POI ){
// RK4 algorithm makes symmeterized influences VERY impractical
Eigen::Vector3d k1, k2, k3, k4;
k1 = velocityInflFromEverything(POI);
k2 = velocityInflFromEverything(POI+k1*dt/2);
k3 = velocityInflFromEverything(POI+k2*dt/2);
k4 = velocityInflFromEverything(POI+k3*dt);
return POI + dt*(k1/6 + k2/3 + k3/3 + k4/6);
}
Eigen::Vector3d cpCaseVP::velocityInflFromEverything( Eigen::Vector3d POI ){
// Freestream influence
Eigen::Vector3d velOnPart = Vinf(POI);
// Particle influence
if(accelerate && timeStep > 3){
velOnPart += FMM.barnesHutVel(POI);
}
else{
for(int j=0;j<particles.size();j++)
{
velOnPart += particles[j]->partVelInflGaussian(POI);
}
}
// Body panel influence
for(int j=0;j<(*bPanels).size();j++){
velOnPart += (*bPanels)[j]->panelV(POI);
}
// Buffer wake influence
for(int j=0;j<(*wPanels).size();j++){
velOnPart += (*wPanels)[j]->panelV(POI);
}
for(int j=0;j<(*w2panels).size();j++){
velOnPart += (*w2panels)[j]->panelV(POI);
}
// Vortex Filament influence
for(int i=0; i<filaments.size(); i++){
velOnPart +=filaments[i]->velInfl(POI);
}
// std::cout << velOnPart.x() << " , " << velOnPart.y() << " , " << velOnPart.z() << std::endl;
return velOnPart;
}
Eigen::Vector3d cpCaseVP::velocityInflFromEverything(particle* part){
// Function is overloaded for symmeterized velocity influence for particles
Eigen::Vector3d pos = part->pos;
// Freestream influence
Eigen::Vector3d velOnPart = Vinf(pos);
// If particle reaches very far field condition
if ((pos-Eigen::Vector3d::Zero()).norm() > 20*params->cref)
{
return velOnPart;
}
// Particle influence
if(accelerate && timeStep > 2){
velOnPart += FMM.barnesHutVel(part);
}
else{
for(int j=0;j<particles.size();j++)
{
velOnPart += particles[j]->partVelInflGaussian(pos);
}
}
// If particle reaches first farfield condition
if((pos-Eigen::Vector3d::Zero()).norm() > 12*params->cref)
{
return velOnPart;
}
// Body panel influence
for(int j=0;j<(*bPanels).size();j++){
velOnPart += (*bPanels)[j]->panelV(pos);
}
// Buffer wake influence
for(int j=0;j<(*wPanels).size();j++){
velOnPart += (*wPanels)[j]->panelV(pos);
}
for(int j=0;j<(*w2panels).size();j++){
velOnPart += (*w2panels)[j]->panelV(pos);
}
// Vortex Filament influence
for(int i=0; i<filaments.size(); i++){
velOnPart +=filaments[i]->velInfl(pos);
}
return velOnPart;
}
void cpCaseVP::writeFilesVP(){
std::stringstream caseLabel;
caseLabel << "/V" << Vmag << "_Mach" << mach << "_alpha" << alpha << "_beta" << beta;
boost::filesystem::path subdir = boost::filesystem::current_path().string()+caseLabel.str();
if (!boost::filesystem::exists(subdir))
{
boost::filesystem::create_directories(subdir);
}
Eigen::MatrixXd nodeMat = geom->getNodePnts();
writeBodyDataVP(subdir,nodeMat);
writeWakeDataVP(subdir,nodeMat);
writeBuffWake2Data(subdir,nodeMat);
writeSpanwiseData(subdir);
writeParticleData(subdir);
writeFilamentData(subdir);
if (params->volMeshFlag && cells.size() > 0) //
{
writeVolMeshData(subdir, pts, cells);
}
if (params->surfStreamFlag)
{
writeBodyStreamlines(subdir);
}
}
void cpCaseVP::writeBodyDataVP(boost::filesystem::path path,const Eigen::MatrixXd &nodeMat){
std::vector<cellDataArray> data;
cellDataArray mu("Doublet Strengths"),sigma("Source Strengths"),pot("Velocity Potential"),V("Velocity"),Cp("Cp"),bN("bezNormals");
Eigen::MatrixXi con(bPanels->size(),3);
mu.data.resize(bPanels->size(),1);
sigma.data.resize(bPanels->size(),1);
pot.data.resize(bPanels->size(),1);
V.data.resize(bPanels->size(),3);
Cp.data.resize(bPanels->size(),1);
bN.data.resize(bPanels->size(),3);
for (int i=0; i<bPanels->size(); i++)
{
mu.data(i,0) = (*bPanels)[i]->getMu();
sigma.data(i,0) = (*bPanels)[i]->getSigma();
pot.data(i,0) = (*bPanels)[i]->getPotential();
V.data.row(i) = (*bPanels)[i]->getGlobalV();
Cp.data(i,0) = (*bPanels)[i]->getCp();
con.row(i) = (*bPanels)[i]->getVerts();
bN.data.row(i) = (*bPanels)[i]->getBezNormal();
}
data.push_back(mu);
data.push_back(sigma);
data.push_back(pot);
data.push_back(V);
data.push_back(Cp);
data.push_back(bN);
piece body;
body.pnts = nodeMat;
body.connectivity = con;
body.cellData = data;
std::string fname = path.string()+"/surfaceData-"+std::to_string(timeStep)+".vtu";
VTUfile bodyFile(fname,body);
}
void cpCaseVP::writeWakeDataVP(boost::filesystem::path path, const Eigen::MatrixXd &nodeMat){
std::vector<cellDataArray> data;
cellDataArray mu("Doublet Strengths"),pot("Velocity Potential");
Eigen::MatrixXi con(wPanels->size(),(*wPanels)[0]->getVerts().size()); // Assumes wake won't mix tris and quads
mu.data.resize(wPanels->size(),1);
pot.data.resize(wPanels->size(),1);
for (int i=0; i<wPanels->size(); i++)
{
mu.data(i,0) = (*wPanels)[i]->getMu();
pot.data(i,0) = (*wPanels)[i]->getPotential();
con.row(i) = (*wPanels)[i]->getVerts();
}
data.push_back(mu);
data.push_back(pot);
piece wake;
wake.pnts = nodeMat;
wake.connectivity = con;
wake.cellData = data;
std::string fname = path.string()+"/wakeData-"+std::to_string(timeStep)+".vtu";
VTUfile wakeFile(fname,wake);
}
void cpCaseVP::writeBuffWake2Data(boost::filesystem::path path, const Eigen::MatrixXd &nodeMat){
std::vector<cellDataArray> data;
cellDataArray mu("Doublet Strengths"),pot("Velocity Potential");
Eigen::MatrixXi con;
con.resize(w2panels->size(),4);
mu.data.resize(w2panels->size(),1);
pot.data.resize(w2panels->size(),1);
for (int i=0; i<w2panels->size(); i++)
{
mu.data(i,0) = (*w2panels)[i]->getMu();
pot.data(i,0) = (*w2panels)[i]->getPotential();
con.row(i) = (*w2panels)[i]->getVerts();
}
data.push_back(mu);
data.push_back(pot);
piece wake;
wake.pnts = nodeMat;
wake.connectivity = con;
wake.cellData = data;
std::string fname = path.string()+"/bufferWake2Data-"+std::to_string(timeStep)+".vtu";
VTUfile wakeFile(fname,wake);
}
void cpCaseVP::writeFilamentData(boost::filesystem::path path){
std::vector<cellDataArray> data;
cellDataArray mu("Gamma");
Eigen::MatrixXi con;
Eigen::MatrixXd nodeMat(2*filaments.size(),3);
for(int i=0; i<filaments.size(); i++){
nodeMat.row(2*i) = filaments[i]->getP1();
nodeMat.row(2*i+1) = filaments[i]->getP2();
}
con.resize(filaments.size(),2); // 2 pnts per filament
mu.data.resize(filaments.size(),1);
int nodeCounter = 0; // Used to make filament end points
for (int i=0; i<filaments.size(); i++)
{
mu.data(i,0) = filaments[i]->getStrength();
con(i,0) = nodeCounter;
con(i,1) = nodeCounter+1;
nodeCounter = nodeCounter+2;
}
data.push_back(mu);
piece fils;
fils.pnts = nodeMat;
fils.connectivity = con;
fils.cellData = data;
std::string fname = path.string()+"/filaments-"+std::to_string(timeStep)+".vtu";
VTUfile filFile(fname,fils);
}
void cpCaseVP::writeParticleData(boost::filesystem::path path){
Eigen::MatrixXd partMat(particles.size(),3);
for (int i=0; i<particles.size(); i++)
{
partMat.row(i) = particles[i]->pos;
}
std::vector<cellDataArray> data;
cellDataArray strength("Strength"), shedTime("Time Step Shed");
Eigen::MatrixXi con(particles.size(),1);
Eigen::MatrixXi shed(particles.size(),1);
strength.data.resize(particles.size(),3);
shedTime.data.resize(particles.size(),1);
for (int i=0; i<particles.size(); i++)
{
strength.data.row(i) = particles[i]->strength;
shedTime.data(i,0) = particles[i]->shedTime;
con(i) = i;
}
data.push_back(strength);
data.push_back(shedTime);
piece parts;
parts.pnts = partMat;
parts.connectivity = con;
parts.cellData = data;
std::string fname = path.string()+"/particleData-"+std::to_string(timeStep)+".vtu";
VTUfile partFile(fname,parts);
}
void cpCaseVP::readBodyKinFile(){
std::ifstream fid;
fid.open(params->bodyKinFileLoc);
bodyKin.resize(numSteps, 6); // U, V, W, p, q, r
for (int i=0; i<numSteps; i++)
{
for(int j=0; j < 6; j++)
{
fid >> bodyKin(i,j);
}
}
fid.close();
}
void cpCaseVP::populateVolMesh(){
// Clear Past Timestep Mesh
volMeshDat.velocity.clear();
volMeshDat.coef_press.clear();
for (int i=0; i<cells.size(); i++) {
Eigen::Vector3d velInCell = velocityInflFromEverything(volMeshDat.cellCenter[i]);
volMeshDat.velocity.push_back(velInCell);
}
for (int i=0; i<cells.size(); i++) {
// Incompressible Bernoulli equation
double Cp = pow( volMeshDat.velocity[i].norm() / Vmag , 2 );
volMeshDat.coef_press.push_back(Cp);
}
}
//void cpCaseVP::moveGeometry(){
// // Steps to continue development include (but are not limited to)
// // In the 'velocityInflFromEverything' functions, initialize with zero velocity instead of freestream
// // Uncomment all 'moveGeometry' calls
// // The 'collapseBufferWake' function must seed particle at the panel center instead of tracking it with freestream.
// // Instead of the 'bodyMovement' vector, you'll probably want to use Eigen::MatrixXd and just slice with .row
//
// std::vector<double> bodyMovement = { -99.619, 0, -8.7156, 0, 0, 0 };
//
// geom->moveGeom(bodyMovement);
//
// // Filaments aren't part of geom... but they should be.
// for (int i=0; i<filaments.size(); i++) {
// filaments[i]->moveFilament(bodyMovement, dt);
// }
//
//
//}
//void cpCase::particleStrengthUpdate(){
// // This function uses the combined vortex stretching and diffusion equation used by Wincklemans for a regularized vortex core with high algebraic smoothing. (he refers to it as the strength update equation.)
//
// std::vector<Eigen::Vector3d> stretchDiffVec; // Creating a vector of diffusion values because the strength change needs to be set after all particle influences have been calculated
// for(int i=0; i<particles.size(); i++)
// {
// Eigen::Vector3d dAlpha = Eigen::Vector3d::Zero();
//
// // FarField2 condition
// if((particles[i]->pos-Eigen::Vector3d::Zero()).norm() > 20*params->cref)
// {
// stretchDiffVec.push_back(dAlpha);
// }
// // FarField1 condition
// else if((particles[i]->pos-Eigen::Vector3d::Zero()).norm() > 12*params->cref)
// {
// // Stretching from particles
// for(int j=0; j<particles.size(); j++)
// {
// dAlpha += particles[i]->partStrengthUpdate(particles[j]);
// }
// stretchDiffVec.push_back(dAlpha);
// }
// // Treat normally
// else{
//
// // Stretching from particles
// for(int j=0; j<particles.size(); j++)
// {