This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlgfem.cpp
6243 lines (5402 loc) · 203 KB
/
lgfem.cpp
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
/// \file
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : [email protected]
//
/*
This file is part of Freefem++
Freefem++ is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Freefem++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __MWERKS__
#pragma optimization_level 0
//#pragma inline_depth(0)
#endif
#include <cmath>
#include <iostream>
#include <cfloat>
using namespace std;
#include "error.hpp"
#include "AFunction.hpp"
#include "rgraph.hpp"
#include <cstdio>
#include "fem.hpp"
#include "Mesh3dn.hpp"
#include "MatriceCreuse_tpl.hpp"
#include "MeshPoint.hpp"
#include <complex>
#include "Operator.hpp"
#include <set>
#include <map>
#include <vector>
#include "lex.hpp"
#include "lgfem.hpp"
#include "lgmesh3.hpp"
#include "lgsolver.hpp"
#include "problem.hpp"
#include "CGNL.hpp"
#include "AddNewFE.h"
#include "array_resize.hpp"
#include "PlotStream.hpp"
// add for the gestion of the endianness of the file.
//PlotStream::fBytes PlotStream::zott; //0123;
//PlotStream::hBytes PlotStream::zottffss; //012345678;
// ---- FH
namespace bamg { class Triangles; }
namespace Fem2D { void DrawIsoT(const R2 Pt[3],const R ff[3],const RN_ & Viso);
extern GTypeOfFE<Mesh3> &P1bLagrange3d;
extern GTypeOfFE<Mesh3> &RT03d;
extern GTypeOfFE<Mesh3> &Edge03d;
void Expandsetoflab(Stack stack,const CDomainOfIntegration & di,set<int> & setoflab,bool &all);
}
#include "BamgFreeFem.hpp"
static bool TheWait=false;
bool NoWait=false;
extern bool NoGraphicWindow;
extern long verbosity;
extern FILE *ThePlotStream; // Add for new plot. FH oct 2008
void init_lgmesh() ;
namespace FreeFempp {
template<class R>
TypeVarForm<R> * TypeVarForm<R>::Global;
}
const int nTypeSolveMat=10;
int kTypeSolveMat;
TypeSolveMat *dTypeSolveMat[nTypeSolveMat];
AnyType Long2TypeSolveMat(Stack, const AnyType &ll) {
long l=GetAny<long>(ll);
ffassert( l>=0 && l <kTypeSolveMat);
return dTypeSolveMat[l];
}
AnyType TypeSolveMat2Long(Stack,const AnyType &tt ) {
const TypeSolveMat *t = GetAny<TypeSolveMat *>(tt);
for(long l=0; l <kTypeSolveMat; ++l)
if( t==dTypeSolveMat[l]) return l;
return long (kTypeSolveMat-1); // sparse solver case
}
basicAC_F0::name_and_type OpCall_FormBilinear_np::name_param[]= {
{ "bmat",&typeid(Matrice_Creuse<R>* )},
LIST_NAME_PARM_MAT
/*
{ "init", &typeid(bool)},
{ "solver", &typeid(TypeSolveMat*)},
{ "eps", &typeid(double) } ,
{ "precon",&typeid(Polymorphic*)},
{ "dimKrylov",&typeid(long)},
{ "bmat",&typeid(Matrice_Creuse<R>* )},
{ "tgv",&typeid(double )},
{ "factorize",&typeid(bool)},
{ "strategy",&typeid(long )},
{ "tolpivot", &typeid(double)},
{ "tolpivotsym", &typeid(double) },
{ "nbiter", &typeid(long)}, // 12
{ "paramint",&typeid(KN_<long>)}, // Add J. Morice 02/09
{ "paramdouble",&typeid(KN_<double>)},
{ "paramstring",&typeid(string*)},
{ "permrow",&typeid(KN_<long>)},
{ "permcol",&typeid(KN_<long>)},
{ "fileparamint",&typeid(string*)}, // Add J. Morice 02/09
{ "fileparamdouble",&typeid(string*)},
{ "fileparamstring",&typeid(string* )},
{ "filepermrow",&typeid(string*)},
{ "filepermcol",&typeid(string*)} //22
*/
};
basicAC_F0::name_and_type OpCall_FormLinear_np::name_param[]= {
"tgv",&typeid(double )
};
/*
template<class R,class TA0,class TA1,class TA2>
class E_F_F0F0F0 :public E_F0 { public:
template <class T> struct remove_reference {typedef T type;};
template <class T> struct remove_reference<T&> {typedef T type;};
typedef typename remove_reference<TA0>::type A0;
typedef typename remove_reference<TA1>::type A1;
typedef typename remove_reference<TA2>::type A2;
typedef R (*func)( A0 , A1, A2 ) ;
func f;
Expression a0,a1,a2;
E_F_F0F0F0(func ff,Expression aa0,Expression aa1,Expression aa2)
: f(ff),a0(aa0),a1(aa1),a2(aa2) {}
AnyType operator()(Stack s) const
{return SetAny<R>( f( GetAny<A0>((*a0)(s)) , GetAny<A1>((*a1)(s)), GetAny<A2>((*a2)(s)) ) );}
bool EvaluableWithOutStack() const
{return a0->EvaluableWithOutStack() && a1->EvaluableWithOutStack() && a2->EvaluableWithOutStack() ;} //
bool MeshIndependent() const
{return a0->MeshIndependent() && a1->MeshIndependent() && a2->MeshIndependent();} //
int compare (const E_F0 *t) const {
int rr;
// cout << "cmp " << typeid(*this).name() << " and " << typeid(t).name() << endl;
const E_F_F0F0F0* tt=dynamic_cast<const E_F_F0F0F0 *>(t);
if (tt && f == tt->f) rr= clexico(a0->compare(tt->a0),a1->compare(tt->a1),a2->compare(tt->a2));
else rr = E_F0::compare(t);
return rr;
} // to give a order in instuction
int Optimize(deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n) const
{
int rr = find(m);
if (rr) return rr;
return insert(new Opt(*this,a0->Optimize(l,m,n),a1->Optimize(l,m,n),a2->Optimize(l,m,n)),l,m,n);
}
// build optimisation
class Opt: public E_F_F0F0F0 { public :
size_t ia,ib,ic;
Opt(const E_F_F0F0F0 &t,size_t iaa,size_t ibb,size_t icc)
: E_F_F0F0F0(t) ,
ia(iaa),ib(ibb),ic(icc) {}
AnyType operator()(Stack s) const
{
//A0 aa =*static_cast<A0 *>(static_cast<void*>(static_cast<char *>(s)+ia));
//A1 bb=*static_cast<A1 *>(static_cast<void*>(static_cast<char *>(s)+ib)) ;
//cout << ia << " " << ib << "f( " << aa << "," << bb << " ) = "<< f(aa,bb) << endl;
return SetAny<R>( f( *static_cast<A0 *>(static_cast<void*>(static_cast<char *>(s)+ia)) ,
*static_cast<A1 *>(static_cast<void*>(static_cast<char *>(s)+ib)) ,
*static_cast<A2 *>(static_cast<void*>(static_cast<char *>(s)+ic))
) );}
};
};
template<class R,class A=R,class B=A,class C=A,class CODE=E_F_F0F0F0<R,A,B,C> >
class OneOperator3 : public OneOperator {
aType r; // return type
typedef typename CODE::func func;
//typedef R (*func)(A,B) ;
func f;
public:
E_F0 * code(const basicAC_F0 & args) const
{ return new CODE(f,t[0]->CastTo(args[0]),t[1]->CastTo(args[1]),t[2]->CastTo(args[3]));}
OneOperator3(func ff):
OneOperator(map_type[typeid(R).name()],map_type[typeid(A).name()],map_type[typeid(B).name()],map_type[typeid(C).name()]),
f(ff){}
};
*/
const E_Array * Array(const C_F0 & a) {
if (a.left() == atype<E_Array>() )
return dynamic_cast<const E_Array *>(a.LeftValue());
else
return 0;
}
bool Box2(const C_F0 & bb, Expression * box)
{
const E_Array * a= Array(bb);
if(a && a->size() == 2)
{
box[0] = to<double>((*a)[0]);
box[1] = to<double>((*a)[1]);
return true;
}
else
return false;
}
bool Box2x2(Expression bb, Expression * box)
{
const E_Array * a= dynamic_cast<const E_Array *>(bb);
if(a && a->size() == 2)
return Box2((*a)[0],box) && Box2((*a)[1],box+2) ;
else
return false;
}
void dump_table()
{
cout << " dump the table of the language " << endl;
cout << " ------------------------------ " <<endl <<endl;
map<const string,basicForEachType *>::const_iterator i; ;
for (i= map_type.begin();i !=map_type.end();i++)
{
cout << " type : " << i->first << endl;
if( i->second )
i->second->ShowTable(cout);
else cout << " Null \n";
cout << "\n\n";
}
for (i= map_type.begin();i !=map_type.end();i++)
{
cout << " type : " << i->first << endl;
if( i->second )
i->second->ShowTable(cout);
else cout << " Null \n";
cout << "\n\n";
}
cout << "--------------------- " << endl;
cout << *TheOperators << endl;
cout << "--------------------- " << endl;
}
/*
class LocalArrayVariable:public E_F0
{
size_t offset;
aType t; // type of the variable just for check
Expression n; // expression of the size
public:
AnyType operator()(Stack s) const {
SHOWVERB( cout << "\n\tget var " << offset << " " << t->name() << endl);
return PtrtoAny(static_cast<void *>(static_cast<char *>(s)+offset),t);}
LocalArrayVariable(size_t o,aType tt,Expression nn):offset(o),t(tt),n(nn) {ffassert(tt);
SHOWVERB(cout << "\n--------new var " << offset << " " << t->name() << endl);
}
};
*/
bool In(long *viso,int n,long v)
{
int i=0,j=n,k;
if (v <viso[0] || v >viso[j-1])
return false;
while (i<j-1)
if ( viso[k=(i+j)/2]> v) j=k;
else i=k;
return (viso[i]=v);
}
class LinkToInterpreter { public:
Type_Expr P,N,x,y,z,label,region,nu_triangle,nu_edge,lenEdge,hTriangle,area,inside,volume;
LinkToInterpreter() ;
};
LinkToInterpreter * l2interpreter;
using namespace Fem2D;
using namespace EF23;
/*
inline pmesh ReadMesh( string * const & s) {
Mesh * m=new Mesh(*s);
R2 Pn,Px;
m->BoundingBox(Pn,Px);
m->quadtree=new FQuadTree(m,Pn,Px,m->nv);
// delete s; modif FH 2006 (stack ptr)
return m;
}
inline pmesh3 ReadMesh3( string * const & s) {
Mesh3 * m=new Mesh3(s->c_str());
R3 Pn,Px;
// m->BoundingBox(Pn,Px);
m->gtree=new Mesh3::GTree(m->vertices,m->Pmin,m->Pmax,m->nv);
// delete s; modif FH 2006 (stack ptr)
return m;
}
*/
template<class Result,class A>
class E_F_A_Ptr_o_R :public E_F0 { public:
typedef Result A::* ptr;
Expression a0;
ptr p;
E_F_A_Ptr_o_R(Expression aa0,ptr pp)
: a0(aa0),p(pp) {}
AnyType operator()(Stack s) const {
return SetAny<Result*>(&(GetAny<A*>((*a0)(s))->*p));}
bool MeshIndependent() const {return a0->MeshIndependent();} //
};
// ----
// remarque pas de template, cela ne marche pas encore ......
class E_P_Stack_P :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R3*>(&MeshPointStack(s)->P);}
operator aType () const { return atype<R3*>();}
};
class E_P_Stack_Px :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->P.x);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_Py :public E_F0mps { public:
AnyType operator()(Stack s) const {throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->P.y);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_Pz :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->P.z);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_N :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R3*>(&MeshPointStack(s)->N);}
operator aType () const { return atype<R3*>();}
};
class E_P_Stack_Nx :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->N.x);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_Ny :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->N.y);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_Nz :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<R*>(&MeshPointStack(s)->N.z);}
operator aType () const { return atype<R*>();}
};
class E_P_Stack_Region :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long*>(&MeshPointStack(s)->region);}
operator aType () const { return atype<long*>();}
};
class E_P_Stack_Label :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long*>(&MeshPointStack(s)->label);}
operator aType () const { return atype<long *>();}
};
class E_P_Stack_Mesh :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<pmesh >(const_cast<pmesh>(MeshPointStack(s)->Th));}
operator aType () const { return atype<pmesh>();}
};
class E_P_Stack_Nu_Triangle :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long>(MeshPointStack(s)->t);}
operator aType () const { return atype<long>();}
};
class E_P_Stack_Nu_Vertex :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long>(MeshPointStack(s)->v);}
operator aType () const { return atype<long>();}
};
class E_P_Stack_Nu_Face :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long>(MeshPointStack(s)->f);}
operator aType () const { return atype<long>();}
};
class E_P_Stack_Nu_Edge :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<long>(MeshPointStack(s)->e);}
operator aType () const { return atype<long>();}
};
class E_P_Stack_inside :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
return SetAny<double>(MeshPointStack(s)->outside? 0.0 : 1.0 );}
operator aType () const { return atype<double>();}
};
class E_P_Stack_lenEdge :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
MeshPoint * mp=MeshPointStack(s);
ffassert(mp->T && mp ->e >=0 && mp->d==2);
double l= mp->T->lenEdge(mp->e);
return SetAny<double>(l);}
operator aType () const { return atype<double>();}
};
class E_P_Stack_hTriangle :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
MeshPoint * mp=MeshPointStack(s);
assert(mp->T) ;
double l= mp->T->h();
return SetAny<double>(l);}
operator aType () const { return atype<double>();}
};
class E_P_Stack_nTonEdge :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
MeshPoint * mp=MeshPointStack(s);
assert(mp->T && mp->e > -1 && mp->d==2 ) ;
long l=mp->Th->nTonEdge(mp->t,mp->e);
// cout << " nTonEdge " << l << endl;
return SetAny<long>( l) ;}
operator aType () const { return atype<long>();}
};
class E_P_Stack_areaTriangle :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
MeshPoint * mp=MeshPointStack(s);
assert(mp->T) ;
double l=-1; // unset ...
if(mp->d==2)
l= mp->T->area;
else if (mp->d==3 && mp->f >=0)
{
R3 NN = mp->T3->N(mp->f);
l= NN.norme()/2.;
}
else
{
cout << "erreur : E_P_Stack_areaTriangle" << mp->d << " " << mp->f << endl;
ffassert(0); // undef
}
return SetAny<double>(l);}
operator aType () const { return atype<double>();}
};
// add FH
class E_P_Stack_VolumeTet :public E_F0mps { public:
AnyType operator()(Stack s) const { throwassert(* (long *) s);
MeshPoint * mp=MeshPointStack(s);
assert(mp->T) ;
double l=-1; // unset ...
if (mp->d==3 && mp->T3 )
{
l= mp->T3->mesure();
}
else
{
cout << "erreur : E_P_Stack_VolumeTet" << mp->d << " " << mp->f << endl;
ffassert(0); // undef
}
return SetAny<double>(l);}
operator aType () const { return atype<double>();}
};
/*
class New_MeshPoint : public E_F0mps {
};*/
//inline pfes MakePtr(pmesh * const & a, TypeOfFE * const & tef){ return pfes(new pfes_tef(a,tef)) ;}
//inline pfes MakePtr(pmesh * const & a){ return pfes(new pfes_tef(a,&P1Lagrange)) ;}
//inline pfes MakePtr(pfes * const & a,long const & n){ return pfes(new pfes_fes(a,n)) ;}
/*
class E_pfes : public E_F0 {
const int N;
Expression *Th,*tef;
E_pfes(Expression *TTh,*ttef) : Th(TTh),tef(ttef),N(ttef?ttef->nbitem():0) {}
virtual AnyType operator()(Stack) const {
return AnyType<pfes*>
}
virtual size_t nbitem() const {return 1;}
};
OneOperator_pfes():OneOperator(atype<void>(),atype<E_Array>(),atype<E_Array>()) {}
E_F0 * code(const basicAC_F0 & args) const ;
};*/
template<class R>
class E_StopGC: public StopGC<R> {
public:
typedef KN<R> Kn;
typedef KN_<R> Kn_;
Stack s;
long n;
long iter;
Kn_ xx,gg;
C_F0 citer,cxx,cgg;
C_F0 stop;
E_StopGC(Stack ss,long nn,const Polymorphic * op): s(ss),n(nn),iter(-1),
xx(0,0),gg(0,0),
citer(CConstant<long*>(&iter)),
cxx(dCPValue(&xx)),
cgg(dCPValue(&gg)),
stop(op,"(",citer,cxx,cgg)
{
}
~E_StopGC()
{// a verifier ???? FH....
delete (E_F0 *) cxx; // ???
delete (E_F0 *) cgg; // ???
delete (E_F0 *) citer; // ???
delete (E_F0 *) stop; // ???
}
// template<class R> class StopGC { public: virtual bool Stop(int iter, R *, R * ){return false;} };
bool Stop(int iterr, R *x, R * g)
{
// cout << " Stop " << iterr << endl;
iter=iterr;
xx.set(x,n);
gg.set(g,n);
return GetAny<bool>(stop.eval(s));
}
};
template<class R>
class LinearCG : public OneOperator
{ public:
typedef KN<R> Kn;
typedef KN_<R> Kn_;
const int cas;
class MatF_O: VirtualMatrice<R> { public:
Stack stack;
mutable Kn x;
C_F0 c_x;
Expression mat1,mat;
typedef typename VirtualMatrice<R>::plusAx plusAx;
MatF_O(int n,Stack stk,const OneOperator * op)
: VirtualMatrice<R>(n),stack(stk),
x(n),c_x(CPValue(x)),
mat1(op->code(basicAC_F0_wa(c_x))),
mat( CastTo<Kn_>(C_F0(mat1,(aType)*op))) {
//ffassert(atype<Kn_ >() ==(aType) *op);
// WhereStackOfPtr2Free(stack)=new StackOfPtr2Free(stack);// FH mars 2005
}
~MatF_O() {
// cout << " del MatF_O mat " << endl;
if(mat1 != mat)
delete mat;
delete mat1;
// cout << " del MatF_Ocx ..." << endl;
Expression zzz = c_x;
// cout << " zzz "<< zzz << endl;
delete zzz;
// WhereStackOfPtr2Free(stack)->clean(); // FH mars 2005
}
void addMatMul(const Kn_ & xx, Kn_ & Ax) const {
ffassert(xx.N()==Ax.N());
x =xx;
Ax += GetAny<Kn_>((*mat)(stack));
WhereStackOfPtr2Free(stack)->clean();
}
plusAx operator*(const Kn & x) const {return plusAx(this,x);}
virtual bool ChecknbLine(int n) const { return true;}
virtual bool ChecknbColumn(int m) const { return true;}
};
class E_LCG: public E_F0mps { public:
const int cas;// <0 => Nolinear
static const int n_name_param=6;
static basicAC_F0::name_and_type name_param[] ;
Expression nargs[n_name_param];
const OneOperator *A, *C;
Expression X,B;
E_LCG(const basicAC_F0 & args,int cc) :cas(cc)
{
args.SetNameParam(n_name_param,name_param,nargs);
{ const Polymorphic * op= dynamic_cast<const Polymorphic *>(args[0].LeftValue());
ffassert(op);
A = op->Find("(",ArrayOfaType(atype<Kn* >(),false)); }
if (nargs[2])
{ const Polymorphic * op= dynamic_cast<const Polymorphic *>(nargs[2]);
ffassert(op);
C = op->Find("(",ArrayOfaType(atype<Kn* >(),false)); }
else C =0;
X = to<Kn*>(args[1]);
if (args.size()>2)
B = to<Kn*>(args[2]);
else
B=0;
}
virtual AnyType operator()(Stack stack) const {
int ret=-1;
E_StopGC<R> *stop=0;
// WhereStackOfPtr2Free(stack)=new StackOfPtr2Free(stack);// FH mars 2005
try {
Kn &x = *GetAny<Kn *>((*X)(stack));
int n=x.N();
MatF_O AA(n,stack,A);
double eps = 1.0e-6;
double *veps=0;
int nbitermax= 100;
long verb = verbosity;
if (nargs[0]) eps= GetAny<double>((*nargs[0])(stack));
if (nargs[1]) nbitermax = GetAny<long>((*nargs[1])(stack));
if (nargs[3]) veps=GetAny<double*>((*nargs[3])(stack));
if (nargs[4]) verb=Abs(GetAny<long>((*nargs[4])(stack)));
if (nargs[5]) stop= new E_StopGC<R>(stack,n,dynamic_cast<const Polymorphic *>(nargs[5]));
// cout << " E_LCG: Stop = " << stop << " " << verb << endl;
long gcverb=51L-Min(Abs(verb),50L);
if(verb==0) gcverb = 1000000000;// no print
if(veps) eps= *veps;
KN<R> bzero(B?1:n); // const array zero
bzero=R();
KN<R> *bb=&bzero;
if (B) {
Kn &b = *GetAny<Kn *>((*B)(stack));
R p = (b,b);
if (p== R())
{
// ExecError("Sorry LinearCG work only with nul right hand side, so put the right hand in the function");
}
bb = &b;
}
if (cas<0) {
if (C)
{ MatF_O CC(n,stack,C);
ret = NLCG(AA,CC,x,nbitermax,eps, gcverb,stop );}
else
ret = NLCG(AA,MatriceIdentite<R>(n),x,nbitermax,eps, gcverb,stop);
}
else
if (C)
{ MatF_O CC(n,stack,C);
ret = ConjuguedGradient2(AA,CC,x,*bb,nbitermax,eps, gcverb, stop );}
else
ret = ConjuguedGradient2(AA,MatriceIdentite<R>(n),x,*bb,nbitermax,eps, gcverb, stop );
if(veps) *veps = -(eps);
}
catch(...)
{
if( stop) delete stop;
// WhereStackOfPtr2Free(stack)->clean(); // FH mars 2005
throw;
}
// WhereStackOfPtr2Free(stack)->clean(); // FH mars 2005
if( stop) delete stop;
return SetAny<long>(ret);
}
operator aType () const { return atype<long>();}
};
E_F0 * code(const basicAC_F0 & args) const {
return new E_LCG(args,cas);}
LinearCG() : OneOperator(atype<long>(),
atype<Polymorphic*>(),
atype<KN<R> *>(),atype<KN<R> *>()),cas(2){}
LinearCG(int cc) : OneOperator(atype<long>(),
atype<Polymorphic*>(),
atype<KN<R> *>()),cas(cc){}
};
template<class R>
basicAC_F0::name_and_type LinearCG<R>::E_LCG::name_param[]= {
{ "eps", &typeid(double) },
{ "nbiter",&typeid(long) },
{ "precon",&typeid(Polymorphic*)},
{ "veps" , &typeid(double*) },
{ "verbosity" , &typeid(long)},
{ "stop" , &typeid(Polymorphic*)}
};
template<class R>
class LinearGMRES : public OneOperator
{ public:
typedef KN<R> Kn;
typedef KN_<R> Kn_;
const int cas;
class MatF_O: VirtualMatrice<R> { public:
Stack stack;
mutable Kn x;
C_F0 c_x;
Kn *b;
Expression mat1,mat;
typedef typename VirtualMatrice<R>::plusAx plusAx;
MatF_O(int n,Stack stk,const OneOperator * op,Kn *bb)
: VirtualMatrice<R>(n),
stack(stk),
x(n),c_x(CPValue(x)),b(bb),
mat1(op->code(basicAC_F0_wa(c_x))),
mat( CastTo<Kn_>(C_F0(mat1,(aType)*op)) /*op->code(basicAC_F0_wa(c_x))*/) {
// ffassert(atype<Kn_ >() ==(aType) *op);
}
~MatF_O() { if(mat1!=mat) delete mat; delete mat1; delete c_x.LeftValue();}
void addMatMul(const Kn_ & xx, Kn_ & Ax) const {
ffassert(xx.N()==Ax.N());
x =xx;
Ax += GetAny<Kn_>((*mat)(stack));
if(b && &Ax!=b) Ax += *b; // Ax -b => add b (not in cas of init. b c.a.d &Ax == b
WhereStackOfPtr2Free(stack)->clean(); // add dec 2008
}
plusAx operator*(const Kn & x) const {return plusAx(this,x);}
virtual bool ChecknbLine(int n) const { return true;}
virtual bool ChecknbColumn(int m) const { return true;}
};
class E_LGMRES: public E_F0mps { public:
const int cas;// <0 => Nolinear
static basicAC_F0::name_and_type name_param[] ;
static const int n_name_param =7;
Expression nargs[n_name_param];
const OneOperator *A, *C;
Expression X,B;
E_LGMRES(const basicAC_F0 & args,int cc) :cas(cc)
{
args.SetNameParam(n_name_param,name_param,nargs);
{ const Polymorphic * op= dynamic_cast<const Polymorphic *>(args[0].LeftValue());
ffassert(op);
A = op->Find("(",ArrayOfaType(atype<Kn* >(),false)); }
if (nargs[2])
{ const Polymorphic * op= dynamic_cast<const Polymorphic *>(nargs[2]);
ffassert(op);
C = op->Find("(",ArrayOfaType(atype<Kn* >(),false)); }
else C =0;
X = to<Kn*>(args[1]);
if (args.size()>2)
B = to<Kn*>(args[2]);
else
B=0;
}
virtual AnyType operator()(Stack stack) const {
Kn &x = *GetAny<Kn *>((*X)(stack));
Kn b(x.n);
E_StopGC<R> *stop=0;
if (B) b = *GetAny<Kn *>((*B)(stack));
else b= R();
int n=x.N();
int dKrylov=50;
double eps = 1.0e-6;
int nbitermax= 100;
long verb = verbosity;
if (nargs[0]) eps= GetAny<double>((*nargs[0])(stack));
if (nargs[1]) nbitermax = GetAny<long>((*nargs[1])(stack));
if (nargs[3]) eps= *GetAny<double*>((*nargs[3])(stack));
if (nargs[4]) dKrylov= GetAny<long>((*nargs[4])(stack));
if (nargs[5]) verb=Abs(GetAny<long>((*nargs[5])(stack)));
if (nargs[6]) stop= new E_StopGC<R>(stack,n,dynamic_cast<const Polymorphic *>(nargs[6]));
long gcverb=51L-Min(Abs(verb),50L);
int ret;
if(verbosity>4)
cout << " ..GMRES: eps= " << eps << " max iter " << nbitermax
<< " dim of Krylov space " << dKrylov << endl;
KNM<R> H(dKrylov+1,dKrylov+1);
int k=dKrylov;//,nn=n;
double epsr=eps;
KN<R> bzero(B?1:n); // const array zero
bzero=R();
KN<R> *bb=&bzero;
if (B) {
Kn &b = *GetAny<Kn *>((*B)(stack));
R p = (b,b);
if (p)
{
// ExecError("Sorry MPILinearCG work only with nul right hand side, so put the right hand in the function");
}
bb = &b;
}
KN<R> * bbgmres =0;
if ( !B ) bbgmres=bb; // none zero if gmres without B
MatF_O AA(n,stack,A,bbgmres);
if(bbgmres ){
*bbgmres= AA* *bbgmres; // Ok Ax == b -> not translation of b .
*bbgmres = - *bbgmres;
if(verbosity>1) cout << " ** GMRES set b = -A(0); : max=" << bbgmres->max() << " " << bbgmres->min()<<endl;
}
//cout << " ** GMRES bb max=" << bb->max() << " " << bb->min()<<endl;
if (cas<0) {
ErrorExec("NL GMRES: to do! sorry ",1);
/* if (C)
{ MatF_O CC(n,stack,C);
ret = NLGMRES(AA,CC,x,nbitermax,eps, 51L-Min(Abs(verbosity),50L) );}
else
ret = NLGMRES(AA,MatriceIdentite<R>(n),x,nbitermax,eps, 51L-Min(Abs(verbosity),50L));
ConjuguedGradient */
}
else
{
if (C)
{ MatF_O CC(n,stack,C,0);
ret=GMRES(AA,(KN<R> &)x, *bb,CC,H,k,nbitermax,epsr,verb,stop);}
else
ret=GMRES(AA,(KN<R> &)x, *bb,MatriceIdentite<R>(n),H,k,nbitermax,epsr,verb,stop);
}
if(verbosity>99) cout << " Sol GMRES :" << x << endl;
if(stop) delete stop;
return SetAny<long>(ret);
}
operator aType () const { return atype<long>();}
};
E_F0 * code(const basicAC_F0 & args) const {
return new E_LGMRES(args,cas);}
LinearGMRES() : OneOperator(atype<long>(),
atype<Polymorphic*>(),
atype<KN<R> *>(),atype<KN<R> *>()),cas(2){}
LinearGMRES(int cc) : OneOperator(atype<long>(),
atype<Polymorphic*>(),
atype<KN<R> *>()),cas(cc){}
};
template<class R>
basicAC_F0::name_and_type LinearGMRES<R>::E_LGMRES::name_param[]= {
{ "eps", &typeid(double) },
{ "nbiter",&typeid(long) },
{ "precon",&typeid(Polymorphic*)},
{ "veps" , &typeid(double*) },
{ "dimKrylov", &typeid(long) },
{ "verbosity", &typeid(long) },
{ "stop" , &typeid(Polymorphic*)}
};
template<typename int2>
typename map<int,int2>::iterator closeto(map<int,int2> & m, int k)
{
typename map<int,int2>::iterator i= m.find(k);
if (i==m.end())
{
i= m.find(k+1);
if (i==m.end())
i= m.find(k-1);
}
return i;
}
template<class T,int N>
class Smallvect { public:
T v[N];
T & operator[](int i){return v[i];}
const T & operator[](int i) const {return v[i];}
};
template<class T,int N>
ostream & operator<<(ostream & f,const Smallvect<T,N> & v)
{
for(int i=0;i<N;++i) f << v[i] << ' ';
return f;
}
template<class T>
int numeroteclink(KN_<T> & ndfv)
{
int nbdfv =0;
for (int i=0;i<ndfv.N();i++)
if (ndfv[i]>=i)
{
int j=i,ii,kkk=0;
do {
ii=ndfv[j];
ffassert(kkk++<10);
assert(nbdfv <= j);
// assert(ii>=nbdfv);
ndfv[j]=nbdfv ;
j=ii;
}
while (j!=nbdfv);
if (verbosity > 100)
cout << " ndf: " << j << " " << ii << " <- " << nbdfv << " " << kkk << endl;
nbdfv++;
}
return nbdfv;
}
bool InCircularList(const int *p,int i,int k)
// find k in circular list: i , p[i], p[p[i]], ...
{
int j=i,l=0;
do {
if (j==k) return true;
ffassert(l++<10);
j=p[j];
} while (j!=i);
return false;
}
bool BuildPeriodic(
int nbcperiodic,
Expression *periodic,
const Mesh &Th,Stack stack,
int & nbdfv, KN<int> & ndfv,int & nbdfe, KN<int> & ndfe) {
/*
build numbering of vertex form 0 to nbdfv-1
and build numbering of edge form 0 to nbdfe-1