forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helmholtz3d5.cxx
3073 lines (2851 loc) · 95.6 KB
/
Helmholtz3d5.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
// (NONLINEAR-)HELMHOLTZ SOLVER WITH HIGH ORDER 3D FINITE ELEMENTS
// ---------------------------------------------------------------
// This code implements the polynomial,
// node, high-order finite element,
// mesh, dynamic-vector,
// and sparse-matrix objects required
// to discretize the nonlinear
// Helmholtz equation in 3-D
// on an adaptive mesh of tetrahedra.
// Readers are advised to add an extra
// code line and apply the destructor
// to each dynamic object at the end
// of the block in which it is defined
// to release the valuable "heap" memory
// occupied by it for future use.
#include<stdio.h>
#include<math.h>
const int PrintMesh=0;
const int PrintSol=0;
const int Regular=0;
const int boundaryLevels=0;
const int nonlinearLevels=10;
const int adaptiveLevels=2;
const int refineLevels=0;
const int Dirichlet=1;
const double SlitWidth=.2;
const double SlitLength=.5;
const double HELM=-.01;
double HELMNonlin=-0.0;
int Newton = 1;
const int RowSumStablizer=0;
double thresholdAdaptive=-0.01;
int useILU=1;
const double thresholdILU=0.05;
const double thresholdCG=1.e-6;
double ratioMG=-.5;
const int AMG=0;
const int Circle=1;
const double thresholdMG=0.05;
const double thresholdMGAtilde=0.05;
const double thresholdMGpositive=0.05;
const double thresholdMGQ=0.0;
const double FactorFine=1.;
const int ThrowQ=0;
const int Smooth=0;
const int TruncateAtilde=0;
const int ThrowAtilde=0;
const int Nu1=2;
const int Nu2=2;
const int NuCoarse=1;
const int cycleIndex=1;
int indexK(int i, int j){
return i>=j ? i * (i + 1) / 2 + j : indexK(j,i);
}
int indexI(int k){
int i = 0;
for(; i * (i + 1) / 2 <= k; i++);
return i - 1;
}
int indexJ(int k){
return k - indexI(k) * (indexI(k) + 1) / 2;
}
int max(int a, int b){return a>b ? a : b;}
int min(int a, int b){return a<b ? a : b;}
double max(double a, double b){return a>b ? a : b;}
double min(double a, double b){return a<b ? a : b;}
double fabs(double d){return d > 0. ? d : -d;} // absolute value
int C(int a, int n){
return n ? a * C(a-1,n-1) : 1;
} // a!/(a-n)!
int factorial(int n){
return n ? n * factorial(n-1) : 1;
} // factorial
int power(int basis, unsigned exp){
return exp ? basis * power(basis,exp-1) : 1;
} // "basis" to the "exp"
template<class T, int N> class vector{
T component[N];
public:
vector(const T&);
vector(const T&a,const T&b){
component[0] = a; component[1] = b;
} // constructor for 2-d vectors
vector(const T&a,const T&b,const T&c){
component[0] = a; component[1] = b; component[2] = c;
} // constructor for 3-d vectors
vector(const vector&);
const vector& operator=(const vector&);
const vector& operator=(const T&);
~vector(){} // destructor
const T& operator[](int i) const{ return component[i]; } //read only ith component
T& operator()(int i){ return component[i];
} //read/write ith component
void set(int i,const T& a){ component[i] = a; } // change ith component
const vector& operator+=(const vector&);
const vector& operator-=(const vector&);
const vector& operator*=(const T&);
const vector& operator/=(const T&);
};
template<class T, int N>
vector<T,N>::vector(const T& a = 0){
for(int i = 0; i < N; i++)
component[i] = a;
} // constructor
template<class T, int N>
vector<T,N>::vector(const vector<T,N>& v){
for(int i = 0; i < N; i++)
component[i] = v.component[i];
} // copy constructor
template<class T, int N>
const vector<T,N>& vector<T,N>::operator=(const vector<T,N>& v){
if(this != &v)
for(int i = 0; i < N; i++)
component[i] = v.component[i];
return *this;
} // assignment operator
template<class T, int N>
const vector<T,N>& vector<T,N>::operator=(const T& a){
for(int i = 0; i < N; i++)
component[i] = a;
return *this;
} // assignment operator with a scalar argument
template<class T, int N>
const vector<T,N>& vector<T,N>::operator+=(const vector<T,N>&v){
for(int i = 0; i < N; i++)
component[i] += v[i];
return *this;
} // adding a vector to the current vector
template<class T, int N>
const vector<T,N>& vector<T,N>::operator-=(const vector<T,N>&v){
for(int i = 0; i < N; i++)
component[i] -= v[i];
return *this;
} // subtracting a vector from the current vector
template<class T, int N>
const vector<T,N>& vector<T,N>::operator*=(const T& a){
for(int i = 0; i < N; i++)
component[i] *= a;
return *this;
} // multiplying the current vector by a scalar
template<class T, int N>
const vector<T,N>& vector<T,N>::operator/=(const T& a){
for(int i = 0; i < N; i++)
component[i] /= a;
return *this;
} // multiplying the current vector by a scalar
template<class T, int N>
const vector<T,N> operator+(const vector<T,N>&u, const vector<T,N>&v){
return vector<T,N>(u) += v;
} // vector plus vector
template<class T, int N>
const vector<T,N> operator-(const vector<T,N>&u, const vector<T,N>&v){
return vector<T,N>(u) -= v;
} // vector minus vector
template<class T, int N>
const vector<T,N> operator*(const vector<T,N>&u, const T& a){
return vector<T,N>(u) *= a;
} // vector times scalar
template<class T, int N>
const vector<T,N> operator*(const T& a, const vector<T,N>&u){
return vector<T,N>(u) *= a;
} // 'T' times vector
template<class T, int N>
const vector<T,N> operator/(const vector<T,N>&u, const T& a){
return vector<T,N>(u) /= a;
} // vector times scalar
template<class T, int N>
const vector<T,N>& operator+(const vector<T,N>&u){
return u;
} // negative of a vector
template<class T, int N>
const vector<T,N> operator-(const vector<T,N>&u){
return vector<T,N>(u) *= -1;
} // negative of a vector
template<class T, int N>
const T operator*(const vector<T,N>&u, const vector<T,N>&v){
T sum = 0;
for(int i = 0; i < N; i++)
sum += u[i] * +v[i];
return sum;
} // vector times vector (inner product)
template<class T, int N>
const T squaredNorm(const vector<T,N>&u){
return u*u;
} // squared l2 norm
template<class T, int N>
const T l2norm(const vector<T,N>&u){
return sqrt(u*u);
} // l2 norm
template<class T, int N>
void print(const vector<T,N>&v){
printf("(");
for(int i = 0;i < N; i++){
printf("v[%d]=",i);
print(v[i]);
}
printf(")\n");
} // printing a vector
typedef vector<double,1> point1;
typedef vector<double,2> point;
typedef vector<double,3> point3;
template<class T, int N, int M> class matrix : public vector<vector<T,N>,M>{
public:
matrix(){}
matrix(const vector<vector<T,N>,M>&){}
matrix(double d,char*){
set(0,point1(d));
} // constructor
matrix(double d){
set(0,point(d,0.));
set(1,point(0.,d));
} // constructor
matrix(const vector<T,N>&u, const vector<T,N>&v){
set(0,u);
set(1,v);
} // constructor
matrix(const vector<T,N>&u, const vector<T,N>&v, const vector<T,N>&w){
set(0,u);
set(1,v);
set(2,w);
} // constructor
matrix(const vector<T,N>&u, const vector<T,N>&v, const vector<T,N>&w, const vector<T,N>&r, const vector<T,N>&s, const vector<T,N>&t){
set(0,u);
set(1,v);
set(2,w);
set(3,r);
set(4,s);
set(5,t);
} // constructor
~matrix(){} // destructor
const T& operator()(int i,int j) const{return (*this)[j][i];}//A(i,j) read only
vector<T,N>& operator()(int i){
return vector<vector<T,N>,M>::operator()(i);
}
T& operator()(int i,int j,char*){
return (*this)(j)(i);
}//A(i,j) read/write
const matrix& operator+=(const matrix&);
const matrix& operator-=(const matrix&);
const matrix& operator*=(const T&);
const matrix& operator/=(const T&);
};
typedef matrix<double,1,1> matrix1;
typedef matrix<double,2,2> matrix2;
typedef matrix<double,3,3> matrix3;
typedef matrix<double,6,6> matrix6;
double det(const matrix2&A){
return A(0,0)*A(1,1) - A(0,1)*A(1,0);
} // determinant of matrix
double det(const matrix3&A){
return A(0,0) * (A(1,1)*A(2,2)-A(1,2)*A(2,1))
- A(0,1) * (A(1,0)*A(2,2)-A(1,2)*A(2,0))
+ A(0,2) * (A(1,0)*A(2,1)-A(1,1)*A(2,0));
} // determinant of matrix3
const matrix1 inverse(const matrix1&A){
point1 column(1./A(0,0));
return matrix1(column);
} // inverse of matrix1
template<class T, int N, int M>
const matrix<T,N,M>& matrix<T,N,M>::operator/=(const T&a){
for(int i=0; i<M; i++)
set(i,(*this)[i] / a);
return *this;
} // division by scalar
template<class T, int N, int M>
const matrix<T,N,M> operator*(const T&a,const matrix<T,N,M>&m){
return matrix<T,N,M>(m) *= a;
} // scalar times matrix
template<class T, int N, int M>
const matrix<T,N,M> operator*(const matrix<T,N,M>&m, const T&a){
return matrix<T,N,M>(m) *= a;
} // matrix times scalar
template<class T, int N, int M>
const matrix<T,N,M> operator/(const matrix<T,N,M>&m, const T&a){
return matrix<T,N,M>(m) /= a;
} // matrix divided by scalar
const matrix2 inverse(const matrix2&A){
point column0(A(1,1),-A(1,0));
point column1(-A(0,1),A(0,0));
return matrix2(column0,column1)/det(A);
} // inverse of matrix
const matrix3 inverse(const matrix3&A){
point3 column0(A(1,1)*A(2,2)-A(1,2)*A(2,1),-(A(1,0)*A(2,2)-A(1,2)*A(2,0)),A(1,0)*A(2,1)-A(1,1)*A(2,0));
point3 column1(-(A(0,1)*A(2,2)-A(0,2)*A(2,1)),A(0,0)*A(2,2)-A(0,2)*A(2,0),-(A(0,0)*A(2,1)-A(0,1)*A(2,0)));
point3 column2(A(0,1)*A(1,2)-A(0,2)*A(1,1),-(A(0,0)*A(1,2)-A(0,2)*A(1,0)),A(0,0)*A(1,1)-A(0,1)*A(1,0));
return matrix3(column0,column1,column2)/det(A);
} // inverse of matrix3
template<class T, int N>
T det(const matrix<T,N,N>&A){
if(N==1)
return A(0,0);
T detA = 0.;
int sign = 1;
for(int j=0; j<N; j++){
detA += sign * A(0,j) * det(minor(0,j,A));
sign *= -1;
}
return detA;
} // determinant of a square matrix
double fabs(const matrix2&A){
return sqrt(fabs(det(A)));
} // upper left element
template<class T, int N>
const matrix<T,N-1,N-1> minor(int k, int l, const matrix<T,N,N>&m){
matrix<T,N-1,N-1> minorkl;
int ii=-1;
for(int i=0; i<N; i++)
if(i!=k){
ii++;
int jj=-1;
for(int j=0; j<N; j++)
if(j!=l)minorkl(ii,++jj,"pur") = m(i,j);
}
return minorkl;
} // (k,l)th minor of m
template<class T, int N, int M>
const matrix<T,N,M>& matrix<T,N,M>::operator+=(const matrix<T,N,M>&m){
vector<vector<T,N>,M>::operator+=(m);
return *this;
} // adding a matrix
template<class T, int N, int M>
const matrix<T,N,M>& matrix<T,N,M>::operator-=(const matrix<T,N,M>&m){
vector<vector<T,N>,M>::operator-=(m);
return *this;
} // subtracting a matrix
template<class T, int N, int M>
const matrix<T,N,M>& matrix<T,N,M>::operator*=(const T&a){
for(int i=0; i<M; i++)
set(i,(*this)[i] * a);
return *this;
} // multiplication by scalar
template<class T, int N, int M>
const matrix<T,N,M> operator+(const matrix<T,N,M>&m1, const matrix<T,N,M>&m2){
return matrix<T,N,M>(m1) += m2;
} // matrix plus matrix
template<class T, int N, int M>
const matrix<T,N,M> operator-(const matrix<T,N,M>&m1, const matrix<T,N,M>&m2){
return matrix<T,N,M>(m1) -= m2;
} // matrix minus matrix
template<class T, int N, int M> const vector<T,M>
operator*(const vector<T,N>&v,const matrix<T,N,M>&m){
vector<T,M> result;
for(int i=0; i<M; i++)
result.set(i, v * m[i]);
return result;
} // vector times matrix
template<class T, int N, int M> const vector<T,N>
operator*(const matrix<T,N,M>&m,const vector<T,M>&v){
vector<T,N> result;
for(int i=0; i<M; i++)
result += v[i] * m[i];
return result;
} // matrix times vector
template<class T, int N, int M, int K> const matrix<T,N,K>
operator*(const matrix<T,N,M>&m1,const matrix<T,M,K>&m2){
matrix<T,N,K> result;
for(int i=0; i<K; i++)
result.set(i,m1 * m2[i]);
return result;
} // matrix times matrix
template<class T, int N, int M, int K> const matrix<T,N,K>&
operator*=(matrix<T,N,M>&m1,const matrix<T,M,K>&m2){
return m1 = m1 * m2;
} // matrix times matrix
double real(const matrix2&A){
return A(0,0);
} // upper left element
const point3 operator&(const point3&u, const point3&v){
point3 i(1.,0.,0.);
point3 j(0.,1.,0.);
point3 k(0.,0.,1.);
return i * (u[1]*v[2]-u[2]*v[1])
- j * (u[0]*v[2]-u[2]*v[0])
+ k * (u[0]*v[1]-u[1]*v[0]);
} // vector product
template<class T, int N>
const matrix<T,N,N>
inverse(const matrix<T,N,N>&A){
matrix<T,N,N> Ainverse;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
Ainverse(i,j,"put") = power(-1,i+j) * det(minor(j,i,A));
return Ainverse/det(A);
} // inverse of matrix3
const point operator/(const point&v, const matrix2&m){
return inverse(m) * v;
} // vector divided by matrix2
const point3 operator/(const point3&v, const matrix3&m){
return inverse(m) * v;
} // vector divided by matrix2
const point& operator/=(point&v, const matrix2&m){
return v = v / m;
} // vector divided by matrix2
const point3& operator/=(point3&v, const matrix3&m){
return v = v / m;
} // vector divided by matrix2
const matrix2 operator/(const matrix2&m1, const matrix2&m2){
return inverse(m2) * m1;
} // matrix2 divided by matrix2
const matrix3 operator/(const matrix3&m1, const matrix3&m2){
return inverse(m2) * m1;
} // matrix3 divided by matrix3
const matrix2& operator/=(matrix2&m1, const matrix2&m2){
return m1 = m1 / m2;
} // matrix2 divided by matrix2
const matrix3& operator/=(matrix3&m1, const matrix3&m2){
return m1 = m1 / m2;
} // matrix3 divided by matrix3
template<class T, int N, int M>
const matrix<T,N,M> transpose(const matrix<T,M,N>&A){
matrix<T,N,M> At;
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
At(i,j,"put") = A(j,i);
return At;
} // transpose of a matrix
template<class T> class node{
T location;
int index;
int sharingCells;
public:
node(const T&loc=0., int ind=-1, int sharing=0):
location(loc),index(ind),sharingCells(sharing){} // constructor
node(const node&n):location(n.location),index(n.index),
sharingCells(n.sharingCells){} // copy constructor
const node& operator=(const node&);
~node(){} // destructor
const T& operator()() const{return location;} // read the location
int getIndex() const{return index;} // read index
void setIndex(int i){index=i;} // set index
int getSharingElements() const{return sharingCells;} // read it
void moreSharingElements(){sharingCells++;} // increase it
int lessSharingElements(){
return
sharingCells ?
!(--sharingCells)
:
1;
} // decrease it
int noSharingElement() const{return !sharingCells;}//dangling node
};
template<class T>
const node<T>& node<T>::operator=(const node<T>&n){
if(this != &n){
location = n.location;
index = n.index;
sharingCells = n.sharingCells;
}
return *this;
} // assignment operator
template<class T>
void print(const node<T>&n){
print(n());
printf("index=%d; %d sharing elements\n",
n.getIndex(),n.getSharingElements());
} // print a node
template<class T, int N> class cell{
node<T>* vertex[N];
int index[56];
public:
int readMeshIndex(int i) const{
return index[i];
}
int& meshIndex(int i){
return index[i];
}
cell(){
for(int i=0; i<N; i++)
vertex[i] = new node<T>(0.,-1,1);
for(int i=0; i<56; i++)
index[i] = -1;
} // default constructor
cell(node<T>&,node<T>&,node<T>&);
cell(node<T>&,node<T>&,node<T>&,node<T>&);
cell(cell<T,N>&);
const cell<T,N>& operator=(cell<T,N>&);
~cell();
node<T>& operator()(int i){return *(vertex[i]);}//read/write ith vertex
const node<T>& operator[](int i)const{return *(vertex[i]);}//read only
void resetIndices(){
for(int i=0; i<N; i++)
vertex[i]->setIndex(-1);
} // reset indices to -1
void indexing(int&count){
for(int i=0; i<N; i++)
if(vertex[i]->getIndex()<0)vertex[i]->setIndex(count++);
} // indexing the vertices
};
template<class T, int N>
cell<T,N>::cell(node<T>&a, node<T>&b, node<T>&c){
vertex[0] = a.noSharingElement() ? new node<T>(a) : &a;
vertex[1] = b.noSharingElement() ? new node<T>(b) : &b;
vertex[2] = c.noSharingElement() ? new node<T>(c) : &c;
for(int i=0; i<N; i++)
vertex[i]->moreSharingElements();
for(int i=0; i<56; i++)
index[i] = -1;
} // constructor
template<class T, int N>
cell<T,N>::cell(node<T>&a, node<T>&b, node<T>&c, node<T>&d){
vertex[0] = a.noSharingElement() ? new node<T>(a) : &a;
vertex[1] = b.noSharingElement() ? new node<T>(b) : &b;
vertex[2] = c.noSharingElement() ? new node<T>(c) : &c;
vertex[3] = d.noSharingElement() ? new node<T>(d) : &d;
for(int i=0; i<N; i++)
vertex[i]->moreSharingElements();
for(int i=0; i<56; i++)
index[i] = -1;
} // constructor
template<class T, int N>
cell<T,N>::cell(cell<T,N>&e){
for(int i=0; i<N; i++){
vertex[i] = e.vertex[i];
vertex[i]->moreSharingElements();
}
for(int i=0; i<56; i++)
index[i] = e.index[i];
} // copy constructor
template<class T, int N> const cell<T,N>&
cell<T,N>::operator=(cell<T,N>&e){
if(this != &e){
for(int i=0; i<N; i++)
if(vertex[i]->lessSharingElements())delete vertex[i];
for(int i=0; i<N; i++){
vertex[i] = e.vertex[i];
vertex[i]->moreSharingElements();
}
for(int i=0; i<56; i++)
index[i] = e.index[i];
}
return *this;
} // assignment operator
template<class T, int N>
cell<T,N>::~cell(){
for(int i=0; i<N; i++)
if(vertex[i]->lessSharingElements())delete vertex[i];
} // destructor
template<class T, int N>
int operator<(const node<T>&n, const cell<T,N>&e){
for(int i=0; i<N; i++)
if(&n == &(e[i]))return i+1;
return 0;
} // check whether a node n is in a cell e
template<class T, int N>
void print(const cell<T,N>&e){
for(int i=0; i<N; i++)
print(e[i]);
for(int i=0; i<56; i++)
print(e.readMeshIndex(i));
} // printing a cell
typedef cell<point,3> triangle;
typedef cell<point3,4> tetrahedron;
template<class T> class dynamicVector{
protected:
int dimension;
T* component;
public:
dynamicVector(int, const T&);
dynamicVector(const dynamicVector&);
const dynamicVector& operator=(const dynamicVector&);
const dynamicVector& operator=(const T&);
~dynamicVector(){delete [] component;} // destructor
int dim() const{ return dimension; } // return the dimension
T& operator()(int i){ return component[i]; } //read/write ith component
const T& operator[](int i) const{ return component[i]; } //read only
const dynamicVector& operator+=(const dynamicVector&);
const dynamicVector& operator-=(const dynamicVector&);
template<class S>
const dynamicVector& operator*=(const S&);
template<class S>
const dynamicVector& operator/=(const S&);
};
template<class T>
int nonZeros(const dynamicVector<T>&v){
int count = 0;
for(int i=0; i<v.dim(); i++)
if(v[i] != 0)
count++;
return count;
} // number of nonzero components
template<class T>
dynamicVector<T>::dynamicVector(int dim=0,const T& a=0) : dimension(dim),
component(dim ? new T[dim] : 0){
for(int i = 0; i < dim; i++)
component[i] = a;
} // constructor
template<class T>
dynamicVector<T>::dynamicVector(const dynamicVector<T>& v)
: dimension(v.dimension), component(v.dimension ? new T[v.dimension] : 0){
for(int i = 0; i < v.dimension; i++)
component[i] = v.component[i];
} // copy constructor
template<class T>
const dynamicVector<T>& dynamicVector<T>::operator=(const dynamicVector<T>& v){
if(this != &v){
if(dimension != v.dimension){
delete [] component;
component = new T[v.dimension];
}
for(int i = 0; i < v.dimension; i++)
component[i] = v.component[i];
dimension = v.dimension;
}
return *this;
} // assignment operator
template<class T>
const dynamicVector<T>& dynamicVector<T>::operator=(const T& a){
for(int i = 0; i < dimension; i++)
component[i] = a;
return *this;
} // assignment operator with a scalar argument
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator+=( const dynamicVector<T>&v){
for(int i = 0; i < dimension; i++)
component[i] += v[i];
return *this;
} // adding a dynamicVector to the current dynamicVector
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator-=( const dynamicVector<T>&v){
for(int i = 0; i < dimension; i++)
component[i] -= v[i];
return *this;
} // subtracting a dynamicVector from the current dynamicVector
template<class T>
template<class S>
const dynamicVector<T>& dynamicVector<T>::operator*=(const S& a){
for(int i = 0; i < dimension; i++)
component[i] *= a;
return *this;
} // multiplying the current dynamicVector by a scalar
template<class T>
template<class S>
const dynamicVector<T>& dynamicVector<T>::operator/=(const S& a){
for(int i = 0; i < dimension; i++)
component[i] /= a;
return *this;
} // dividing the current dynamicVector by a scalar
template<class T>
const dynamicVector<T>
operator+(const dynamicVector<T>&u, const dynamicVector<T>&v){
return dynamicVector<T>(u) += v;
} // dynamicVector plus dynamicVector
template<class T>
const dynamicVector<T>
operator-(const dynamicVector<T>&u, const dynamicVector<T>&v){
return dynamicVector<T>(u) -= v;
} // dynamicVector minus dynamicVector
template<class T, class S>
const dynamicVector<T> operator*(const dynamicVector<T>&u, const S& a){
return dynamicVector<T>(u) *= a;
} // dynamicVector times scalar
template<class T>
const dynamicVector<T> operator*(double a, const dynamicVector<T>&u){
return dynamicVector<T>(u) *= a;
} // scalar times dynamicVector
template<class T, class S>
const dynamicVector<T> operator/(const dynamicVector<T>&u, const S& a){
return dynamicVector<T>(u) /= a;
} // dynamicVector divided by scalar
template<class T>
const dynamicVector<T> operator-(const dynamicVector<T>&u){
return dynamicVector<T>(u) *= -1.;
} // negative of a dynamicVector
template<class T>
double operator*(const dynamicVector<T>&u, const dynamicVector<T>&v){
double sum = 0;
for(int i = 0; i < u.dim(); i++)
sum += u[i] * +v[i];
return sum;
} // inner product
template<class T>
void print(const dynamicVector<T>&v){
printf("(");
for(int i = 0;i < v.dim(); i++){
printf("v[%d]=",i);
print(v[i]);
}
printf(")\n");
} // printing a dynamicVector
template<class T> class linkedList{
protected:
T item;
linkedList* next;
public:
T& operator()(int i){
return (item.getColumn() == i) ? item :
next&&(item.getColumn() < i) ? (*next)(i) : *(new T(0.));
} // read/write the item at column i
const T& operator()() const{return item;} // read "item" field
const linkedList* readNext() const{return next;} // read "next" field
linkedList():next(0){} // default constructor
linkedList(T&t, linkedList* N=0)
:item(t),next(N){} // constructor
linkedList(const linkedList&l):item(l()),next(
l.next ? new linkedList(*l.next) : 0){} // copy constructor
~linkedList(){
delete next;
next = 0;
} // destructor
const linkedList& operator=(const linkedList&);
linkedList& last(){return next ? next->last() : *this;} // last item
int length() const{return next ? next->length() + 1 : 1;}//no. of items
void append(T&t){last().next = new linkedList(t);}//append an item
void insertNextItem(T&t){next = new linkedList(t,next);}//insert an item
void insertFirstItem(T&t){
next = new linkedList(item,next);
item = t;
} // insert an item at the beginning
void dropNextItem();
void dropFirstItem();
template<class S>
const S truncateItems(double, const S&);
template<class S>
const S dropPositiveItems(int, const S&, double);
template<class S>
const S maskItems(const dynamicVector<int>&, const S&);
template<class S>
const S maskItemsSum(const dynamicVector<int>&, const dynamicVector<S>&);
const linkedList& operator+=(linkedList&);
linkedList& order(int);
};
template<class T>
const linkedList<T>&linkedList<T>::operator=(const linkedList<T>&L){
if(this != &L){
item = L();
if(next){
if(L.next)
*next = *L.next;
else{
delete next;
next = 0;
}
}
else
if(L.next)next = new linkedList(*L.next);
}
return *this;
} // assignment operator
template<class T>
void linkedList<T>::dropNextItem(){
if(next){
if(next->next){
linkedList<T>* keep = next;
next = next->next;
keep->item.~T();
}
else{
delete next;
next = 0;
}
} // drop the second item from the linked list
else
printf("error: cannot drop next element\n");
}
template<class T>
void linkedList<T>::dropFirstItem(){
if(next){
item = next->item;
dropNextItem();
}
else
printf("error: cannot drop first element\n");
} // drop the first item in the linked list
template<class T>
template<class S>
const S linkedList<T>::truncateItems(double threshold, const S& compare){
S sum=0.;
if(next){
int dropped = 0;
for(linkedList<T>* iterator = this;
iterator->next;
iterator = dropped ? iterator : iterator->next){
dropped = 0;
if(fabs(iterator->next->item.getValue()) <= threshold * fabs(compare)){
sum += iterator->next->item.getValue();
iterator->dropNextItem();
dropped = 1;
}
}
}
if(next&&(fabs(item.getValue()) <= threshold * fabs(compare))){
sum += item.getValue();
dropFirstItem();
}
return sum;
} // truncate small items
template<class T>
template<class S>
const S linkedList<T>::dropPositiveItems(
int diag, const S¢er, double threshold){
S sum=0.;
if(next){
int dropped = 0;
for(linkedList<T>* iterator = this;
iterator->next;
iterator = dropped ? iterator : iterator->next){
dropped = 0;
if((real(iterator->next->item.getValue()) / real(center) >= -threshold)&&
(iterator->next->item.getColumn() != diag)){
sum += iterator->next->item.getValue();
iterator->dropNextItem();
dropped = 1;
}
}
}
if(next&&(real(item.getValue()) / real(center) >= -threshold)&&(item.getColumn() != diag)){
sum += item.getValue();
dropFirstItem();
}
return sum;
} // truncate positive off-diagonal items
template<class T>
template<class S>
const S linkedList<T>::maskItems(const dynamicVector<int>& mask, const S&){
S sum=0.;
if(next){
int dropped = 0;
for(linkedList<T>* iterator = this;
iterator->next;
iterator = dropped ? iterator : iterator->next){
dropped = 0;
if(!mask[iterator->next->item.getColumn()]){
sum += iterator->next->item.getValue();
iterator->dropNextItem();
dropped = 1;
}
}
}
if(next&&(!mask[item.getColumn()])){
sum += item.getValue();
dropFirstItem();
}
return sum;
} // truncate masked items
template<class T>
template<class S>
const S linkedList<T>::maskItemsSum(const dynamicVector<int>& mask, const dynamicVector<S>&f){
S sum=0.;
if(next){
int dropped = 0;
for(linkedList<T>* iterator = this;
iterator->next;
iterator = dropped ? iterator : iterator->next){
dropped = 0;
if(!mask[iterator->next->item.getColumn()]){
sum += iterator->next->item.getValue() * f[iterator->next->item.getColumn()];
iterator->dropNextItem();
dropped = 1;
}
}
}
if(next&&(!mask[item.getColumn()])){
sum += item.getValue() * f[item.getColumn()];
dropFirstItem();
}
return sum;
} // truncate masked items
template<class T> const linkedList<T>&
linkedList<T>::operator+=(linkedList<T>&L){
linkedList<T>* runner = this;
linkedList<T>* Lrunner = &L;
if(L.item < item){
insertFirstItem(L.item);
Lrunner = L.next;
}
for(; runner->next; runner=runner->next){
if(Lrunner&&(Lrunner->item == runner->item)){
runner->item += Lrunner->item;
Lrunner = Lrunner->next;
}
for(; Lrunner&&(Lrunner->item < runner->next->item);
Lrunner = Lrunner->next){
runner->insertNextItem(Lrunner->item);
runner = runner->next;
}
}
if(Lrunner&&(Lrunner->item == runner->item)){
runner->item += Lrunner->item;
Lrunner = Lrunner->next;
}
if(Lrunner)runner->next = new linkedList<T>(*Lrunner);
return *this;
} // merge two linked lists
template<class T>