-
Notifications
You must be signed in to change notification settings - Fork 23
/
post_solver.cxx
3474 lines (2954 loc) · 118 KB
/
post_solver.cxx
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
/*=========================================================================
*
* Copyright (c) 2014-2015 The Regents of the University of California.
* All Rights Reserved.
*
* Portions of the code Copyright (c) 2009-2013 Open Source Medical
* Software Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*=========================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <memory.h>
#include <float.h>
#include <iostream>
#include <string>
#include <vector>
#ifndef WIN32
#include <unistd.h>
#include <strings.h>
#endif
#ifdef WIN32
#include <string.h>
#endif
#include "cvFlowsolverOptions.h"
#include "SimVascular.h"
#include "cvSolverIO.h"
#include "vtkPoints.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkIdList.h"
#include "vtkCellType.h"
#include "vtkXMLUnstructuredGridReader.h"
#include "vtkXMLUnstructuredGridWriter.h"
#include "vtkUnstructuredGridWriter.h"
#include "vtkPointData.h"
#include "vtkGeometryFilter.h"
#include "vtkCleanPolyData.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkPolyDataWriter.h"
#include "vtkCellData.h"
#include "vtkXMLPolyDataReader.h"
#include "vtkCellArray.h"
#ifdef SV_USE_ZLIB
#include "simvascular_zlib.h"
#else
#include <stdlib.h>
#define gzopen fopen
#define gzprintf fprintf
#define gzFile FILE*
#define gzclose fclose
#endif
#ifdef WIN32
void bzero(void* ptr, size_t sz) {
int i;
char *cptr;
cptr = (char*) ptr;
for (i=0; i < sz; i++) {
cptr[i]=0;
}
return;
}
#endif
using namespace std;
/*
nv is the number of degrees of freedom per node
numel is the total number of elements
ien_ is the TRUE global connectivity
(index local element node number and element
number to TRUE global node number)
numnp is the total number of nodes
x is the coordinates (3*numnp)
q is the solution (nv*numnp)
nen is the number of nodes in an element
*/
class PostSolver {
public:
PostSolver();
~PostSolver();
int SetInputDirectory(const char* indir);
int QueryNumberOfSolutionVariables(int stepNumber);
int QueryNumberOfProcessors();
int QueryFluidProperties();
int QueryWallProperties();
int ReadConnectivity();
int GetNumberOfNodalCoordinates() {return nshgtot_;};
int GetNumberOfElements() {return neltot_;};
int GetNumberOfSolutionVariables() {return numvar_;};
int GetNumberOfWallProperties() {return numwallprop_;};
int GetNumberOfProcessorsUsed() {return numprocs_;};
double* GetCoordinatePtr() {return xglobal_;};
int* GetConnectivityPtr() {return ien_;};
double* GetFluidProperties() {return matprops_;};
double* GetWallProperties() {return wpglobal_;};
int ExportFlowSolverFileFormat(int lstep, int newstepnumber, bool RequestedNewSn,
bool RequestedSolution, bool RequestedTimeDeriv, bool RequestedDisplacements, bool RequestedYbar, bool YbarArrayExists, bool RequestedYerror,
double* qglobal,double* aglobal,double* dglobal, double *ybarglobal, double* y1global, double* y2global, double* ye1global, double* ye2global,
const char* outdir);
int ExportYBar(int stepnumber, int numy, double* yglobal, const char* outdir);
int ParseRestartFile( int stepNumber, const char* field , int *numval, double **myglobal);
int ExistRestartFile (int stepNumber);
int SetInputFileType(const char* iotype);
// CTEST: Compare Residual Files
int CompareResidualFiles(const char* newResFile,const char* refResFile, double threshold);
// CTEST: Compare VTU Files
int CompareVTUFiles(const char* newResFile,const char* refResFile, double threshold);
private:
int ione_,itwo_,ithree_,iseven_;
double *xlocal_;
double *xglobal_;
int *ien_;
int **ncorpd2d_;
int **ecorpd2d_;
double *wplocal_;
double *wpglobal_;
double *matprops_;
char iotype_[80];
char indir_[255];
int numvar_;
int numwallprop_;
int numprocs_;
int nshgtot_;
int neltot_;
int maxnshg_;
int nsd_;
};
PostSolver::PostSolver() {
ione_ = 1;
itwo_ = 2;
ithree_ = 3;
iseven_ = 7;
xlocal_ = NULL;
xglobal_ = NULL;
ien_ = NULL;
wplocal_ = NULL;
wpglobal_= NULL;
matprops_=NULL;
iotype_[0]='\0';
sprintf(iotype_,"%s","binary");
// default directory is current directory
indir_[0]='.';
indir_[1]='/';
indir_[2]='\0';
numvar_ = 0;
numwallprop_ = 0;
numprocs_ = 0;
nshgtot_ = 0;
neltot_ = 0;
maxnshg_ = 0;
}
PostSolver::~PostSolver() {
if (xlocal_ != NULL) {
delete [] xlocal_;
}
if (ien_ != NULL) {
delete [] ien_;
}
if (ncorpd2d_ != NULL) {
for(int i=0; i< numprocs_; i++) {
delete [] ncorpd2d_[i];
}
delete [] ncorpd2d_;
}
if (ecorpd2d_ != NULL) {
for(int i=0; i< numprocs_; i++) {
delete [] ecorpd2d_[i];
}
delete [] ecorpd2d_;
}
if (wplocal_ != NULL) {
delete [] wplocal_;
}
}
int PostSolver::SetInputFileType(const char* iotype) {
iotype_[0]='\0';
sprintf(iotype_,"%s",iotype);
return CV_OK;
}
int PostSolver::SetInputDirectory(const char* indir) {
indir_[0]='\0';
sprintf(indir_,"%s",indir);
return CV_OK;
}
int PostSolver::QueryNumberOfSolutionVariables(int stepNumber) {
char filename[255];
filename[0]='\0';
int irstin;
int intfromfile[50];
/* scanning restart.<stepnum>.1 for numvar_ */
sprintf(filename,"%srestart.%d.1",indir_,stepNumber);
cout << "Opening " << filename << " to scan for number of variables..."<<endl;
if (openfilewithspaces_(filename, "read", &irstin ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
readheader_(&irstin,"solution",(void*)intfromfile,&ithree_,"double",iotype_);
numvar_=intfromfile[1]; /* pushing the second int into numvar_ */
closefile_( &irstin, "read" );
return CV_OK;
}
int PostSolver::QueryNumberOfProcessors() {
int iarray[10];
char filename[255];
filename[0]='\0';
int igeom = 0;
sprintf(filename,"%sgeombc.dat.%d",indir_,1); /* geometry and bc database */
cout << "Opening " << filename << " to scan for number of processors and global modes..."<<endl;
if (openfilewithspaces_(filename, "read", &igeom ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
readheader_(&igeom,"number of processors",(void*)iarray,&ione_,"integer",iotype_);
numprocs_ = iarray[0];
readheader_(&igeom,"number of global modes",(void*)iarray,&ione_,"integer",iotype_);
nshgtot_ = iarray[0];
closefile_(&igeom, "read");
return CV_OK;
}
int PostSolver::QueryFluidProperties(){
int iarray[10];
char filename[255];
filename[0]='\0';
int igeom = 0;
sprintf(filename,"%sgeombc.dat.%d",indir_,1); /* geometry and bc database */
cout << "Opening " << filename << " to scan for material properties..."<<endl;
if (openfilewithspaces_(filename, "read", &igeom ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
iarray[0]=0;
readheader_(&igeom,"material properties",(void*)iarray,&ione_,"double",iotype_);
int numprops= iarray[0];
if(numprops<=0){
cout << "Warning: No material properties found in geombc"<<endl;
return CV_ERROR;
}else if(numprops==1){
cout << "Warning: No enough material properties found in geombc"<<endl;
return CV_ERROR;
}else{
matprops_=new double[numprops]();
readdatablock_(&igeom,"material properties",(void*)matprops_,&numprops,"double", iotype_);
}
return CV_OK;
}
int PostSolver::ReadConnectivity() {
int i,j,k,l;
int igeom = 0;
int numel, nen, nshgl;
int np;
int nshlmin,nelsofar,nelblk,nentp,nenl,nshl;
int ixsiz, nsd, itmpentsiz, iqsiz;
int gnum, opnum, nendx, junique;
int neltp, ipordl;
int *tmpent = NULL;
int iarray[10];
int intfromfile[50];
char filename[255];
filename[0]='\0';
neltot_ = 0;
maxnshg_ = 0;
/* scanning geom.dat.<procnum> to add up neltot_ and to determine maxnshg_ */
for(i=0; i< numprocs_; i++){
filename[0]='\0';
sprintf(filename,"%sgeombc.dat.%d",indir_,i+1); /* geometry and bc database */
if (openfilewithspaces_(filename, "read", &igeom ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
readheader_(&igeom,"number of interior elements",(void*)iarray,&ione_,"integer",iotype_);
numel=iarray[0];
readheader_(&igeom,"maximum number of element nodes",(void*)iarray,&ione_,"integer",iotype_);
nen=iarray[0];
readheader_(&igeom,"number of modes",(void*)iarray,&ione_,"integer",iotype_);
nshgl=iarray[0];
if(nshgl>maxnshg_) maxnshg_=nshgl;
closefile_(&igeom, "read" );
neltot_+=numel;
}
// now that we have our size of array information we can allocate
// our local and global geometry arrays (here local means on a
// given processor and global means the total array assembled
// across all processors)
xglobal_ = new double [ 3*nshgtot_ ];
xlocal_ = new double [ 3*maxnshg_ ];
ncorpd2d_ = new int* [numprocs_];
ecorpd2d_ = new int* [numprocs_];
bool realGlobalElementID=true;
nendx = nen;
if(nen>4) nendx=8; /* DX thinks of things as ALL tets or hexes ONLY */
ien_ = new int [ nendx*neltot_ ];
nshlmin=nendx;
nelsofar=0;
// Next we loop over the processors and read each processors
// geometry database. Using the ncorpd2d_ array, we reconstruct the
// global geometry structures (coordinates and connectivity)
for(i=0; i< numprocs_; i++){
/* open geom file and read header*/
sprintf(filename,"%sgeombc.dat.%d",indir_,i+1);
cout << "Reducing : " << filename << endl;
if (openfilewithspaces_( filename, "read", &igeom ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
readheader_(&igeom,"number of nodes",(void*)iarray,&ione_,"integer", iotype_);
np=iarray[0];
readheader_(&igeom,"number of modes",(void*)iarray,&ione_,"integer", iotype_);
nshgl=iarray[0];
readheader_(&igeom,"number of interior tpblocks",(void*)iarray,&ione_,"integer",iotype_);
nelblk=iarray[0];
readheader_(&igeom,"number of interior elements",(void*)iarray,&ione_,"integer",iotype_);
numel=iarray[0];
/* read coordinates and fill into global array */
readheader_(&igeom,"co-ordinates",(void*)intfromfile,&itwo_,"double",iotype_);
np=intfromfile[0];
nsd=intfromfile[1];
// Read the number of dimensions
nsd_=nsd;
ixsiz=np*nsd;
readdatablock_(&igeom,"co-ordinates",(void*)xlocal_,&ixsiz, "double",iotype_);
/* get the map from partion numbering to global numbering */
if ( numprocs_ > 1 ) {
readheader_(&igeom,"mode number map from partition to global",(void*)intfromfile,&ione_,"integer",iotype_);
np=intfromfile[0];
ncorpd2d_[i] = new int[np];
readdatablock_(&igeom,"mode number map from partition to global",(void*)ncorpd2d_[i],&np,"integer",iotype_);
intfromfile[0]=0;
readheader_(&igeom,"element number map from partition to global",(void*)intfromfile,&ione_,"integer",iotype_);
numel=intfromfile[0];
if(numel>0){
ecorpd2d_[i] = new int[numel];
readdatablock_(&igeom,"element number map from partition to global",(void*)ecorpd2d_[i],&numel,"integer",iotype_);
}else{
realGlobalElementID=false;
}
} else {
ncorpd2d_[i] = new int [nshgl];
for(j=0; j< nshgl ; j++)
ncorpd2d_[i][j]=j+1;
ecorpd2d_[i] = new int [numel];
for(j=0; j< numel ; j++)
ecorpd2d_[i][j]=j+1;
}
/* map it to global numbering */
for(k=0; k< 3; k++)
for(j=0; j< nshgl ; j++)
xglobal_[k*nshgtot_+ncorpd2d_[i][j]-1] = xlocal_[k*nshgl+j];
/*read connectivity data */
for(k=0; k< nelblk; k++){//nelblk is always 1
/* keyphrase identifying interior connectivity element block */
readheader_(&igeom,"connectivity interior",(void*)intfromfile,&iseven_,"integer",iotype_);
neltp =intfromfile[0];
nenl =intfromfile[1];
ipordl =intfromfile[2];
nshl =intfromfile[3];
if(nshl < nshlmin) nshlmin=nshl;
/* allocate the array to the right size */
tmpent = new int [ nshl*neltp ];
itmpentsiz=neltp*nshl;
/* now read the array */
readdatablock_(&igeom,"connectivity interior",(void*)tmpent,&itmpentsiz,"integer", iotype_);
if(realGlobalElementID){
for(l=0; l< neltp; l++){
for(j=0; j<nshl ; j++){
gnum=j*neltot_+ecorpd2d_[i][l]-1;
opnum=tmpent[j*neltp+l];
ien_[gnum]=ncorpd2d_[i][opnum-1];
}
}
}else{
/* Now we need to bring ien_ to the global numbering */
for(l=0; l< neltp; l++){
for(j=0; j<nshl ; j++){
gnum=nelsofar+j*neltot_+l;
opnum=tmpent[j*neltp+l];
ien_[gnum]=ncorpd2d_[i][opnum-1];
}
/* pad to largest nendx since dx has to treat pyr and wedg as
degenerate hex */
opnum=ien_[gnum]; /* hijack opnum to keep last real node */
for(j=nshl; j<nendx ; j++){
gnum=nelsofar+j*neltot_+l;
ien_[gnum]=opnum;
}
}
nelsofar+=neltp;
}
delete [] tmpent;
}
closefile_( &igeom, "read" );
}
return CV_OK;
}
int PostSolver::QueryWallProperties(){
int i,j,k;
int iarray[10];
int igeom = 0;
char filename[255];
filename[0]='\0';
// ------------ Read the first proc to determine number of wall properties -------------- //
sprintf(filename,"%sgeombc.dat.%d",indir_,1);
if (openfilewithspaces_( filename, "read", &igeom ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
/* read variable wall properties */
iarray[0]=0;
readheader_(&igeom,"varwallprop",(void*)iarray,&itwo_,"double",iotype_);
if (iarray[0] > 0){
numwallprop_ = iarray[1];
}
// --------------------------------------------------------------------------------------- //
wpglobal_ = new double [ numwallprop_ * nshgtot_ ];
wplocal_ = new double [ numwallprop_ * maxnshg_ ];
for (i=0; i< numprocs_; i++){
sprintf(filename,"%sgeombc.dat.%d",indir_,i+1);
cout << "Reducing : wall properties from " << filename << endl;
if (openfilewithspaces_( filename, "read", &igeom ) != CVSOLVER_IO_OK) {
delete [] wpglobal_;
delete [] wplocal_;
wpglobal_ = NULL;
wplocal_ = NULL;
return CV_ERROR;
}
/* read variable wall properties */
iarray[0]=0;
readheader_(&igeom,"varwallprop",(void*)iarray,&itwo_,"double",iotype_);
if (iarray[0] > 0){
int numNode = iarray[0];
int isize = numwallprop_ * numNode;
readdatablock_(&igeom,"varwallprop",(void*)wplocal_,&isize, "double",iotype_);
for (k = 0; k < numwallprop_; k++){
for (j = 0; j < numNode; j++){
wpglobal_[k*nshgtot_+ncorpd2d_[i][j]-1] = wplocal_[k*numNode+j];
}
}
closefile_( &igeom, "read" );
} else{
cout << "No wall properties found from " << filename<<". Will search from restart files." << endl;
delete [] wpglobal_;
delete [] wplocal_;
wpglobal_ = NULL;
wplocal_ = NULL;
closefile_( &igeom, "read" );
break;
}
}
return CV_OK;
}
vtkUnstructuredGrid* createGrid(int nshgtot,double* xglobal,int neltot, int* ien){
int i;
vtkPoints* pts = NULL;
vtkUnstructuredGrid* grid = NULL;
grid = vtkUnstructuredGrid::New();
grid->Allocate(neltot,1000);
// nodes
cout << "Create vtkPoints with " << nshgtot << " nodes." << endl;
pts = vtkPoints::New();
pts->Allocate(nshgtot,1000);
pts->SetNumberOfPoints(nshgtot);
vtkIntArray* gid = vtkIntArray::New();
gid->SetNumberOfComponents(1);
gid->Allocate(nshgtot,1000);
gid->SetNumberOfTuples(nshgtot);
gid->SetName("GlobalNodeID");
for( i=0; i< nshgtot; i++ ) {
pts->SetPoint(i,xglobal[0*nshgtot+i],xglobal[1*nshgtot+i],xglobal[2*nshgtot+i]);
// global ids start at 1
gid->SetTuple1(i,i+1);
}
grid->SetPoints(pts);
grid->GetPointData()->AddArray(gid);
pts->Delete();
gid->Delete();
// elements
cout << "Create tets for " << neltot << " elements." << endl;
vtkIdList* ptids = vtkIdList::New();
ptids->Allocate(10,10);
ptids->Initialize();
ptids->SetNumberOfIds(4);
vtkIntArray* eid = vtkIntArray::New();
eid->SetNumberOfComponents(1);
eid->Allocate(neltot,1000);
eid->SetNumberOfTuples(neltot);
eid->SetName("GlobalElementID");
for(i=0; i< neltot; i++){
//for(j=0; j < nendx; j++) gzprintf(frest,"%d ",ien[j*neltot+i]);
ptids->SetId(0,ien[0*neltot+i]-1);
ptids->SetId(1,ien[1*neltot+i]-1);
ptids->SetId(2,ien[2*neltot+i]-1);
ptids->SetId(3,ien[3*neltot+i]-1);
grid->InsertNextCell(VTK_TETRA,ptids);
eid->SetTuple1(i,i+1);
}
ptids->Delete();
grid->GetCellData()->SetScalars(eid);
grid->GetCellData()->SetActiveScalars("GlobalElementID");
grid->GetCellData()->CopyAllOn();
eid->Delete();
return grid;
}
vtkDoubleArray* cloneVtkDoubleArray(int nshgtot, int numComponent, vtkDoubleArray* source){
vtkDoubleArray *target = vtkDoubleArray::New();
target->SetNumberOfComponents(numComponent);
target->Allocate(nshgtot,10000);
target->SetNumberOfTuples(nshgtot);
for (int i=0; i< nshgtot; i++) {
//target->SetTuple4(i,aglobal[0*nshgtot+i],aglobal[1*nshgtot+i],aglobal[2*nshgtot+i],aglobal[3*nshgtot+i]);
target->SetTuple(i,i,source);
}
return target;
}
int SetWallFlag(int nshgtot,const char* wallfn, int **wallflag){
// read file
vtkPolyData* pd = NULL;
vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New();
reader->SetFileName(wallfn);
reader->Update();
pd = reader->GetOutput();
if (pd == NULL) {
cout << "ERROR in reading wall file: "<<wallfn<<"!" << endl;
return CV_ERROR;
}
vtkIntArray* gids = NULL;
gids =static_cast<vtkIntArray*>(reader->GetOutput()->GetPointData()->GetArray("GlobalNodeID"));
if (gids == NULL) {
cout << "ERROR: problem finding GlobalNodeID in wall file: "<<wallfn<<"!" << endl;
return CV_ERROR;
}
int* tempwallflag=new int[nshgtot]();
for (int i = 0; i < gids->GetNumberOfTuples(); i++) {
int nodeId = gids->GetTuple1(i);
tempwallflag[nodeId - 1] = 1;
}
// cleanup
reader->Delete();
*wallflag = tempwallflag;
return CV_OK;
}
int WallFiltering(double* vglobal, int nshgtot, int numComp,int* wallflag){
for(int k=0; k< numComp; k++){
for(int j=0; j< nshgtot ; j++){
if(wallflag[j]==0){
vglobal[k*nshgtot+j] = 0.0;
}
}
}
return CV_OK;
}
int GetWallElements(int neltot,int* ien, const char* wallfn, int* wallElementNum, int** wallElements){
vtkPolyData* pd = NULL;
vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New();
reader->SetFileName(wallfn);
reader->Update();
pd = reader->GetOutput();
pd->GetPointData()->SetActiveScalars("GlobalNodeID");
pd->GetCellData()->SetActiveScalars("GlobalElementID");
vtkIdList* ptids = vtkIdList::New();
ptids->Allocate(10, 10);
ptids->Initialize();
vtkCellArray *cells = pd->GetPolys();
cells->InitTraversal();
vtkIntArray* gids = NULL;
gids = static_cast<vtkIntArray*>(pd->GetPointData()->GetArray("GlobalNodeID"));
if (gids == NULL) {
cout << "ERROR: problem finding GlobalNodeID in " << wallfn << endl;
return CV_ERROR;
}
int n0, n1, n2, n3;
int elementId, cellNum;
cellNum=cells->GetNumberOfCells();
int* we=new int[cellNum*4]();
for (int icell = 0; icell < cellNum; icell++) {
ptids->Reset();
cells->GetCell(4 * icell, ptids);
if (ptids->GetNumberOfIds() != 3) {
cout << "ERROR: invalid number of ids in cell ("<<ptids->GetNumberOfIds()<<")!" << endl;
return CV_ERROR;
}
elementId = pd->GetCellData()->GetScalars()->GetTuple1(icell);
n0 = gids->GetTuple1(ptids->GetId(0));
n1 = gids->GetTuple1(ptids->GetId(1));
n2 = gids->GetTuple1(ptids->GetId(2));
n3 = -1;
for (int i = 0; i < 4; i++) {
if (ien[i * neltot + (elementId - 1)] != n0
&& ien[i * neltot + (elementId - 1)] != n1
&& ien[i * neltot + (elementId - 1)] != n2){
n3 = ien[i * neltot + (elementId - 1)];
break;
}
}
if (n3 < 0) {
cout << "ERROR: could not find nodes in element ("<<elementId<<" "<<n0<<" "<<n1<<" "<<n2<<") in "<< wallfn << endl;
return CV_ERROR;
}
we[0*cellNum+icell]=n0;
we[1*cellNum+icell]=n1;
we[2*cellNum+icell]=n2;
we[3*cellNum+icell]=n3;
}
*wallElementNum=cellNum;
*wallElements=we;
ptids->Delete();
reader->Delete();
return CV_OK;
}
int CalculateWallStress(int nshgtot, int wallElementNum, int* wallElements, bool applyWD, double* xglobal,double* dglobal,double* qglobal,double mu,
double** vtglobal, double** vwglobal){
int nsd=3;
double* sA=new double[nshgtot]();
double* sTF=new double[nshgtot*nsd]();
double* sSF=new double[nshgtot*nsd]();
double* wallTraction=new double[nshgtot*nsd]();
double* wallShearStress=new double[nshgtot*nsd]();
double beArea, eJac,normM, temp, coef;
double beNorm[3], eNaNbx[4][3];
double V[3][2], normV[3];
double SJ, Xa, Xb, Xc, Ya, Yb, Yc, Za, Zb, Zc;
double Tdn[3], ndTdn, taue[3];
int nid[4];
int n1,n2,i,j;
double* x=new double[nshgtot*nsd]();
if(dglobal==NULL || !applyWD){
for (int n1=0; n1< nshgtot; n1++) {
for(int i=0;i<3;i++){
x[i*nshgtot+n1]=xglobal[i*nshgtot+n1];
}
}
}else{
for (int n1=0; n1< nshgtot; n1++) {
for(int i=0;i<3;i++){
x[i*nshgtot+n1]=xglobal[i*nshgtot+n1]+dglobal[i*nshgtot+n1];
}
}
}
// double** x = new double*[3];
// x[0] = new double[nshg];
// x[1] = new double[nshg];
// x[2] = new double[nshg];
coef=24.0;
for(int n1=0;n1<wallElementNum;n1++){
nid[0]=wallElements[0*wallElementNum+n1]-1;//used as index in C array
nid[1]=wallElements[1*wallElementNum+n1]-1;
nid[2]=wallElements[2*wallElementNum+n1]-1;
nid[3]=wallElements[3*wallElementNum+n1]-1;
//for beNorm
for(j=0;j<nsd-1;j++){
for(i=0;i<nsd;i++){
V[i][j] = x[i*nshgtot+nid[j+1]]-x[i*nshgtot+nid[0]];
}
}
normV[0] = V[1][0]*V[2][1] - V[2][0]*V[1][1];
normV[1] = V[2][0]*V[0][1] - V[0][0]*V[2][1];
normV[2] = V[0][0]*V[1][1] - V[1][0]*V[0][1];
normM = 0.0;
for(i=0;i<nsd;i++){
normM = normM + normV[i]*normV[i];
}
normM = sqrt(normM);
beNorm[0] = normV[0]/normM;
beNorm[1] = normV[1]/normM;
beNorm[2] = normV[2]/normM;
temp = 0.0;
for(i=0;i<nsd;i++){
temp = temp + beNorm[i]*(x[i*nshgtot+nid[3]]-x[i*nshgtot+nid[0]]);
}
if(temp>0.0){
beNorm[0] = -beNorm[0];
beNorm[1] = -beNorm[1];
beNorm[2] = -beNorm[2];
}
//for beArea
Xa = x[0*nshgtot+nid[0]] - x[0*nshgtot+nid[2]];
Xb = x[0*nshgtot+nid[1]] - x[0*nshgtot+nid[2]];
Ya = x[1*nshgtot+nid[0]] - x[1*nshgtot+nid[2]];
Yb = x[1*nshgtot+nid[1]] - x[1*nshgtot+nid[2]];
Za = x[2*nshgtot+nid[0]] - x[2*nshgtot+nid[2]];
Zb = x[2*nshgtot+nid[1]] - x[2*nshgtot+nid[2]];
beArea = sqrt((Xa*Yb-Xb*Ya)*(Xa*Yb-Xb*Ya)+(Ya*Zb-Yb*Za)*(Ya*Zb-Yb*Za)+(Za*Xb-Zb*Xa)*(Za*Xb-Zb*Xa));
Xa = x[0*nshgtot+nid[0]] - x[0*nshgtot+nid[3]];
Xb = x[0*nshgtot+nid[1]] - x[0*nshgtot+nid[3]];
Xc = x[0*nshgtot+nid[2]] - x[0*nshgtot+nid[3]];
Ya = x[1*nshgtot+nid[0]] - x[1*nshgtot+nid[3]];
Yb = x[1*nshgtot+nid[1]] - x[1*nshgtot+nid[3]];
Yc = x[1*nshgtot+nid[2]] - x[1*nshgtot+nid[3]];
Za = x[2*nshgtot+nid[0]] - x[2*nshgtot+nid[3]];
Zb = x[2*nshgtot+nid[1]] - x[2*nshgtot+nid[3]];
Zc = x[2*nshgtot+nid[2]] - x[2*nshgtot+nid[3]];
eJac = Xa*Yb*Zc+Xb*Yc*Za+Xc*Ya*Zb-(Xc*Yb*Za + Xb*Ya*Zc + Xa*Yc*Zb);
SJ = fabs(eJac)/eJac/24.0;
eJac = fabs(eJac);
eNaNbx[0][0] = SJ*(Yb*Zc - Yc*Zb);
eNaNbx[1][0] = SJ*(Yc*Za - Ya*Zc);
eNaNbx[2][0] = SJ*(Ya*Zb - Yb*Za);
eNaNbx[0][1] = SJ*(Xc*Zb - Xb*Zc);
eNaNbx[1][1] = SJ*(Xa*Zc - Xc*Za);
eNaNbx[2][1] = SJ*(Xb*Za - Xa*Zb);
eNaNbx[0][2] = SJ*(Xb*Yc - Xc*Yb);
eNaNbx[1][2] = SJ*(Xc*Ya - Xa*Yc);
eNaNbx[2][2] = SJ*(Xa*Yb - Xb*Ya);
for(i=0;i<nsd;i++){
eNaNbx[3][i] = 0.0;
for(n2=0;n2<3;n2++){
eNaNbx[3][i] = eNaNbx[3][i] - eNaNbx[n2][i];
}
}
double ux[3][3] = {0.0};
for(n2=0;n2<4;n2++){
for(i=0;i<nsd;i++){
for(j=0;j<nsd;j++){
ux[i][j] = ux[i][j]+ coef*qglobal[(i+1)*nshgtot+nid[n2]]*eNaNbx[n2][j]/eJac;
}
}
}
for(i=0;i<nsd;i++){
Tdn[i] = 0.0;
for(j=0;j<nsd;j++){
Tdn[i] = Tdn[i] + mu*(ux[i][j] + ux[j][i])*beNorm[j];
}
}
ndTdn = 0.0;
for(i=0;i<nsd;i++){
ndTdn = ndTdn + Tdn[i]*beNorm[i];
}
for(i=0;i<nsd;i++){
taue[i] = Tdn[i] - ndTdn*beNorm[i];
}
for(n2=0;n2<3;n2++){
sA[nid[n2]] = sA[nid[n2]] + beArea;
for(i=0;i<nsd;i++){
sTF[i*nshgtot+nid[n2]] = sTF[i*nshgtot+nid[n2]] - beArea*Tdn[i];
sSF[i*nshgtot+nid[n2]] = sSF[i*nshgtot+nid[n2]] - beArea*taue[i];
}
}
}
for(int n=0;n<nshgtot;n++){
if (sA[n]>0.0){
for(i=0;i<nsd;i++){
wallTraction[i*nshgtot+n] = sTF[i*nshgtot+n]/sA[n];
wallShearStress[i*nshgtot+n] = sSF[i*nshgtot+n] /sA[n];
}
}
}
*vtglobal=wallTraction;
*vwglobal=wallShearStress;
delete [] sA;
delete [] sTF;
delete [] sSF;
delete [] x;
return CV_OK;
}
// =======================================
// Export using old flowsolver file format
// =======================================
int PostSolver::ExportFlowSolverFileFormat(int lstep, int newstepnumber, bool RequestedNewSn,
bool RequestedSolution, bool RequestedTimeDeriv, bool RequestedDisplacements, bool RequestedYbar, bool YbarArrayExists, bool RequestedYerror,
double* qglobal,double* aglobal,double* dglobal, double *ybarglobal,double* y1global, double* y2global, double* ye1global, double* ye2global, const char* outdir){
// Write the total restart into restart.lstep.0 NOTE 0 is not
// used in our partitioned system which goes from 1..nproc
int i=-1;
int irstin = 0;
int iarray[10];
int magic_number = 362436;
int* mptr = &magic_number;
char rfile[255];
// allow user to reset the step number
if (RequestedNewSn) {
lstep = newstepnumber;
}
sprintf(rfile,"%srestart.%d.%d",outdir,lstep,i+1);
if (openfilewithspaces_(rfile, "write" , &irstin ) != CVSOLVER_IO_OK) {
return CV_ERROR;
}
writestring_( &irstin,"# SimVascular Input File Version 2.0\n");
writestring_( &irstin, "# Byte Order Magic Number : 362436 \n");
bzero( (void*)rfile, 255 );
sprintf(rfile,"# Output generated by phPost version 2.25: \n");
writestring_( &irstin, rfile );
int one = 1;
int size = 1;
int nitems = 1;
iarray[ 0 ] = 1;
writeheader_( &irstin, "byteorder magic number ",(void*)iarray, &nitems, &size, "integer", iotype_ );