/
PETSc-code.hpp
6572 lines (6352 loc) · 271 KB
/
PETSc-code.hpp
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
#include "petsc.h"
#include "PETSc.hpp"
#include "compositeFESpace.hpp"
typedef PETSc::DistributedCSR< HpSchwarz< PetscScalar > > Dmat;
typedef PETSc::DistributedCSR< HpSchwarz< PetscReal > > DmatR;
typedef PETSc::DistributedCSR< HpSchwarz< PetscComplex > > DmatC;
typedef PETSc::DistributedCSR< HpSchur< PetscScalar > > Dbddc;
typedef PETSc::DistributedCSR< HpSchur< PetscReal > > DbddcR;
typedef PETSc::DistributedCSR< HpSchur< PetscComplex > > DbddcC;
#if defined(WITH_bemtool) && defined(WITH_htool) && defined(PETSC_HAVE_HTOOL)
namespace PETSc {
template<typename P, typename MeshBemtool>
struct HtoolCtx {
htool::VirtualGenerator<PetscScalar>* generator;
bemtool::Dof<P>* dof;
MeshBemtool* mesh;
bemtool::Geometry* node;
HtoolCtx() : generator(), dof(), mesh(), node() { }
~HtoolCtx() {
delete generator;
delete dof;
delete mesh;
delete node;
}
};
template<typename P, typename MeshBemtool>
static PetscErrorCode GenEntriesFromCtx(PetscInt sdim,PetscInt M,PetscInt N, const PetscInt *const J, const PetscInt *const K, PetscScalar* ptr,void *ctx) {
PetscFunctionBeginUser;
HtoolCtx<P,MeshBemtool>* user = reinterpret_cast<HtoolCtx<P,MeshBemtool>*>(ctx);
user->generator->copy_submatrix(M,N,J,K,ptr);
PetscFunctionReturn(PETSC_SUCCESS);
}
template<typename P, typename MeshBemtool>
static PetscErrorCode DestroyHtoolCtx(void *ctx) {
HtoolCtx<P,MeshBemtool>* user = (HtoolCtx<P,MeshBemtool>*)ctx;
PetscFunctionBeginUser;
delete user;
PetscFunctionReturn(PETSC_SUCCESS);
}
template<template<typename P, typename MeshBemtool> class Gen, typename P, typename MeshBemtool, typename std::enable_if< std::is_same< HtoolCtx<P, MeshBemtool>, Gen<P, MeshBemtool> >::value >::type* = nullptr>
htool::VirtualGenerator<PetscScalar>* get_gen(Gen<P, MeshBemtool>* generator) {
return generator->generator;
}
template<template<typename P, typename MeshBemtool> class Gen, typename P, typename MeshBemtool, typename std::enable_if< !std::is_same< HtoolCtx<P, MeshBemtool>, Gen<P, MeshBemtool> >::value >::type* = nullptr>
htool::VirtualGenerator<PetscScalar>* get_gen(Gen<P, MeshBemtool>* generator) {
return generator;
}
template<class Matrix, template<typename P, typename MeshBemtool> class Gen, typename P, typename MeshBemtool, class R = PetscScalar, typename std::enable_if< std::is_same< Matrix, Dmat >::value >::type* = nullptr>
void Assembly(Matrix* A, Gen<P, MeshBemtool>* generator, string compressor,vector<double> &p1,vector<double> &p2,MPI_Comm comm,int dim,bool sym = false) {
PetscInt m, M;
KSPDestroy(&A->_ksp);
if(A->_vS) {
for(int i = 0; i < A->_vS->size(); ++i)
MatDestroy(&(*A->_vS)[i]);
delete A->_vS;
A->_vS = nullptr;
}
if(!A->_petsc) {
MatCreateHtoolFromKernel(PETSC_COMM_SELF,p2.size()/3,p1.size()/3,p2.size()/3,p1.size()/3,3,p2.data(),p1.data(),nullptr,get_gen(generator),&A->_petsc);
}
else {
Mat B;
PetscInt m, n, M, N, rbegin, cbegin;
MatGetLocalSize(A->_petsc, &m, &n);
MatGetSize(A->_petsc, &M, &N);
MatGetOwnershipRange(A->_petsc, &rbegin, NULL);
MatGetOwnershipRangeColumn(A->_petsc, &cbegin, NULL);
ffassert(N == p1.size()/3 && M == p2.size()/3);
MatCreateHtoolFromKernel(PetscObjectComm((PetscObject)A->_petsc),m,n,M,N,3,p2.data()+rbegin*3,p1.data()+cbegin*3,nullptr,get_gen(generator),&B);
MatHeaderReplace(A->_petsc, &B);
}
if(compressor.size()) {
PetscOptionsInsertString(NULL, compressor.c_str());
}
if(sym) {
MatSetOption(A->_petsc, MAT_SYMMETRIC, PETSC_TRUE);
}
MatSetFromOptions(A->_petsc);
MatAssemblyBegin(A->_petsc, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(A->_petsc, MAT_FINAL_ASSEMBLY);
if(std::is_same<HtoolCtx<P, MeshBemtool>, Gen<P, MeshBemtool>>::value) {
MatHtoolSetKernel(A->_petsc, GenEntriesFromCtx<P, MeshBemtool>, generator);
}
}
template<class fes1, class fes2, typename std::enable_if< (fes1::FESpace::Mesh::RdHat::d >= 3) || std::is_same<typename fes1::FESpace::Mesh, Mesh>::value >::type* = nullptr >
void varfBem(const typename fes1::FESpace*& PUh, const typename fes2::FESpace*& PVh, bool same, int VFBEM, Stack stack, const list<C_F0>& bargs, const Data_Sparse_Solver& ds, Dmat* B) {
ffassert(VFBEM == -1);
}
template<typename P, typename MeshBemtool, typename std::enable_if< std::is_same<P, bemtool::RT0_2D>::value || std::is_same<P, bemtool::P0_1D>::value || std::is_same<P, bemtool::P0_2D>::value >::type* = nullptr >
bemtool::Dof<P>* new_dof(MeshBemtool *mesh) {
return new bemtool::Dof<P>(*mesh);
}
template<typename P, typename MeshBemtool, typename std::enable_if< !std::is_same<P, bemtool::RT0_2D>::value && !std::is_same<P, bemtool::P0_1D>::value && !std::is_same<P, bemtool::P0_2D>::value >::type* = nullptr >
bemtool::Dof<P>* new_dof(MeshBemtool *mesh) {
return new bemtool::Dof<P>(*mesh, true);
}
template<typename P, typename MeshBemtool, typename Mesh1, bool T, typename Mesh2, typename FESpace2, typename std::enable_if< !T >::type* = nullptr >
void dispatch(MeshBemtool *mesh, bemtool::Geometry *node, int VFBEM, Stack stack, const list<C_F0>& bargs, const Data_Sparse_Solver& ds, Dmat* B, const Mesh2& ThV, std::vector<double>& p1, std::vector<double>& p2, bool same, const FESpace2& Vh) {
HtoolCtx<P,MeshBemtool>* ctx = new HtoolCtx<P,MeshBemtool>;
ctx->dof = new_dof<P>(mesh);
ctx->mesh = mesh;
ctx->node = node;
bemtool::Geometry node_output;
if (VFBEM == 1) {
pair<BemKernel*, std::complex<double>> kernel = getBemKernel(stack, bargs);
BemKernel *Ker = kernel.first;
std::complex<double> alpha = kernel.second;
ff_BIO_Generator_Maxwell<PetscScalar>(ctx->generator,Ker,*ctx->dof,alpha);
}
else if (VFBEM == 2) {
BemPotential *Pot = getBemPotential(stack, bargs);
if (Vh.MaxNbNodePerElement == Mesh2::RdHat::d+1)
Mesh2Bemtool(ThV,node_output);
else if (Vh.MaxNbNodePerElement == 1) {
int m = Vh.NbOfDF;
typename Mesh2::RdHat pbt(1./(Mesh2::RdHat::d+1),1./(Mesh2::RdHat::d+1));
for (int i=0; i<m; i++) {
Fem2D::R3 p = ThV[i](pbt);
bemtool::R3 q;
q[0]=p.x; q[1]=p.y; q[2]=p.z;
node_output.setnodes(q);
}
}
else
ffassert(0);
ff_POT_Generator_Maxwell<PetscScalar,P>(ctx->generator,Pot,*ctx->dof,*mesh,node_output);
}
if (same) {
PetscContainer ptr;
PetscObjectQuery((PetscObject)B->_petsc, "HtoolCtx", (PetscObject*)&ptr);
PetscContainerDestroy(&ptr);
Assembly(B,ctx,ds.sparams,p1,p1,MPI_COMM_NULL,Mesh1::RdHat::d+1,ds.sym);
PetscContainerCreate(PetscObjectComm((PetscObject)B->_petsc), &ptr);
PetscContainerSetPointer(ptr, ctx);
PetscContainerSetUserDestroy(ptr, DestroyHtoolCtx<P,MeshBemtool>);
PetscObjectCompose((PetscObject)B->_petsc, "HtoolCtx", (PetscObject)ptr);
}
else {
Assembly(B,ctx,ds.sparams,p1,p2,MPI_COMM_NULL,Mesh1::RdHat::d+1,ds.sym);
delete ctx;
}
}
template<typename P, typename MeshBemtool, typename Mesh1, bool T, typename Mesh2, typename FESpace2, typename std::enable_if< T >::type* = nullptr >
void dispatch(MeshBemtool *mesh, bemtool::Geometry *node, int VFBEM, Stack stack, const list<C_F0>& bargs, const Data_Sparse_Solver& ds, Dmat* B, const Mesh2& ThV, std::vector<double>& p1, std::vector<double>& p2, bool same, const FESpace2& Vh) {
HtoolCtx<P,MeshBemtool>* ctx = new HtoolCtx<P,MeshBemtool>;
ctx->dof = new_dof<P>(mesh);
ctx->mesh = mesh;
ctx->node = node;
bemtool::Geometry node_output;
if (VFBEM == 1) {
pair<BemKernel*, std::complex<double>> kernel = getBemKernel(stack, bargs);
BemKernel *Ker = kernel.first;
std::complex<double> alpha = kernel.second;
ff_BIO_Generator<PetscScalar,P,Mesh1>(ctx->generator,Ker,*ctx->dof,alpha);
}
else if (VFBEM == 2) {
BemPotential *Pot = getBemPotential(stack, bargs);
if (Vh.MaxNbNodePerElement == Mesh2::RdHat::d+1)
Mesh2Bemtool(ThV,node_output);
else if (Vh.MaxNbNodePerElement == 1) {
int m = Vh.NbOfDF;
typename Mesh2::RdHat pbt(1./(Mesh2::RdHat::d+1),1./(Mesh2::RdHat::d+1));
for (int i=0; i<m; i++) {
Fem2D::R3 p = ThV[i](pbt);
bemtool::R3 q;
q[0]=p.x; q[1]=p.y; q[2]=p.z;
node_output.setnodes(q);
}
}
else
ffassert(0);
ff_POT_Generator<PetscScalar,P,MeshBemtool,Mesh1>(ctx->generator,Pot,*ctx->dof,*mesh,node_output);
}
if (same) {
PetscContainer ptr;
PetscObjectQuery((PetscObject)B->_petsc, "HtoolCtx", (PetscObject*)&ptr);
PetscContainerDestroy(&ptr);
Assembly(B,ctx,ds.sparams,p1,p1,MPI_COMM_NULL,Mesh1::RdHat::d+1,ds.sym);
PetscContainerCreate(PetscObjectComm((PetscObject)B->_petsc), &ptr);
PetscContainerSetPointer(ptr, ctx);
PetscContainerSetUserDestroy(ptr, DestroyHtoolCtx<P,MeshBemtool>);
PetscObjectCompose((PetscObject)B->_petsc, "HtoolCtx", (PetscObject)ptr);
}
else {
Assembly(B,ctx,ds.sparams,p1,p2,MPI_COMM_NULL,Mesh1::RdHat::d+1,ds.sym);
delete ctx;
}
}
template<class fes1, class fes2, typename std::enable_if< (fes1::FESpace::Mesh::RdHat::d < 3) && !std::is_same<typename fes1::FESpace::Mesh, Mesh>::value >::type* = nullptr >
void varfBem(const typename fes1::FESpace*& PUh, const typename fes2::FESpace*& PVh, bool same, int VFBEM, Stack stack, const list<C_F0>& bargs, const Data_Sparse_Solver& ds, Dmat* B) {
if (VFBEM == 1)
ffassert(same);
typedef typename fes1::pfes pfes1;
typedef typename fes1::FESpace FESpace1;
typedef typename FESpace1::Mesh Mesh1;
typedef typename fes2::pfes pfes2;
typedef typename fes2::FESpace FESpace2;
typedef typename FESpace2::Mesh Mesh2;
typedef typename std::conditional<Mesh1::RdHat::d==1, bemtool::Mesh1D, bemtool::Mesh2D>::type MeshBemtool;
typedef typename std::conditional<Mesh1::RdHat::d==1, bemtool::P0_1D, bemtool::P0_2D>::type P0;
typedef typename std::conditional<Mesh1::RdHat::d==1, bemtool::P1_1D, bemtool::P1_2D>::type P1;
typedef typename std::conditional<Mesh1::RdHat::d==1, bemtool::P2_1D, bemtool::P2_2D>::type P2;
const FESpace1& Uh = *PUh;
const FESpace2& Vh = *PVh;
const Mesh1& ThU = Uh.Th; // line
const Mesh2& ThV = Vh.Th; // colunm
int n = Uh.NbOfDF;
int m = Vh.NbOfDF;
bemtool::Geometry *node = new bemtool::Geometry;
MeshBemtool *mesh = new MeshBemtool;
Mesh2Bemtool(ThU, *node, *mesh);
bemtool::Dof<P1> *dof = new bemtool::Dof<P1>(*mesh,true);
vector<double> p1;
p1.reserve(3*n);
vector<double> p2;
typename Mesh1::RdHat pbs(1./(Mesh1::RdHat::d+1),1./(Mesh1::RdHat::d+1));
int Snbv = Uh.TFE[0]->ndfonVertex;
int Snbe = Uh.TFE[0]->ndfonEdge;
int Snbt = Uh.TFE[0]->ndfonFace;
bool SP0 = Mesh1::RdHat::d == 1 ? (Snbv == 0) && (Snbe == 1) && (Snbt == 0) : (Snbv == 0) && (Snbe == 0) && (Snbt == 1);
bool SP1 = (Snbv == 1) && (Snbe == 0) && (Snbt == 0);
bool SP2 = (Snbv == 1) && (Snbe == 1) && (Snbt == 0);
bool SRT0 = (Mesh1::RdHat::d == 2) && (Snbv == 0) && (Snbe == 1) && (Snbt == 0);
if (SP2) {
bemtool::Dof<P2> dof(*mesh,true);
for (int i=0; i<n; i++) {
const std::vector<bemtool::N2>& j = dof.ToElt(i);
bemtool::R3 p = dof(j[0][0])[j[0][1]];
p1.emplace_back(p[0]);
p1.emplace_back(p[1]);
p1.emplace_back(p[2]);
}
}
else if (SRT0) {
bemtool::Dof<bemtool::RT0_2D> dof(*mesh);
for (int i=0; i<n; i++) {
const std::vector<bemtool::N2>& j = dof.ToElt(i);
bemtool::R3 p = dof(j[0][0])[j[0][1]];
p1.emplace_back(p[0]);
p1.emplace_back(p[1]);
p1.emplace_back(p[2]);
}
}
else {
for (int i=0; i<n; i++) {
Fem2D::R3 p;
if (SP1)
p = ThU.vertices[i];
else if (SP0)
p = ThU[i](pbs);
else {
if (mpirank == 0) std::cerr << "ff-BemTool error: only P0, P1 and P2 discretizations are available for now." << std::endl;
ffassert(0);
}
p1.emplace_back(p.x);
p1.emplace_back(p.y);
p1.emplace_back(p.z);
}
}
if (!same) {
if(Vh.TFE[0]->N == 1) {
typename Mesh2::RdHat pbt(1./(Mesh2::RdHat::d+1),1./(Mesh2::RdHat::d+1));
p2.reserve(3*m);
for (int i=0; i<m; i++) {
Fem2D::R3 p;
if (Vh.MaxNbNodePerElement == Mesh2::RdHat::d+1)
p = ThV.vertices[i];
else if (Vh.MaxNbNodePerElement == 1)
p = ThV[i](pbt);
else {
if (mpirank == 0) std::cerr << "ff-BemTool error: only P0 and P1 FEspaces are available for reconstructions." << std::endl;
ffassert(0);
}
p2.emplace_back(p.x);
p2.emplace_back(p.y);
p2.emplace_back(p.z);
}
}
else {
ffassert(SRT0 && Mesh1::RdHat::d == 2 && VFBEM == 2);
int nnn = Vh.TFE[0]->N;
typename Mesh2::RdHat pbt(1./(Mesh2::RdHat::d+1),1./(Mesh2::RdHat::d+1));
p2.reserve(3*m);
int mDofScalar = m/nnn; // computation of the dof of one component
for (int i=0; i<mDofScalar; i++) {
Fem2D::R3 p;
if (Vh.MaxNbNodePerElement == Mesh2::RdHat::d + 1)
p = ThV.vertices[i];
else if (Vh.MaxNbNodePerElement == 1)
p = ThV[i](pbt);
else {
if (mpirank == 0) std::cerr << "ff-BemTool error: only P0 and P1 FEspaces are available for reconstructions." << std::endl;
ffassert(0);
}
for(int iii=0; iii<nnn; iii++){
ffassert( nnn*3*i+3*iii+2 < nnn*3*m );
p2.emplace_back(p.x);
p2.emplace_back(p.y);
p2.emplace_back(p.z);
}
}
}
}
if (SP1)
dispatch<P1,MeshBemtool,Mesh1,true>(mesh, node, VFBEM, stack, bargs, ds, B, ThV, p1, p2, same, Vh);
else if (SP0)
dispatch<P0,MeshBemtool,Mesh1,true>(mesh, node, VFBEM, stack, bargs, ds, B, ThV, p1, p2, same, Vh);
else if (SP2)
dispatch<P2,MeshBemtool,Mesh1,true>(mesh, node, VFBEM, stack, bargs, ds, B, ThV, p1, p2, same, Vh);
else if (SRT0 && Mesh1::RdHat::d == 2) {
dispatch<bemtool::RT0_2D,MeshBemtool,Mesh1,false>(mesh, node, VFBEM, stack, bargs, ds, B, ThV, p1, p2, same, Vh);
}
else
ffassert(0);
}
} // namespace PETSc
#endif
namespace PETSc {
template<class K, class MMesh, class fes1, class fes2 >
struct varfToMat : public OneOperator {
class Op : public E_F0mps {
public:
Call_FormBilinear<fes1, fes2>* b;
Expression a;
AnyType operator()(Stack s) const;
Op(Expression x, Expression y) : b(new Call_FormBilinear<fes1, fes2>(*dynamic_cast<const Call_FormBilinear<fes1, fes2>*>(y))), a(x) {
assert(b && b->nargs);
ffassert(FieldOfForm(b->largs, IsComplexType<upscaled_type<K>>::value) == IsComplexType<upscaled_type<K>>::value);
#if defined(WITH_bemtool) && defined(WITH_htool) && defined(PETSC_HAVE_HTOOL)
// Check the nbitem of inconnu and test in BemFormBilinear
checkNbItemFEspacesInconnuAndTest(b->largs,b->N,b->M);
#endif
}
operator aType () const { return atype<Dmat*>(); }
};
E_F0* code(const basicAC_F0& args) const {
return new Op(to<Dmat*>(args[0]), args[1]);
}
varfToMat() : OneOperator(atype<Dmat*>(), atype<Dmat*>(), atype<const Call_FormBilinear<fes1, fes2>*>()) {}
};
template<class fes1, class fes2, typename std::enable_if< std::is_same< fes1, fes2 >::value >::type* = nullptr >
void assert_ptr(fes1* pUh, fes2* pVh) {
ffassert(pUh == pVh);
}
template<class fes1, class fes2, typename std::enable_if< !std::is_same< fes1, fes2 >::value >::type* = nullptr >
void assert_ptr(fes1* pUh, fes2* pVh) { }
template<class K, class MMesh, class fes1, class fes2>
AnyType varfToMat<K, MMesh, fes1, fes2>::Op::operator()(Stack stack) const {
typedef typename fes1::pfes pfes1;
typedef typename fes1::FESpace FESpace1;
typedef typename FESpace1::Mesh Mesh1;
typedef typename fes2::pfes pfes2;
typedef typename fes2::FESpace FESpace2;
typedef typename FESpace2::Mesh Mesh2;
assert(b && b->nargs);
pfes1* pUh = GetAny<pfes1*>((*b->euh)(stack));
pfes2* pVh = GetAny<pfes2*>((*b->evh)(stack));
const FESpace1* PUh = (FESpace1*)**pUh;
const FESpace2* PVh = (FESpace2*)**pVh;
Data_Sparse_Solver ds;
ds.factorize = 0;
ds.initmat = true;
int np = OpCall_FormBilinear_np::n_name_param - NB_NAME_PARM_HMAT;
SetEnd_Data_Sparse_Solver<upscaled_type<K>>(stack, ds, b->nargs, np);
WhereStackOfPtr2Free(stack) = new StackOfPtr2Free(stack);
Dmat& B(*GetAny<Dmat*>((*a)(stack)));
if(!PUh || !PVh)
return SetAny<Dmat*>(&B);
const FESpace1& Uh = *PUh;
const FESpace2& Vh = *PVh;
const Mesh1& Th = Uh.Th;
bool same = isSameMesh(b->largs, &Uh.Th, &Vh.Th, stack);
#if defined(WITH_bemtool) && defined(WITH_htool) && defined(PETSC_HAVE_HTOOL)
int VFBEM = typeVFBEM(b->largs, stack);
#else
int VFBEM = -1;
#endif
if (VFBEM == -1) {
ffassert((std::is_same< fes1, fes2 >::value));
Matrice_Creuse<upscaled_type<K>> A;
A.init();
if(same) {
if(A.Uh != Uh || A.Vh != Vh) {
A.Uh = Uh;
A.Vh = Vh;
if(ds.sym) {
A.A.master(new MatriceMorse<upscaled_type<K>>(Vh.NbOfDF, Vh.NbOfDF, 0, ds.sym));
assert_ptr(&Uh, &Vh);
}
else
A.A.master(new MatriceMorse<upscaled_type<K>>(Vh.NbOfDF, Uh.NbOfDF, 2 * Vh.NbOfDF, 0));
}
if(AssembleVarForm<upscaled_type<K>, MatriceCreuse<upscaled_type<K>>, MMesh, FESpace1,FESpace2>(stack, Th, Uh, Vh, ds.sym, A.A, 0, b->largs))
AssembleBC<upscaled_type<K>, MMesh,FESpace1, FESpace2>(stack, Th, Uh, Vh, ds.sym, A.A, 0, 0, b->largs, ds.tgv);
}
else {
MatriceMorse<upscaled_type<K>> *pMA = new MatriceMorse<upscaled_type<K>>(Vh.NbOfDF, Uh.NbOfDF, 0, ds.sym);
MatriceMap<upscaled_type<K>>& D = *pMA;
bool bc = AssembleVarForm<upscaled_type<K>, MatriceMap<upscaled_type<K>>, MMesh, FESpace1,FESpace2>(stack, Th, Uh, Vh, ds.sym, &D, 0, b->largs);
A.A.master(pMA);
if(bc)
AssembleBC<upscaled_type<K>>(stack, Th, Uh, Vh, ds.sym, A.A, 0, 0, b->largs, ds.tgv);
}
changeOperatorSimple(&B, &A);
if(B._A)
B._A->setMatrix(nullptr);
}
#if defined(WITH_bemtool) && defined(WITH_htool) && defined(PETSC_HAVE_HTOOL)
else {
varfBem<fes1, fes2>(PUh, PVh, same, VFBEM, stack, b->largs, ds, &B);
}
#endif
return SetAny<Dmat*>(&B);
}
} // namespace PETSc
namespace PETSc {
template< class Type >
struct _n_User;
template< class Type >
using User = _n_User< Type >*;
template< bool C, class HpddmType,
typename std::enable_if< std::is_same< HpddmType, Dmat >::value >::type* = nullptr >
void initPETScStructure(
HpddmType* ptA, PetscInt bs, PetscBool symmetric,
KN< typename std::conditional< std::is_same< HpddmType, Dmat >::value, double, long >::type >* ptD) {
double timing = MPI_Wtime( );
long long global;
if (ptD) {
if(!ptA->_D) {
PetscReal* d = reinterpret_cast<PetscReal*>(ptD->operator double*());
if(!std::is_same<upscaled_type<PetscReal>, PetscReal>::value) {
for(int i = 0; i < ptD->n; ++i)
d[i] = ptD->operator[](i);
}
ptA->_A->restriction(d);
if (!C) ptA->_A->initialize(d);
else {
ptA->_D = new KN<PetscReal>(ptD->n);
for(int i = 0; i < ptD->n; ++i)
ptA->_D->operator[](i) = d[i];
ptA->_A->initialize(*ptA->_D);
}
}
else {
ptA->_A->initialize(*ptA->_D);
}
}
if (!C && !ptD) global = PETSC_DECIDE;
else ptA->_A->distributedNumbering(ptA->_num, ptA->_first, ptA->_last, global);
if (verbosity > 0 && mpirank == 0)
cout << " --- global numbering created (in " << MPI_Wtime( ) - timing << ")" << endl;
timing = MPI_Wtime( );
PetscInt* ia = nullptr;
PetscInt* ja = nullptr;
PetscScalar* c = nullptr;
bool free = ptA->_A->getMatrix( )->HPDDM_ia
? ptA->_A->distributedCSR(ptA->_num, ptA->_first, ptA->_last, ia, ja, c)
: false;
MatCreate(ptA->_A->getCommunicator(), &ptA->_petsc);
if (bs > 1) MatSetBlockSize(ptA->_petsc, bs);
MatSetSizes(ptA->_petsc, ptA->_last - ptA->_first, ptA->_last - ptA->_first, global, global);
bool sym = ptA->_A->getMatrix( )->HPDDM_sym;
if (ia) {
if (sym) {
MatSetType(ptA->_petsc, MATSBAIJ);
MatSetUp(ptA->_petsc);
MatSeqSBAIJSetPreallocationCSR(ptA->_petsc, 1, ia, ja, c);
MatMPISBAIJSetPreallocationCSR(ptA->_petsc, 1, ia, ja, c);
} else {
MatSetType(ptA->_petsc, MATAIJ);
MatSeqAIJSetPreallocationCSR(ptA->_petsc, ia, ja, c);
MatMPIAIJSetPreallocationCSR(ptA->_petsc, ia, ja, c);
MatSetOption(ptA->_petsc, MAT_SYMMETRIC, symmetric);
}
} else {
MatSetType(ptA->_petsc, MATAIJ);
MatSetUp(ptA->_petsc);
}
if (free) {
delete[] ia;
delete[] ja;
delete[] c;
}
ptA->_A->setBuffer( );
if (verbosity > 0 && mpirank == 0)
cout << " --- global CSR created (in " << MPI_Wtime( ) - timing << ")" << endl;
}
template< bool C, class HpddmType,
typename std::enable_if< !std::is_same< HpddmType, Dmat >::value >::type* = nullptr >
void initPETScStructure(
HpddmType* ptA, PetscInt& bs, PetscBool symmetric,
KN< typename std::conditional< std::is_same< HpddmType, Dmat >::value, double, long >::type >* ptD) {
const HPDDM::MatrixCSR< PetscScalar >* M = ptA->_A->getMatrix( );
if (!M->HPDDM_sym) cout << "Please assemble a symmetric CSR" << endl;
double timing = MPI_Wtime( );
ptA->_A->template renumber< false >(STL< long >(*ptD), nullptr);
long long global;
ptA->_A->distributedNumbering(ptA->_num, ptA->_first, ptA->_last, global);
if (verbosity > 0 && mpirank == 0)
cout << " --- global numbering created (in " << MPI_Wtime( ) - timing << ")" << endl;
timing = MPI_Wtime( );
PetscInt* indices;
PetscMalloc(sizeof(PetscInt) * M->HPDDM_n / bs, &indices);
for (unsigned int i = 0; i < M->HPDDM_n; i += bs) indices[i / bs] = ptA->_num[i] / bs;
ISLocalToGlobalMapping rmap;
ISLocalToGlobalMappingCreate(ptA->_A->getCommunicator(), bs, M->HPDDM_n / bs, indices, PETSC_OWN_POINTER,
&rmap);
MatCreateIS(ptA->_A->getCommunicator(), bs, PETSC_DECIDE, PETSC_DECIDE, global, global, rmap, NULL,
&ptA->_petsc);
Mat local;
MatISGetLocalMat(ptA->_petsc, &local);
MatSetType(local, MATSEQSBAIJ);
MatSetUp(local);
std::vector< std::vector< std::pair< int, PetscScalar > > > transpose(M->HPDDM_n);
for (int i = 0; i < transpose.size( ); ++i)
for (int j = M->HPDDM_ia[i]; j < M->HPDDM_ia[i + 1]; ++j) {
transpose[M->HPDDM_ja[j]].emplace_back(i, M->HPDDM_a[j]);
if (bs > 1 && (i - M->HPDDM_ja[j] <= (i % bs)) && M->HPDDM_ja[j] != i)
transpose[i].emplace_back(M->HPDDM_ja[j], M->HPDDM_a[j]);
}
int nnz = 0;
for (int i = 0; i < transpose.size( ); ++i) {
std::sort(transpose[i].begin( ), transpose[i].end( ),
[](const std::pair< int, PetscScalar >& lhs,
const std::pair< int, PetscScalar >& rhs) { return lhs.first < rhs.first; });
nnz += transpose[i].size( );
}
PetscInt* ia = new PetscInt[M->HPDDM_n / bs + 1];
PetscInt* ja = new PetscInt[nnz / (bs * bs)];
PetscScalar* a = new PetscScalar[nnz];
ia[0] = 0;
for (int i = 0; i < transpose.size( ); ++i) {
for (int j = 0; j < transpose[i].size( ); ++j) {
if (i % bs == 0 && j % bs == 0) ja[ia[i / bs] + j / bs] = transpose[i][j].first / bs;
a[ia[i / bs] * (bs * bs) + j % bs + (j / bs) * (bs * bs) + (i % bs) * bs] =
transpose[i][j].second;
}
if (i % bs == 0) ia[i / bs + 1] = ia[i / bs] + transpose[i].size( ) / bs;
}
MatSeqSBAIJSetPreallocationCSR(local, bs, ia, ja, a);
MatSetOption(ptA->_petsc, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE);
MatSetOption(ptA->_petsc, MAT_SYMMETRIC, symmetric);
MatAssemblyBegin(ptA->_petsc, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(ptA->_petsc, MAT_FINAL_ASSEMBLY);
delete[] a;
delete[] ja;
delete[] ia;
IS to, from;
PetscInt nr;
Vec rglobal;
ISLocalToGlobalMappingGetSize(rmap, &nr);
ISCreateStride(PETSC_COMM_SELF, nr, 0, 1, &to);
ISLocalToGlobalMappingApplyIS(rmap, to, &from);
MatCreateVecs(ptA->_petsc, &rglobal, NULL);
Vec isVec;
VecCreate(PETSC_COMM_SELF, &isVec);
VecSetType(isVec, VECSTANDARD);
VecSetSizes(isVec, PETSC_DECIDE, nr);
VecScatterCreate(rglobal, from, isVec, to, &ptA->_scatter);
VecDestroy(&isVec);
VecDestroy(&rglobal);
ISDestroy(&from);
ISDestroy(&to);
// ISLocalToGlobalMappingView(rmap, PETSC_VIEWER_STDOUT_WORLD);
ISLocalToGlobalMappingDestroy(&rmap);
if (verbosity > 0 && mpirank == 0)
cout << " --- global CSR created (in " << MPI_Wtime( ) - timing << ")" << endl;
}
template< class Type, class K >
long globalNumbering(Type* const& A, KN< K >* const& numbering) {
if (A) {
numbering->resize(A->_A->getDof( ));
if (A->_num)
for (int i = 0; i < numbering->n; ++i) numbering->operator[](i) = A->_num[i];
}
return 0L;
}
static Mat ff_to_PETSc(const HPDDM::MatrixCSR< PetscScalar >* const A) {
Mat aux;
if (A->HPDDM_sym) {
std::vector< std::pair< int, int > >* transpose =
new std::vector< std::pair< int, int > >[A->HPDDM_n]( );
for (int i = 0; i < A->HPDDM_n; ++i)
for (int j = A->HPDDM_ia[i] - (HPDDM_NUMBERING == 'F');
j < A->HPDDM_ia[i + 1] - (HPDDM_NUMBERING == 'F'); ++j)
transpose[A->HPDDM_ja[j] - (HPDDM_NUMBERING == 'F')].emplace_back(i, j);
for (int i = 0; i < A->HPDDM_n; ++i)
std::sort(transpose[i].begin( ), transpose[i].end( ));
PetscInt* ia = new PetscInt[A->HPDDM_n + 1];
PetscInt* ja = new PetscInt[A->HPDDM_nnz];
PetscScalar* c = new PetscScalar[A->HPDDM_nnz];
ia[0] = 0;
for (int i = 0; i < A->HPDDM_n; ++i) {
for (int j = 0; j < transpose[i].size( ); ++j) {
c[ia[i] + j] = A->HPDDM_a[transpose[i][j].second];
ja[ia[i] + j] = transpose[i][j].first;
}
ia[i + 1] = ia[i] + transpose[i].size( );
}
delete[] transpose;
MatCreate(PETSC_COMM_SELF, &aux);
MatSetSizes(aux, A->HPDDM_n, A->HPDDM_n, A->HPDDM_n, A->HPDDM_n);
MatSetType(aux, MATSEQSBAIJ);
MatSetUp(aux);
MatSeqSBAIJSetPreallocationCSR(aux, 1, ia, ja, c);
delete[] c;
delete[] ja;
delete[] ia;
} else if(std::is_same<decltype(A->HPDDM_ia), PetscInt>::value)
MatCreateSeqAIJWithArrays(PETSC_COMM_SELF, A->HPDDM_n, A->HPDDM_m, reinterpret_cast<PetscInt*>(A->HPDDM_ia), reinterpret_cast<PetscInt*>(A->HPDDM_ja), A->HPDDM_a, &aux);
else {
MatCreate(PETSC_COMM_SELF, &aux);
MatSetSizes(aux, A->HPDDM_n, A->HPDDM_n, A->HPDDM_n, A->HPDDM_n);
MatSetType(aux, MATSEQAIJ);
#if defined(PETSC_USE_64BIT_INDICES)
PetscInt* ia = new PetscInt[A->HPDDM_n + 1];
std::copy_n(A->HPDDM_ia, A->HPDDM_n + 1, ia);
PetscInt* ja = new PetscInt[A->HPDDM_nnz];
std::copy_n(A->HPDDM_ja, A->HPDDM_nnz, ja);
PetscScalar* c = new PetscScalar[A->HPDDM_nnz];
std::copy_n(A->HPDDM_a, A->HPDDM_nnz, c);
MatSeqAIJSetPreallocationCSR(aux, ia, ja, c);
delete[] c;
delete[] ja;
delete[] ia;
#else
MatSeqAIJSetPreallocationCSR(aux, A->HPDDM_ia, A->HPDDM_ja, A->HPDDM_a);
#endif
}
return aux;
}
template< class Type >
long ParMmgCommunicators(Type* const& A, KN< double >* const& gamma, KN< long >* const& rest, KN< KN< long >>* const& communicators) {
if (A && A->_petsc && A->_A) {
std::unordered_map<int, std::pair<int, PetscInt>> map;
PetscScalar* val = new PetscScalar[A->_A->getDof()]();
for(int i = 0; i < rest->n; ++i) {
if(std::abs(gamma->operator[](i) - 1.0) < 1.0e-6) {
map[rest->operator[](i)] = std::make_pair(i, A->_num[rest->operator[](i)]);
val[rest->operator[](i)] = HPDDM::Wrapper<PetscScalar>::d__1;
}
}
A->_A->recvBuffer(val);
delete [] val;
PetscScalar** const buffer = A->_A->getBuffer();
const HPDDM::vectorNeighbor& neighbors = A->_A->getMap();
communicators->resize(2 * neighbors.size() + 1);
communicators->operator[](0).resize(neighbors.size());
unsigned short k = 0;
for(unsigned short i = 0; i < neighbors.size(); ++i) {
communicators->operator[](0)[k] = neighbors[i].first;
communicators->operator[](2 * k + 1).resize(neighbors[i].second.size());
communicators->operator[](2 * k + 2).resize(neighbors[i].second.size());
int m = 0;
for(unsigned int j = 0; j < neighbors[i].second.size(); ++j) {
if(std::abs(buffer[i][j] - HPDDM::Wrapper<PetscScalar>::d__1) < 1.0e-6) {
std::unordered_map<int, std::pair<int, PetscInt>>::const_iterator it = map.find(neighbors[i].second[j]);
if(it != map.cend()) {
communicators->operator[](2 * k + 1)[m] = it->second.first + 1;
communicators->operator[](2 * k + 2)[m++] = it->second.second + 1;
}
}
}
if(m > 0) {
communicators->operator[](2 * k + 1).resize(m);
communicators->operator[](2 * k + 2).resize(m);
++k;
}
}
int size;
MPI_Comm_size(A->_A->getCommunicator(), &size);
assert(size == 1 || k);
communicators->operator[](0).resize(k);
communicators->resize(2 * k + 1);
}
return 0L;
}
template< class Type >
class changeOperator : public OneOperator {
public:
const int c;
class changeOperator_Op : public E_F0mps {
public:
Expression A;
Expression B;
const int c;
static const int n_name_param = 2;
static basicAC_F0::name_and_type name_param[];
Expression nargs[n_name_param];
changeOperator_Op(const basicAC_F0& args, int d) : A(0), B(0), c(d) {
args.SetNameParam(n_name_param, name_param, nargs);
A = to< Type* >(args[0]);
if (c == 0)
B = to< Matrice_Creuse< upscaled_type<PetscScalar> >* >(args[1]);
else
B = to< Type* >(args[1]);
}
AnyType operator( )(Stack stack) const;
operator aType( ) const { return atype< long >( ); }
};
E_F0* code(const basicAC_F0& args) const { return new changeOperator_Op(args, c); }
changeOperator( )
: OneOperator(atype< long >( ), atype< Type* >( ),
atype< Matrice_Creuse< upscaled_type<PetscScalar> >* >( )),
c(0) {}
changeOperator(int)
: OneOperator(atype< long >( ), atype< Type* >( ), atype< Type* >( )), c(1) {}
};
template< class Type >
basicAC_F0::name_and_type changeOperator< Type >::changeOperator_Op::name_param[] = {
{"restriction", &typeid(Matrice_Creuse< double >*)}, {"parent", &typeid(Type*)}};
template< class Type >
void change(Type* const& ptA, Matrice_Creuse< upscaled_type<PetscScalar> >* const& mat, Type* const& ptB,
Matrice_Creuse< double >* const& pList, Type* const& ptParent) {
if (mat) {
if (ptA) {
MatriceMorse< upscaled_type<PetscScalar> >* mN = nullptr;
if (mat->A) mN = static_cast< MatriceMorse< upscaled_type<PetscScalar> >* >(&*(mat->A));
PetscBool assembled = PETSC_FALSE;
if (ptA->_petsc) MatAssembled(ptA->_petsc, &assembled);
if (mN) {
HPDDM::MatrixCSR< void >* dL = nullptr;
if (pList && pList->A) {
MatriceMorse< double >* mList = static_cast< MatriceMorse< double >* >(&*(pList->A));
ffassert(mList->n == mList->nnz);
ffassert(mList->m == mN->n);
dL = new_HPDDM_MatrixCSRvoid(mList, false);
}
HPDDM::MatrixCSR< PetscScalar >* dM = new_HPDDM_MatrixCSR< PetscScalar >(mN);
HPDDM::MatrixCSR< PetscScalar >* dN;
if (!dL)
dN = dM;
else {
unsigned int* perm = new unsigned int[dM->HPDDM_n]( );
for (unsigned int i = 0; i < dL->HPDDM_n; ++i) perm[dL->HPDDM_ja[i]] = i + 1;
dN = new HPDDM::MatrixCSR< PetscScalar >(dM, dL, perm);
delete[] perm;
delete dM;
dM = nullptr;
}
if (ptA->_A) {
ptA->_A->setMatrix(dN);
#if defined(PCHPDDM) && defined(PETSC_USE_SHARED_LIBRARIES)
PC pc = nullptr;
if(ptParent) {
PetscInt M, N;
Mat** mat;
MatNestGetSubMats(ptParent->_petsc, &M, &N, &mat);
PetscInt i;
for(i = 0; i < std::min(N, M); ++i) {
if(mat[i][i] == ptA->_petsc)
break;
}
if(i < std::min(N, M)) {
PC parent;
KSPGetPC(ptParent->_ksp, &parent);
KSP *subksp;
PetscInt nsplits;
PCFieldSplitGetSubKSP(parent, &nsplits, &subksp);
KSPGetPC(subksp[i], &pc);
PetscFree(subksp);
}
}
else if(ptA->_ksp) {
KSPGetPC(ptA->_ksp, &pc);
}
if(pc) {
PCType type;
PCGetType(pc, &type);
PetscBool isType;
PetscStrcmp(type, PCHPDDM, &isType);
if(isType) {
const HPDDM::MatrixCSR<PetscScalar>* const A = ptA->_A->getMatrix();
Mat aux = ff_to_PETSc(A);
Mat N;
PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject*)&N);
if(!N) {
PetscInt* idx;
PetscMalloc1(dN->HPDDM_n, &idx);
std::copy_n(ptA->_num, dN->HPDDM_n, idx);
IS is;
ISCreateGeneral(PETSC_COMM_SELF, ptA->_A->getMatrix()->HPDDM_n, idx, PETSC_OWN_POINTER, &is);
PetscObjectCompose((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject)aux);
if (!A->HPDDM_sym && ptA->_petsc) {
PetscInt bs;
MatGetBlockSize(ptA->_petsc, &bs);
if (bs > 1) {
ISSetBlockSize(is, bs);
MatSetBlockSize(aux, bs);
}
}
PCHPDDMSetAuxiliaryMat(pc, is, aux, NULL, NULL);
PCSetFromOptions(pc);
MatDestroy(&aux);
ISDestroy(&is);
}
else {
PetscObjectCompose((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject)aux);
if (!A->HPDDM_sym && ptA->_petsc) {
PetscInt bs;
MatGetBlockSize(ptA->_petsc, &bs);
if (bs > 1)
MatSetBlockSize(aux, bs);
}
PCHPDDMSetAuxiliaryMat(pc, NULL, aux, NULL, NULL);
MatDestroy(&aux);
}
}
}
#endif
}
ffassert(ptA->_num);
PetscInt* ia = nullptr;
PetscInt* ja = nullptr;
PetscScalar* c = nullptr;
bool free = true;
if (ptA->_cnum) {
PetscInt* cnum = ptA->_num + dN->HPDDM_n;
if (ptA->_petsc) {
int rank, size, N;
MPI_Comm_size(PetscObjectComm((PetscObject)ptA->_petsc), &size);
if (size > 1) {
const PetscInt* ranges;
MatGetOwnershipRangesColumn(ptA->_petsc, &ranges);
MPI_Comm_rank(PetscObjectComm((PetscObject)ptA->_petsc), &rank);
MatGetSize(ptA->_petsc, NULL, &N);
if (ranges[1] == N) {
if (rank != 0)
cnum = new PetscInt[N];
MPI_Bcast(cnum, N, MPIU_INT, 0, PetscObjectComm((PetscObject)ptA->_petsc));
}
}
}
free = HPDDM::template Subdomain< PetscScalar >::distributedCSR(
ptA->_num, ptA->_first, ptA->_last, ia, ja, c, dN, cnum);
if (cnum != ptA->_num + dN->HPDDM_n)
delete [] cnum;
}
else
free = ptA->_A->distributedCSR(ptA->_num, ptA->_first, ptA->_last, ia, ja, c);
if (assembled) {
MatZeroEntries(ptA->_petsc);
for (PetscInt i = 0; i < ptA->_last - ptA->_first; ++i) {
PetscInt row = ptA->_first + i;
MatSetValues(ptA->_petsc, 1, &row, ia[i + 1] - ia[i],
ja + ia[i], c + ia[i], INSERT_VALUES);
}
MatAssemblyBegin(ptA->_petsc, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(ptA->_petsc, MAT_FINAL_ASSEMBLY);
} else {
if (!ptA->_petsc) {
MatCreate(PETSC_COMM_WORLD, &ptA->_petsc);
MatSetSizes(ptA->_petsc, ptA->_last - ptA->_first, ptA->_last - ptA->_first,
PETSC_DECIDE, PETSC_DECIDE);
}
if (ptA->_A && ptA->_A->getMatrix( )->HPDDM_sym) {
MatSetType(ptA->_petsc, MATSBAIJ);
MatSetUp(ptA->_petsc);
MatSeqSBAIJSetPreallocationCSR(ptA->_petsc, 1, ia, ja, c);
MatMPISBAIJSetPreallocationCSR(ptA->_petsc, 1, ia, ja, c);
} else {
MatSetType(ptA->_petsc, MATAIJ);
MatSeqAIJSetPreallocationCSR(ptA->_petsc, ia, ja, c);
MatMPIAIJSetPreallocationCSR(ptA->_petsc, ia, ja, c);
}
}
if (free) {
delete[] ia;
delete[] ja;
delete[] c;
}
} else if (!assembled) {
PetscInt m;
MatGetLocalSize(ptA->_petsc, &m, NULL);
PetscInt* ia = new PetscInt[m + 1]( );
MatSeqAIJSetPreallocationCSR(ptA->_petsc, ia, NULL, NULL);
MatMPIAIJSetPreallocationCSR(ptA->_petsc, ia, NULL, NULL);
delete[] ia;
} else {
MatAssemblyBegin(ptA->_petsc, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(ptA->_petsc, MAT_FINAL_ASSEMBLY);
}
if (ptParent) {
PetscBool assembled;
MatAssembled(ptParent->_petsc, &assembled);
if (!assembled) {
PetscInt M, N;
Mat** mat;
MatNestGetSubMats(ptParent->_petsc, &M, &N, &mat);
PetscBool assemble = PETSC_TRUE;
for (PetscInt i = 0; i < M && assemble; ++i) {
for (PetscInt j = 0; j < N && assemble; ++j) {
if (mat[i][j]) {
PetscBool assembled;
MatAssembled(mat[i][j], &assembled);
if (!assembled) assemble = PETSC_FALSE;
}
}
}
if (assemble) {
MatAssemblyBegin(ptParent->_petsc, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(ptParent->_petsc, MAT_FINAL_ASSEMBLY);
}
}
}
if (ptA->_ksp) {
KSPSetOperators(ptA->_ksp, ptA->_petsc, ptA->_petsc);
if (std::is_same< Type, Dmat >::value && ptA->_vS && !ptA->_vS->empty( )) {
KSPSetFromOptions(ptA->_ksp);
PetscBool assembled;
MatAssembled(ptA->_petsc, &assembled);
if (assembled) {
if (ptParent) MatAssembled(ptParent->_petsc, &assembled);
if (assembled) KSPSetUp(ptA->_ksp);
}
PC pc;
KSPGetPC(ptA->_ksp, &pc);
PCType type;
PCGetType(pc, &type);
PetscBool isFieldSplit;
PetscStrcmp(type, PCFIELDSPLIT, &isFieldSplit);
if (isFieldSplit) {
PetscInt nsplits;
KSP* subksp;
PCFieldSplitGetSubKSP(pc, &nsplits, &subksp);
for (int i = 0; i < nsplits; ++i) {
PC subpc;
KSPGetPC(subksp[i], &subpc);
PCGetType(subpc, &type);
PetscStrcmp(type, PCFIELDSPLIT, &isFieldSplit);
if (isFieldSplit) {
pc = subpc;
break;
}
}
setCompositePC(pc, ptA->_vS);
}
}
}
}
} else {
MatType type;
PetscBool isType = PETSC_FALSE;
if (ptB->_petsc) {
MatGetType(ptB->_petsc, &type);
PetscStrcmp(type, MATNEST, &isType);
}
if (!isType) {
Mat backup = ptA->_petsc;
ptA->_petsc = nullptr;
ptA->dtor( );
ptA->_petsc = backup;
PetscMPIInt flag;
if (ptA->_petsc)
MPI_Comm_compare(PetscObjectComm((PetscObject)ptA->_petsc),
PetscObjectComm((PetscObject)ptB->_petsc), &flag);
if (!ptA->_petsc || (flag != MPI_CONGRUENT && flag != MPI_IDENT)) {
MatDestroy(&ptA->_petsc);
if (ptB->_petsc) {
PetscBool assembled;
MatAssembled(ptB->_petsc, &assembled);
MatDuplicate(ptB->_petsc, assembled ? MAT_COPY_VALUES : MAT_DO_NOT_COPY_VALUES, &ptA->_petsc);
MatDestroy(&ptB->_petsc);
}
} else
MatHeaderReplace(ptA->_petsc, &ptB->_petsc);
KSPDestroy(&ptB->_ksp);
ptA->_A = ptB->_A;