-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathStokes.cxx
2032 lines (1891 loc) · 64.5 KB
/
Stokes.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
// THE STOKES SOLVER (CHAPTER 21)
// ------------------------------
// This program solves the Stokes equations
// in the circle (Chapter 21). GMRES(20,10) is used,
// with the preconditioner obtained from perturbing
// the original system by adding the diagonal submatrix
// lambda I at the upper-left block. This preconditioner
// is inverted in the block-LU form, where the
// Schur-complement submatrix is inverted approximately
// by modified multigrid.
#include<stdio.h>
#include<math.h>
const int PrintMesh=0;
const int PrintX=0;
const int Regular=0;
const int boundaryLevels=1;
const int adaptiveLevels=6;
const int Dirichlet=1;
const double GammaD=.5;
const double Diag=0.0;
const double MU=1.;
double LAMBDA = 20.;
double NU=LAMBDA/(2*MU+LAMBDA);
//double NU=.9;
const double CoefDelta=1.;
const int RowSumStablizer=0;
const double Stablizer=0.0;
const double DiffX=1.;
const double DiffY=1.;
const double thresholdAdaptive=0.01;
const double thresholdILU=0.05;
const int useILU=0;
const double thresholdPCG=1.e-2;
const double thresholdCGS=1.e-6;
const double ratioMG=.95;
const int SmoothP=0;
const int AMG=0;
const int Circle=1;
const double thresholdMG=0.05;
const int Nu1=1;
const int Nu2=1;
const int NuCoarse=1;
const int cycleIndex=1;
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 abs(double d){return d > 0. ? d : -d;} // absolute value
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(){delete [] component;} // destructor
~vector(){} // destructor
const T& operator[](int i) const{ return component[i]; } //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>
T squaredNorm(const vector<T,N>&u){
return u*u;
} // squared 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
template<class T, int N, int M> class matrix : public vector<vector<T,N>,M>{
public:
matrix(){}
matrix(const vector<T,N>&u, const vector<T,N>&v){
set(0,u);
set(1,v);
} // constructor
const T& operator()(int i,int j) const{return (*this)[j][i];}//A(i,j)
const matrix& operator*=(const T&);
const matrix& operator/=(const T&);
};
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>& 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
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
typedef vector<double,2> point;
typedef vector<double,3> point3d;
typedef matrix<double,2,2> matrix2;
typedef matrix<double,3,3> matrix3;
double det(const matrix2&A){
return A(0,0)*A(1,1) - A(0,1)*A(1,0);
} // determinant of matrix
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 matrix2 transpose(const matrix2&A){
return matrix2(point(A(0,0),A(0,1)),point(A(1,0),A(1,1)));
} // transpose of a matrix
template<class T> class node{
T location;
int index;
int sharingElements;
public:
node(const T&loc=0., int ind=-1, int sharing=0):
location(loc),index(ind),sharingElements(sharing){} // constructor
node(const node&n):location(n.location),index(n.index),
sharingElements(n.sharingElements){} // 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 sharingElements;} // read it
void moreSharingElements(){sharingElements++;} // increase it
int lessSharingElements(){return !(--sharingElements);} // decrease it
int noSharingElement() const{return !sharingElements;}//dangling node
};
template<class T>
const node<T>& node<T>::operator=(const node<T>&n){
if(this != &n){
location = n.location;
index = n.index;
sharingElements = n.sharingElements;
}
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 finiteElement{
node<T>* vertex[N];
int index;
public:
finiteElement():index(-1){
for(int i=0; i<N; i++)
vertex[i] = new node<T>;
} // default constructor
finiteElement(node<T>&,node<T>&,node<T>&);
finiteElement(finiteElement<T,N>&);
const finiteElement<T,N>& operator=(finiteElement<T,N>&);
~finiteElement();
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
double p() const{
for(int i=0; i<N; i++)
if(((*vertex[i])()[0]>0.)||((*vertex[i])()[1]>0.)) return 1.;
return DiffX;
} // coefficient of x-derivative
double q() const{
for(int i=0; i<N; i++)
if(((*vertex[i])()[0]>0.)||((*vertex[i])()[1]>0.)) return 1.;
return DiffY;
} // coefficient of y-derivative
void setIndex(int i){index = i;}
int getIndex() const{return index;}
};
template<class T, int N>
finiteElement<T,N>::finiteElement(node<T>&a, node<T>&b, node<T>&c):index(-1){
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();
} // constructor
template<class T, int N>
finiteElement<T,N>::finiteElement(finiteElement<T,N>&e):index(e.index){
for(int i=0; i<N; i++){
vertex[i] = e.vertex[i];
vertex[i]->moreSharingElements();
}
} // copy constructor
template<class T, int N> const finiteElement<T,N>&
finiteElement<T,N>::operator=(finiteElement<T,N>&e){
index = e.index;
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();
}
}
return *this;
} // assignment operator
template<class T, int N>
finiteElement<T,N>::~finiteElement(){
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 finiteElement<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 finite element e
template<class T, int N>
void print(const finiteElement<T,N>&e){
for(int i=0; i<N; i++)
print(e[i]);
} // printing a finiteElement
typedef finiteElement<point,3> triangle;
typedef finiteElement<point3d,4> tetrahedron;
template<class T> class connectedList{
protected:
T item;
connectedList* next;
public:
const T& operator()() const{return item;} // read "item" field
const connectedList* readNext() const{return next;} // read "next" field
connectedList():next(0){} // default constructor
connectedList(T&t, connectedList* N=0)
:item(t),next(N){} // constructor
connectedList(const connectedList&l):item(l()),next(
l.next ? new connectedList(*l.next) : 0){} // copy constructor
~connectedList(){delete next; next = 0;} // destructor
const connectedList& operator=(const connectedList&);
connectedList& 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 connectedList(t);}//append an item
void insertNextItem(T&t){next = new connectedList(t,next);}//insert an item
void insertFirstItem(T&t){
next = new connectedList(item,next);
item = t;
} // insert an item at the beginning
void dropNextItem();
void dropFirstItem();
void truncateItems(double);
const connectedList& operator+=(connectedList&);
connectedList& order(int);
};
template<class T>
const connectedList<T>&connectedList<T>::operator=(const connectedList<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 connectedList(*L.next);
}
return *this;
} // assignment operator
template<class T>
void connectedList<T>::dropNextItem(){
if(next){
if(next->next){
connectedList<T>* keep = next;
next = next->next;
keep->item.~T();
}
else{
delete next;
//next->item.~T();
next = 0;
}
} // drop the second item from the connected list
else
printf("error: cannot drop next element\n");
}
template<class T>
void connectedList<T>::dropFirstItem(){
if(next){
item = next->item;
dropNextItem();
}
else
printf("error: cannot drop first element\n");
} // drop the first item in the connected list
template<class T>
void connectedList<T>::truncateItems(double threshold){
if(next){
if(abs(next->item.getValue()) <= threshold){
dropNextItem();
truncateItems(threshold);
}
else
next->truncateItems(threshold);
}
if(next&&(abs(item.getValue()) <= threshold))
dropFirstItem();
} // truncate certain items
template<class T> const connectedList<T>&
connectedList<T>::operator+=(connectedList<T>&L){
connectedList<T>* runner = this;
connectedList<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 connectedList<T>(*Lrunner);
return *this;
} // merge two connected lists
template<class T>
connectedList<T>& connectedList<T>::order(int length){
if(length>1){
connectedList<T>* runner = this;
for(int i=0; i<length/2-1; i++)
runner = runner->next;
connectedList<T>* second = runner->next;
runner->next = 0;
order(length/2);
*this += second->order(length-length/2);
}
return *this;
} // order a disordered connected list
template<class T>
void print(const connectedList<T>&l){
printf("item:\n");
print(l());
if(l.readNext())print(*l.readNext());
} // print a connected list
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&);
const dynamicVector& operator*=(const T&);
const dynamicVector& operator/=(const T&);
};
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 + v.dimension);
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>
const dynamicVector<T>& dynamicVector<T>::operator*=(const T& a){
for(int i = 0; i < dimension; i++)
component[i] *= a;
return *this;
} // multiplying the current dynamicVector by a scalar
template<class T>
const dynamicVector<T>& dynamicVector<T>::operator/=(const T& 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>
const dynamicVector<T> operator*(const dynamicVector<T>&u, const T& a){
return dynamicVector<T>(u) *= a;
} // dynamicVector times scalar
template<class T>
const dynamicVector<T> operator*(const T& a, const dynamicVector<T>&u){
return dynamicVector<T>(u) *= a;
} // T times dynamicVector
template<class T>
const dynamicVector<T> operator/(const dynamicVector<T>&u, const T& 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>
T operator*(const dynamicVector<T>&u, const dynamicVector<T>&v){
T 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 mesh:public connectedList<T>{
public:
mesh(){} // default constructor
mesh(T&e){item = e;} // constructor
int indexing();
void refineNeighbor(node<point>&, node<point>&, node<point>&);
void refine(const dynamicVector<double>&, double);
void refineBoundary(int);
double maxNorm(const dynamicVector<double>&);
};
template<class T>
int mesh<T>::indexing(){
for(mesh<T>* runner = this; runner; runner=(mesh<T>*)runner->next)
runner->item.resetIndices();
int count=0;
int countElements=0;
for(mesh<T>* runner = this; runner; runner=(mesh<T>*)runner->next){
runner->item.indexing(count);
runner->item.setIndex(countElements++);
}
return count;
} // indexing finite-element mesh
void mesh<triangle>::refineNeighbor(node<point>&nI,
node<point>&nJ, node<point>&nIJ){
int ni = nI < item;
int nj = nJ < item;
if(ni&&nj){
ni--;
nj--;
int nk = 0;
while((nk==ni)||(nk==nj))
nk++;
triangle t1(nI,nIJ,item(nk));
triangle t2(nJ,nIJ,item(nk));
insertNextItem(t2);
insertNextItem(t1);
dropFirstItem();
}
else{
if(next)
((mesh<triangle>*)next)->refineNeighbor(nI,nJ,nIJ);
else{
node<point> newNode((1./sqrt(squaredNorm(nIJ()))) * nIJ());
triangle t1(nI,nIJ,newNode);
triangle t2(nJ,nIJ,t1(2));
insertNextItem(t2);
insertNextItem(t1);
}
}
} // refine also the neighbor of a refined triangle
void mesh<triangle>::refine(const dynamicVector<double>&v,
double threshold){
//vector<int,4> jump(0);
for(int i=0; i<3; i++)
for(int j=2; j>i; j--)
if((item[i].getIndex() >= 0)&&(item[j].getIndex() >= 0)&&
((abs(v[item[i].getIndex()] - v[item[j].getIndex()])>threshold)||
(abs(v[item[i].getIndex()+v.dim()/2] -
v[item[j].getIndex()+v.dim()/2])>threshold))){
/*
jump.set(i+j,1);
}
if(Regular&&(jump[1]||jump[3])) jump.set(2,1);
for(int i=0; i<3; i++)
//for(int j=2; j>i; j--)
for(int j=i+1; j<3; j++)
if(jump[i+j]){
*/
int k=0;
node<point> itemij = (item[i]()+item[j]())/2.;
while((k==i)||(k==j))
k++;
triangle t1(item(i),itemij,item(k));
triangle t2(item(j),t1(1),item(k));
if(next)
((mesh<triangle>*)next)->
refineNeighbor(item(i),item(j),t1(1));
insertNextItem(t2);
insertNextItem(t1);
dropFirstItem();
refine(v, threshold);
return;
}
if(next)((mesh<triangle>*)next)->refine(v, threshold);
} // adaptive refinement
void mesh<triangle>::refineBoundary(int levels){
mesh<triangle>* runner = this;
for(int i=1; i<levels; i++)
for(int j=0; j<power(2,i); j++){
point vertex0 = (*runner)()[0]();
point vertex1 = (*runner)()[1]();
point midpoint = (vertex0 + vertex1)/2.;
double angle1 = acos(vertex1[0]);
if(j >= power(2,i-1))angle1 = -angle1;
double angleIncrement = acos(sqrt(squaredNorm(midpoint)));
double angleMidpoint = angle1 - angleIncrement;
point newPoint(cos(angleMidpoint),sin(angleMidpoint));
node<point> newVertex(newPoint);
triangle t1(runner->item(0),newVertex,runner->item(1));
append(t1);
angleMidpoint = angle1 + angleIncrement;
newVertex=node<point>(point(cos(angleMidpoint),sin(angleMidpoint)));
triangle t2(runner->item(1),newVertex,runner->item(2));
append(t2);
runner = (mesh<triangle>*)runner->next;
}
} // refine at the boundary of a circular domain
double mesh<triangle>::maxNorm(const dynamicVector<double>&v){
double result=0.;
for(int i=0; i<3; i++)
if(squaredNorm(item[i]()-point(Circle,0))>0.01){
result=max(result,abs(v[item[i].getIndex()]));
result=max(result,abs(v[item[i].getIndex()+v.dim()/2]));
}
if(next)result=max(result,((mesh<triangle>*)next)->maxNorm(v));
return result;
} // mzximum of values at nodes away from the singularity at (1,0)
template<class T> class rowElement{
T value;
int column;
public:
rowElement(const T& val=0, int col=-1):value(val),column(col){}//constructor
rowElement(const rowElement&e):value(e.value),column(e.column){}//copy con.
const rowElement& operator=(const rowElement&e){
if(this != &e){
value = e.value;
column = e.column;
}
return *this;
} // assignment operator
~rowElement(){} // destructor
const T& getValue() const{return value;} // read the value
int getColumn() const{return column;} // return the column
const rowElement& operator+=(const T&t){
value += t;
return *this;
} // adding a T
const rowElement& operator+=(const rowElement<T>&e){
value += e.value;
return *this;
} // adding a rowElement
const rowElement& operator-=(const T&t){
value -= t;
return *this;
} // subtracting a T
const rowElement& operator-=(const rowElement<T>&e){
value -= e.value;
return *this;
} // subtracting a rowElement
const rowElement& operator*=(const T&t){
value *= t;
return *this;
} // multiplying by a T
const rowElement& operator/=(const T&t){
value /= t;
return *this;
} // dividing by a T
};
template<class T>
int operator<(const rowElement<T>&e, const rowElement<T>&f){
return e.getColumn() < f.getColumn();
} // smaller column index
template<class T>
int operator>(const rowElement<T>&e, const rowElement<T>&f){
return e.getColumn() > f.getColumn();
} // greater column index
template<class T>
int operator==(const rowElement<T>&e, const rowElement<T>&f){
return e.getColumn() == f.getColumn();
} // same column
template<class T>
const rowElement<T> operator+(const rowElement<T>&e, const T&t){
return rowElement<T>(e) += t;
} // rowElement plus a T
template<class T>
const rowElement<T> operator+(const T&t, const rowElement<T>&e){
return rowElement<T>(e) += t;
} // T plus rowElement
template<class T>
const rowElement<T> operator-(const rowElement<T>&e, const T&t){
return rowElement<T>(e) -= t;
} // rowElement minusa T
template<class T>
const rowElement<T> operator*(const rowElement<T>&e, const T&t){
return rowElement<T>(e) *= t;
} // rowElement times a T
template<class T>
const rowElement<T> operator*(const T&t, const rowElement<T>&e){
return rowElement<T>(e) *= t;
} // T times rowElement
template<class T>
const rowElement<T> operator/(const rowElement<T>&e, const T&t){
return rowElement<T>(e) /= e;
} // rowElement divided by a T
template<class T>
void print(const rowElement<T>&e){
print(e.getValue());
printf("column=%d\n",e.getColumn());
} // print a rowElement object
void print(int i){
printf("%d; ",i);
} // print an integer variable
void print(double d){
printf("%f; ",d);
} // print a double variable
template<class T> class row : public connectedList<rowElement<T> >{
public:
row(const T&val=0,int col=-1){item=rowElement<T>(val,col);}//constructor
void append(const T&val, int col){
rowElement<T> e(val,col);
connectedList<rowElement<T> >::append(e);
} // append a rowElement at the end of row
const rowElement<T>& operator()() const{return item;}//read first item
const T& getValue() const{ return item.getValue(); }//first-item value
int getColumn() const{ return item.getColumn(); }//first-item column
const T rowSum() const{
return next ? getValue() + (*(row<T>*)next).rowSum() : getValue();
} // row sum
const T operator[](int i) const{
return (getColumn() == i) ? getValue() :
next&&(getColumn() < i) ? (*(row*)next)[i] : 0.;
} // read the rowElement at column i
const T rowSumCoarse(const row<T>&r,const dynamicVector<int>&coarse)const{
T contribution = coarse[getColumn()] ? r[getColumn()] : 0.;
return next ? contribution +
((row<T>*)next)->rowSumCoarse(r,coarse) : contribution;
} // row sum at coarse points
void addCoarse(const row<T>&r,const dynamicVector<int>&coarse){
if(coarse[getColumn()])
item += r[getColumn()];
if(next) ((row<T>*)next)->addCoarse(r,coarse);
} // add values at coarse points
void insertNextItem(const T&val, int col){
rowElement<T> e(val,col);
connectedList<rowElement<T> >::insertNextItem(e);
} // insert a rowElement
void insertFirstItem(const T&val, int col){
rowElement<T> e(val,col);
connectedList<rowElement<T> >::insertFirstItem(e);
} // insert a rowElement at the beginning
const row& operator*=(const T&t){
item *= t;
if(next) *(row*)next *= t;
return *this;
} // multiply by a T
const row& operator/=(const T&t){
item /= t;
if(next) *(row*)next /= t;
return *this;
} // multiply by a T
const T operator*(const dynamicVector<T>&v) const{
return next ? getValue() * v[getColumn()] + *(row*)next * v
: getValue() * v[getColumn()];
} // row times vector (inner product)
void shiftColumns(int n){
item = rowElement<T>(getValue(),getColumn()+n);
if(next)(*(row<T>*)next).shiftColumns(n);
} // shift columns
void renumberColumns(const dynamicVector<int>&renumber){
item = rowElement<T>(getValue(),renumber[getColumn()]-1);
if(next)(*(row<T>*)next).renumberColumns(renumber);
} // renumber columns
void dropItems(const dynamicVector<int>&);
void dropPositiveItems(int, const T&, double);
};
template<class T>
void row<T>::dropItems(const dynamicVector<int>&mask){
if(next){
if(!mask[(*next)().getColumn()]){
dropNextItem();
dropItems(mask);
}
else
(*(row<T>*)next).dropItems(mask);
if(!mask[getColumn()])dropFirstItem();
}
} // drop certain items
template<class T>
void row<T>::dropPositiveItems(int i, const T¢er, double threshold){
if(next){
if(((*next)().getColumn() != i)&&
((*next)().getValue()/center >= -threshold)){
dropNextItem();
dropPositiveItems(i, center, threshold);
}
else
(*(row<T>*)next).dropPositiveItems(i, center, threshold);
if((getColumn() != i)&&(getValue()/center >= -threshold))
dropFirstItem();
}
} // drop positive off-diagonal elements
template<class T>
const row<T> operator*(const row<T>&r, const T&t){
return row<T>(r) *= t;
} // row times T
template<class T>