-
Notifications
You must be signed in to change notification settings - Fork 0
/
step-12-dual_error.cc
executable file
·1475 lines (1351 loc) · 65.9 KB
/
step-12-dual_error.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ---------------------------------------------------------------------
*
* Copyright (C) 2009 - 2015 by the deal.II authors
*
* This file is part of the deal.II library.
*
* The deal.II library is free software; you can use it, 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.
* The full text of the license can be found in the file LICENSE at
* the top level of the deal.II distribution.
*
* ---------------------------------------------------------------------
//modified by zeng @Beihang University on 2018/07
*
* Author: Guido Kanschat, Texas A&M University, 2009
*/
// The first few files have already been covered in previous examples and will
// thus not be further commented on:
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/function.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/grid_out.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/numerics/data_out.h>
#include <deal.II/fe/mapping_q1.h>
#include <deal.II/fe/fe_tools.h>
// Here the discontinuous finite elements are defined. They are used in the
// same way as all other finite elements, though -- as you have seen in
// previous tutorial programs -- there isn't much user interaction with finite
// element classes at all: they are passed to <code>DoFHandler</code> and
// <code>FEValues</code> objects, and that is about it.
#include <deal.II/fe/fe_dgq.h>
// We are going to use the simplest possible solver, called Richardson
// iteration, that represents a simple defect correction. This, in combination
// with a block SSOR preconditioner (defined in precondition_block.h), that
// uses the special block matrix structure of system matrices arising from DG
// discretizations.
#include <deal.II/lac/solver_richardson.h>
#include <deal.II/lac/precondition_block.h>
// We are going to use gradients as refinement indicator.
#include <deal.II/numerics/derivative_approximation.h>
#include <deal.II/numerics/error_estimator.h>
// Here come the new include files for using the MeshWorker framework. The
// first contains the class MeshWorker::DoFInfo, which provides local
// integrators with a mapping between local and global degrees of freedom. It
// stores the results of local integrals as well in its base class
// Meshworker::LocalResults. In the second of these files, we find an object
// of type MeshWorker::IntegrationInfo, which is mostly a wrapper around a
// group of FEValues objects. The file <tt>meshworker/simple.h</tt> contains
// classes assembling locally integrated data into a global system containing
// only a single matrix. Finally, we will need the file that runs the loop
// over all mesh cells and faces.
#include <deal.II/meshworker/dof_info.h>
#include <deal.II/meshworker/integration_info.h>
#include <deal.II/meshworker/simple.h>
#include <deal.II/meshworker/loop.h>
// Like in all programs, we finish this section by including the needed C++
// headers and declaring we want to use objects in the dealii namespace
// without prefix.
#include <iostream>
#include <fstream>
#include <iomanip>
namespace Step12
{
using namespace dealii;
namespace PrimalSolverData //contain the b.cs and rhs for the primal solver
{
template <int dim>
class BoundaryValues: public Function<dim>
{
public:
BoundaryValues () {};
virtual void value_list (const std::vector<Point<dim> > &points,
std::vector<double> &values,
const unsigned int component=0) const;
};
template <int dim>
void BoundaryValues<dim>::value_list(const std::vector<Point<dim> > &points,
std::vector<double> &values,
const unsigned int) const
{
Assert(values.size()==points.size(),
ExcDimensionMismatch(values.size(),points.size()));
for (unsigned int i=0; i<values.size(); ++i)
{
if (points[i](0)>=0.125 && points[i](0)<=0.75)
values[i]=1.;
else
values[i]=0.;
}
}
template<int dim>
class RightHandSide: public Function<dim>
{
public:
RightHandSide():Function<dim>(){};
virtual double value (const Point<dim>& p,
const unsigned int component=0) const;
};
template<int dim>
double RightHandSide<dim>::value(const Point<dim>& p,
const unsigned int /*component*/) const
{
double return_value = 0.;
//for (unsigned int i=0;i<dim;++i)
// return_value += std::pow(p(i),2.); //rhs function is: f=x^2+y^2
return return_value; //the equation has no rhs.
}
} //end of the PrimalSolverData namespace
namespace AdvectionSolver
{
template<int dim>
class Base
{
public:
Base();
virtual ~Base();
virtual void initialize_problem()=0;
virtual void solve_problem()=0;
virtual void output_results(unsigned int cycle) const=0;
virtual void refine_grid()=0;
virtual unsigned int n_dofs() const=0;
virtual unsigned int n_active_cells() const=0;
protected:
Triangulation<dim> triangulation;
};
template<int dim>
Base<dim>::Base()
{}
template<int dim>
Base<dim>::~Base()
{}
template<int dim>
class Solver:public virtual Base<dim>
{
public:
Solver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature);
virtual ~Solver();
virtual void solve_problem();
virtual unsigned int n_dofs() const;
virtual unsigned int n_active_cells() const;
protected:
const MappingQ1<dim> mapping; //this const member must be initialized in the constructor
const FiniteElement<dim>& fe; //define a reference to the fe passed to Solver: FE_DGQ
const Quadrature<dim> quadrature;
const Quadrature<dim-1> face_quadrature;
DoFHandler<dim> dof_handler;
Vector<double> solution;
//these virtual functions inherited from the base class
void setup_system(); //can only be called in a public member function
virtual void assemble_system()=0; //remain to complete
typedef MeshWorker::DoFInfo<dim> DoFInfo;
typedef MeshWorker::IntegrationInfo<dim> CellInfo;
virtual void integrate_cell_term(DoFInfo& dinfo,CellInfo& info)=0;
virtual void integrate_boundary_term(DoFInfo& dinfo,CellInfo& info)=0;
virtual void integrate_face_term(DoFInfo& dinfo1,DoFInfo& dinfo2,CellInfo& info1,CellInfo& info2)=0;
struct LinearSystem{
LinearSystem();
void reinit(DoFHandler<dim>& dof_handler);
void solve(Vector<double>& solution, const FiniteElement<dim>& fe) const;
SparsityPattern sparsity_pattern;
SparseMatrix<double> system_matrix;
Vector<double> right_hand_side;
};
LinearSystem linear_system; //need default constructor, similar to dof_handler
};
//constructor of Solver
template<int dim>
Solver<dim>::Solver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature)
:
Base<dim>(),
mapping(),
fe(fe), //here (fe) should be a dg_fe.
quadrature(quadrature),
face_quadrature(face_quadrature),
dof_handler(Base<dim>::triangulation) //initialize the member objects.
{}
template<int dim>
Solver<dim>::~Solver(){
dof_handler.clear();
}
template<int dim>
Solver<dim>::LinearSystem::LinearSystem() //default constructor
{}
template<int dim>
void
Solver<dim>::LinearSystem::reinit(DoFHandler<dim>& dof_handler){
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_flux_sparsity_pattern (dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
system_matrix.reinit (sparsity_pattern);
right_hand_side.reinit (dof_handler.n_dofs());
}
//the system solver can be modified when needed.
template<int dim>
void
Solver<dim>::LinearSystem::solve(Vector<double>& solution, const FiniteElement<dim>& fe) const {
SolverControl solver_control(1000,1e-12);
SolverRichardson<> solver(solver_control);
PreconditionBlockSSOR<SparseMatrix<double>> preconditioner;
preconditioner.initialize(system_matrix,fe.dofs_per_cell);
solver.solve(system_matrix,solution,right_hand_side,preconditioner);
}
template<int dim>
void
Solver<dim>::setup_system(){
dof_handler.distribute_dofs(fe);
solution.reinit(dof_handler.n_dofs());
linear_system.reinit(dof_handler); //similar to dof_handler.distribute_dofs(fe)
}
template<int dim>
void
Solver<dim>::solve_problem(){ //this function will be called as "dg_method.solve_problem()"
setup_system();
assemble_system();
linear_system.solve(solution,fe);
}
template<int dim>
unsigned int
Solver<dim>::n_dofs()const{
return dof_handler.n_dofs();
}
template<int dim>
unsigned int
Solver<dim>::n_active_cells() const{
return Base<dim>::triangulation.n_active_cells();
}
//the primal solver
template<int dim>
class PrimalSolver:public Solver<dim>{
public:
PrimalSolver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature);
virtual void initialize_problem(); //inherite from the base class
virtual void output_results(unsigned int cycle) const;
virtual void refine_grid();
protected:
PrimalSolverData::RightHandSide<dim> rhs_function; //specified by user
PrimalSolverData::BoundaryValues<dim> boundary_function;
private:
virtual void assemble_system();
typedef MeshWorker::DoFInfo<dim> DoFInfo;
typedef MeshWorker::IntegrationInfo<dim> CellInfo;
virtual void integrate_cell_term(DoFInfo& dinfo,CellInfo& info);
virtual void integrate_boundary_term(DoFInfo& dinfo,CellInfo& info);
virtual void integrate_face_term(DoFInfo& dinfo1,DoFInfo& dinfo2,CellInfo& info1,CellInfo& info2);
};
template<int dim>
PrimalSolver<dim>::PrimalSolver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature)
:
Base<dim>(),
Solver<dim>(fe,quadrature,face_quadrature)
{}
template<int dim>
void
PrimalSolver<dim>::output_results(unsigned int cycle) const{
// Write the grid in eps format.
std::string filename = "grid-";
filename += ('0' + cycle);
Assert (cycle < 10, ExcInternalError());
filename += ".eps";
deallog << "Writing grid to <" << filename << ">" << std::endl;
std::ofstream eps_output (filename.c_str());
GridOut grid_out;
grid_out.write_eps (Base<dim>::triangulation, eps_output);
// Output of the solution in gnuplot format.
filename = "sol-";
filename += ('0' + cycle);
Assert (cycle < 10, ExcInternalError());
filename += ".gnuplot";
deallog << "Writing solution to <" << filename << ">" << std::endl;
std::ofstream gnuplot_output (filename.c_str());
DataOut<dim> data_out;
data_out.attach_dof_handler (Solver<dim>::dof_handler);
data_out.add_data_vector (Solver<dim>::solution, "u");
data_out.build_patches ();
data_out.write_gnuplot(gnuplot_output);
}
template<int dim>
void
PrimalSolver<dim>::refine_grid(){
Vector<float> gradient_indicator (Base<dim>::triangulation.n_active_cells());
// Now the approximate gradients are computed
DerivativeApproximation::approximate_gradient (Solver<dim>::mapping,
Solver<dim>::dof_handler,
Solver<dim>::solution,
gradient_indicator);
// and they are cell-wise scaled by the factor $h^{1+d/2}$
typename DoFHandler<dim>::active_cell_iterator
cell = Solver<dim>::dof_handler.begin_active(),
endc = Solver<dim>::dof_handler.end();
for (unsigned int cell_no=0; cell!=endc; ++cell, ++cell_no)
gradient_indicator(cell_no)*=std::pow(cell->diameter(), 1+1.0*dim/2);
// Finally they serve as refinement indicator.
GridRefinement::refine_and_coarsen_fixed_number (Base<dim>::triangulation,
gradient_indicator,
0.3, 0.1);
Base<dim>::triangulation.execute_coarsening_and_refinement ();
}
template<int dim>
void
PrimalSolver<dim>::initialize_problem(){
const Point<dim> bottom_left = Point<dim>();
const Point<dim> upper_right = Point<dim>(2.,1.); //used to specify the domain
std::vector<unsigned int> repetitions; //used to subdivide the original domain
repetitions.push_back (8);
if (dim>=2)
repetitions.push_back (4);
//repetitions is a vector with 2 elements of value 8 and 4.
GridGenerator::subdivided_hyper_rectangle(Base<dim>::triangulation,repetitions,bottom_left,upper_right); //creat a rectangle triangulation, p1/p2 specify the domain
}
template<int dim>
void
PrimalSolver<dim>::assemble_system(){
MeshWorker::IntegrationInfoBox<dim> info_box;
const unsigned int n_gauss_points = Solver<dim>::quadrature.size();
info_box.initialize_gauss_quadrature(n_gauss_points,n_gauss_points,n_gauss_points);
info_box.initialize_update_flags();
UpdateFlags update_flags = update_quadrature_points | update_values | update_gradients;
info_box.add_update_flags(update_flags,true,true,true,true);
info_box.initialize(Solver<dim>::fe,Solver<dim>::mapping);
MeshWorker::DoFInfo<dim> dof_info(Solver<dim>::dof_handler);
MeshWorker::Assembler::SystemSimple<SparseMatrix<double>,Vector<double>> assembler;
assembler.initialize(Solver<dim>::linear_system.system_matrix,
Solver<dim>::linear_system.right_hand_side);
MeshWorker::loop<dim,dim,MeshWorker::DoFInfo<dim>,MeshWorker::IntegrationInfoBox<dim>>
(Solver<dim>::dof_handler.begin_active(),Solver<dim>::dof_handler.end(),
dof_info,info_box,
std::bind(&PrimalSolver<dim>::integrate_cell_term,this,std::placeholders::_1,std::placeholders::_2),
std::bind(&PrimalSolver<dim>::integrate_boundary_term,this,std::placeholders::_1,std::placeholders::_2),
std::bind(&PrimalSolver<dim>::integrate_face_term,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4),
assembler);
}
template<int dim>
void PrimalSolver<dim>::integrate_cell_term(DoFInfo &dinfo,CellInfo &info){
const FEValuesBase<dim> &fe_v = info.fe_values();
FullMatrix<double> &local_matrix = dinfo.matrix(0).matrix;
Vector<double> &local_vector=dinfo.vector(0).block(0); //I add this vector to store the rhs_function term
const std::vector<double> &JxW = fe_v.get_JxW_values ();
// With these objects, we continue local integration like always. First,
// we loop over the quadrature points and compute the advection vector in
// the current point.
for (unsigned int point=0; point<fe_v.n_quadrature_points; ++point)
{
Point<dim> beta;
if(fe_v.quadrature_point(point)(0)<1){
beta(0) = fe_v.quadrature_point(point)(1);
beta(1) = 1.-fe_v.quadrature_point(point)(0);
}
else{
beta(0) = 2.-fe_v.quadrature_point(point)(1);
beta(1) = fe_v.quadrature_point(point)(0)-1.;
}
beta /= beta.norm();
// We solve a homogeneous equation, thus no right hand side shows up
// in the cell term. What's left is integrating the matrix entries.
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
local_matrix(i,j) -= beta*fe_v.shape_grad(i,point)*
fe_v.shape_value(j,point) *
JxW[point];
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i) //this is the right_hand_side
local_vector(i) += fe_v.shape_value(i,point)*
rhs_function.value(fe_v.quadrature_point(point))*
JxW[point]; //return position of the point-th point in real space.
}
}
template <int dim>
void PrimalSolver<dim>::integrate_boundary_term (DoFInfo &dinfo, CellInfo &info){
const FEValuesBase<dim> &fe_v = info.fe_values();
FullMatrix<double> &local_matrix = dinfo.matrix(0).matrix;
Vector<double> &local_vector = dinfo.vector(0).block(0);
const std::vector<double> &JxW = fe_v.get_JxW_values ();
const std::vector<Tensor<1,dim> > &normals = fe_v.get_all_normal_vectors ();
std::vector<double> g(fe_v.n_quadrature_points);
boundary_function.value_list (fe_v.get_quadrature_points(), g);
for (unsigned int point=0; point<fe_v.n_quadrature_points; ++point)
{
Point<dim> beta;
if(fe_v.quadrature_point(point)(0)<1){
beta(0) = fe_v.quadrature_point(point)(1);
beta(1) = 1.-fe_v.quadrature_point(point)(0);
}
else{
beta(0) = 2.-fe_v.quadrature_point(point)(1);
beta(1) = fe_v.quadrature_point(point)(0)-1.;
}
beta /= beta.norm();
const double beta_n=beta * normals[point];
if (beta_n>0)
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
local_matrix(i,j) += beta_n *
fe_v.shape_value(j,point) *
fe_v.shape_value(i,point) *
JxW[point];
else
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
local_vector(i) -= beta_n *
g[point] *
fe_v.shape_value(i,point) *
JxW[point];
}
}
template <int dim>
void PrimalSolver<dim>::integrate_face_term (DoFInfo &dinfo1,DoFInfo &dinfo2,
CellInfo &info1,CellInfo &info2)
{
const FEValuesBase<dim> &fe_v = info1.fe_values();
const FEValuesBase<dim> &fe_v_neighbor = info2.fe_values();
FullMatrix<double> &u1_v1_matrix = dinfo1.matrix(0,false).matrix;
FullMatrix<double> &u2_v1_matrix = dinfo1.matrix(0,true).matrix;
FullMatrix<double> &u1_v2_matrix = dinfo2.matrix(0,true).matrix;
FullMatrix<double> &u2_v2_matrix = dinfo2.matrix(0,false).matrix;
const std::vector<double> &JxW = fe_v.get_JxW_values ();
const std::vector<Tensor<1,dim> > &normals = fe_v.get_all_normal_vectors ();
for (unsigned int point=0; point<fe_v.n_quadrature_points; ++point)
{
Point<dim> beta;
if(fe_v.quadrature_point(point)(0)<1){
beta(0) = fe_v.quadrature_point(point)(1);
beta(1) = 1.-fe_v.quadrature_point(point)(0);
}
else{
beta(0) = 2.-fe_v.quadrature_point(point)(1);
beta(1) = fe_v.quadrature_point(point)(0)-1.;
}
beta /= beta.norm();
const double beta_n=beta * normals[point];
if (beta_n>0)
{
// This term we've already seen:
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
u1_v1_matrix(i,j) += beta_n *
fe_v.shape_value(j,point) *
fe_v.shape_value(i,point) *
JxW[point];
// We additionally assemble the term $(\beta\cdot n u,\hat
// v)_{\partial \kappa_+}$,
for (unsigned int k=0; k<fe_v_neighbor.dofs_per_cell; ++k)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
u1_v2_matrix(k,j) -= beta_n *
fe_v.shape_value(j,point) *
fe_v_neighbor.shape_value(k,point) *
JxW[point];
}
else
{
// This one we've already seen, too:
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int l=0; l<fe_v_neighbor.dofs_per_cell; ++l)
u2_v1_matrix(i,l) += beta_n *
fe_v_neighbor.shape_value(l,point) *
fe_v.shape_value(i,point) *
JxW[point];
// And this is another new one: $(\beta\cdot n \hat u,\hat
// v)_{\partial \kappa_-}$:
for (unsigned int k=0; k<fe_v_neighbor.dofs_per_cell; ++k)
for (unsigned int l=0; l<fe_v_neighbor.dofs_per_cell; ++l)
u2_v2_matrix(k,l) -= beta_n *
fe_v_neighbor.shape_value(l,point) *
fe_v_neighbor.shape_value(k,point) *
JxW[point];
}
}
}
} //the namespace AdvectionSolver end here
namespace DualSolverData //contain the b.cs and rhs of the dualsolver
{
template <int dim>
class BoundaryValues: public Function<dim>
{
public:
BoundaryValues () {};
virtual void value_list (const std::vector<Point<dim> > &points,
std::vector<double> &values,
const unsigned int component=0) const;
};
template <int dim>
void BoundaryValues<dim>::value_list(const std::vector<Point<dim> > &points,
std::vector<double> &values,
const unsigned int) const
{
Assert(values.size()==points.size(),
ExcDimensionMismatch(values.size(),points.size()));
for (unsigned int i=0; i<values.size(); ++i)
{
if (points[i](1)>=0.25 && points[i](1)<=1.)
values[i]=exp(pow(3./8,-2)-pow((points[i](1)-5./8)*(points[i](1)-5./8)-3./8,-2));
else
values[i]=0.;
}
}
template<int dim>
class RightHandSide: public Function<dim>
{
public:
RightHandSide():Function<dim>(){};
virtual double value (const Point<dim>& p,
const unsigned int component=0) const;
};
template<int dim>
double RightHandSide<dim>::value(const Point<dim>& p,
const unsigned int /*component*/) const
{
double return_value = 0.;
//for (unsigned int i=0;i<dim;++i)
// return_value += std::pow(p(i),2.); //rhs function is: f=x^2+y^2
return return_value; //the equation has no rhs.
}
} //end of the DualSolverData namespace
//Dual Solver
namespace AdvectionSolver
{
template <int dim>
class DualSolver:public Solver<dim>{
public:
DualSolver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature);
virtual void initialize_problem(); //inherite from the base class
//virtual void output_results(unsigned int cycle) const;
protected:
DualSolverData::RightHandSide<dim> rhs_function; //specified by user
DualSolverData::BoundaryValues<dim> boundary_function;
private:
virtual void assemble_system();
typedef MeshWorker::DoFInfo<dim> DoFInfo;
typedef MeshWorker::IntegrationInfo<dim> CellInfo;
virtual void integrate_cell_term(DoFInfo& dinfo,CellInfo& info);
virtual void integrate_boundary_term(DoFInfo& dinfo,CellInfo& info);
virtual void integrate_face_term(DoFInfo& dinfo1,DoFInfo& dinfo2,CellInfo& info1,CellInfo& info2);
};
template<int dim>
DualSolver<dim>::DualSolver(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature)
:
Base<dim>(),
Solver<dim>(fe,quadrature,face_quadrature)
{}
template<int dim>
void
DualSolver<dim>::initialize_problem(){
//do nothing in this class
}
template<int dim>
void
DualSolver<dim>::assemble_system(){
MeshWorker::IntegrationInfoBox<dim> info_box; //provide the FeValues needed to do integration
const unsigned int n_gauss_points = Solver<dim>::quadrature.size();
info_box.initialize_gauss_quadrature(n_gauss_points,n_gauss_points,n_gauss_points);
info_box.initialize_update_flags();
UpdateFlags update_flags = update_quadrature_points | update_values | update_gradients;
info_box.add_update_flags(update_flags,true,true,true,true);
info_box.initialize(Solver<dim>::fe,Solver<dim>::mapping); //till now the info_box is ready
MeshWorker::DoFInfo<dim> dof_info(Solver<dim>::dof_handler); //used to store the results
MeshWorker::Assembler::SystemSimple<SparseMatrix<double>,Vector<double>> assembler;
assembler.initialize(Solver<dim>::linear_system.system_matrix,
Solver<dim>::linear_system.right_hand_side);
MeshWorker::loop<dim,dim,MeshWorker::DoFInfo<dim>,MeshWorker::IntegrationInfoBox<dim>>
(Solver<dim>::dof_handler.begin_active(),Solver<dim>::dof_handler.end(),
dof_info,info_box,
std::bind(&DualSolver<dim>::integrate_cell_term,this,std::placeholders::_1,std::placeholders::_2),
std::bind(&DualSolver<dim>::integrate_boundary_term,this,std::placeholders::_1,std::placeholders::_2),
std::bind(&DualSolver<dim>::integrate_face_term,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4),
assembler);
}
template<int dim>
void
DualSolver<dim>::integrate_cell_term(DoFInfo& dinfo,CellInfo& info)
{
const FEValuesBase<dim>& fe_v = info.fe_values();
FullMatrix<double>& local_matrix = dinfo.matrix(0).matrix;
const std::vector<double>& JxW = fe_v.get_JxW_values();
for(unsigned int point=0;point<fe_v.n_quadrature_points;++point){
Point<dim> alpha; //alpha is opposite to beta of the primal problem
if(fe_v.quadrature_point(point)(0)<1){
alpha(0) = -fe_v.quadrature_point(point)(1);
alpha(1) = -(1.-fe_v.quadrature_point(point)(0));
}
else{
alpha(0) = -(2.-fe_v.quadrature_point(point)(1));
alpha(1) = -(fe_v.quadrature_point(point)(0)-1.);
}
alpha /= alpha.norm();
for(unsigned int i=0;i<fe_v.dofs_per_cell;++i)
for(unsigned int j=0;j<fe_v.dofs_per_cell;++j)
local_matrix(i,j)-=alpha*fe_v.shape_grad(i,point)*fe_v.shape_value(j,point)*JxW[point];
}
}
template<int dim>
void
DualSolver<dim>::integrate_boundary_term(DoFInfo& dinfo,CellInfo& info){
const FEValuesBase<dim> &fe_v = info.fe_values();
FullMatrix<double> &local_matrix = dinfo.matrix(0).matrix;
Vector<double> &local_vector = dinfo.vector(0).block(0);
const std::vector<double> &JxW = fe_v.get_JxW_values ();
const std::vector<Tensor<1,dim> > &normals = fe_v.get_all_normal_vectors ();
std::vector<double> psi(fe_v.n_quadrature_points); //use psi to store the b.c of the dual prob
boundary_function.value_list (fe_v.get_quadrature_points(), psi);
for (unsigned int point=0; point<fe_v.n_quadrature_points; ++point)
{
Point<dim> alpha;
if(fe_v.quadrature_point(point)(0)<1){
alpha(0) = -fe_v.quadrature_point(point)(1);
alpha(1) = -(1.-fe_v.quadrature_point(point)(0));
}
else{
alpha(0) = -(2.-fe_v.quadrature_point(point)(1));
alpha(1) = -(fe_v.quadrature_point(point)(0)-1.);
}
alpha /= alpha.norm();
const double alpha_n=alpha * normals[point];
if (alpha_n>0)
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
local_matrix(i,j) += alpha_n *
fe_v.shape_value(j,point) *
fe_v.shape_value(i,point) *
JxW[point];
else
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
local_vector(i) -= -psi[point] * //here alpha_n*g=psi
fe_v.shape_value(i,point) *
JxW[point];
}
}
template<int dim>
void
DualSolver<dim>::integrate_face_term(DoFInfo& dinfo1,DoFInfo& dinfo2,
CellInfo& info1,CellInfo& info2)
{
const FEValuesBase<dim> &fe_v = info1.fe_values();
const FEValuesBase<dim> &fe_v_neighbor = info2.fe_values();
FullMatrix<double> &z1_w1_matrix = dinfo1.matrix(0,false).matrix;
FullMatrix<double> &z2_w1_matrix = dinfo1.matrix(0,true).matrix;
FullMatrix<double> &z1_w2_matrix = dinfo2.matrix(0,true).matrix;
FullMatrix<double> &z2_w2_matrix = dinfo2.matrix(0,false).matrix;
const std::vector<double> &JxW = fe_v.get_JxW_values ();
const std::vector<Tensor<1,dim> > &normals = fe_v.get_all_normal_vectors ();
for (unsigned int point=0; point<fe_v.n_quadrature_points; ++point)
{
Point<dim> alpha;
if(fe_v.quadrature_point(point)(0)<1){
alpha(0) = -fe_v.quadrature_point(point)(1);
alpha(1) = -(1.-fe_v.quadrature_point(point)(0));
}
else{
alpha(0) = -(2.-fe_v.quadrature_point(point)(1));
alpha(1) = -(fe_v.quadrature_point(point)(0)-1.);
}
alpha /= alpha.norm();
const double alpha_n=alpha * normals[point];
if (alpha_n>0)
{
// This term we've already seen:
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
z1_w1_matrix(i,j) += alpha_n *
fe_v.shape_value(j,point) *
fe_v.shape_value(i,point) *
JxW[point];
// We additionally assemble the term $(\beta\cdot n u,\hat
// v)_{\partial \kappa_+}$,
for (unsigned int k=0; k<fe_v_neighbor.dofs_per_cell; ++k)
for (unsigned int j=0; j<fe_v.dofs_per_cell; ++j)
z1_w2_matrix(k,j) -= alpha_n *
fe_v.shape_value(j,point) *
fe_v_neighbor.shape_value(k,point) *
JxW[point];
}
else
{
// This one we've already seen, too:
for (unsigned int i=0; i<fe_v.dofs_per_cell; ++i)
for (unsigned int l=0; l<fe_v_neighbor.dofs_per_cell; ++l)
z2_w1_matrix(i,l) += alpha_n *
fe_v_neighbor.shape_value(l,point) *
fe_v.shape_value(i,point) *
JxW[point];
// And this is another new one: $(\beta\cdot n \hat u,\hat
// v)_{\partial \kappa_-}$:
for (unsigned int k=0; k<fe_v_neighbor.dofs_per_cell; ++k)
for (unsigned int l=0; l<fe_v_neighbor.dofs_per_cell; ++l)
z2_w2_matrix(k,l) -= alpha_n *
fe_v_neighbor.shape_value(l,point) *
fe_v_neighbor.shape_value(k,point) *
JxW[point];
}
}
}
enum RefinementCriterion
{
dual_weighted_error_estimator,
global_refinement,
kelly_indicator,
derivative
};
//the WeightedResidual class
template<int dim>
class WeightedResidual:public PrimalSolver<dim>,public DualSolver<dim>{
public:
WeightedResidual(const FiniteElement<dim>& primal_fe,
const FiniteElement<dim>& dual_fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature);
virtual void initialize_problem();
virtual void solve_problem();
virtual void output_results(unsigned int cycle);
virtual void refine_grid();
virtual unsigned int n_dofs() const;
virtual unsigned int n_active_cells() const;
double return_functional() const;
private:
std::clock_t start, timer;
typedef typename std::map<typename DoFHandler<dim>::face_iterator,double> FaceIntegrals;
typedef typename DoFHandler<dim>::active_cell_iterator active_cell_iterator;
struct CellData{
FEValues<dim> fe_values;
std::vector<double> cell_residual;
std::vector<double> rhs_values;
std::vector<double> dual_weights;
const SmartPointer<const Function<dim>> right_hand_side; //this pointer always point to the rhs function of the primal_solver
typename std::vector<Tensor<1,dim>> cell_grads; //tensor of rank 1 (i.e. a vector with dim components)
CellData(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Function<dim>& right_hand_side);
};
//store several data structure in face_data. here the fe_face_values_cell etc. are just interfaces used to reinit data on every cell. and face_values etc. store the "real" data needed for computation of face_term_error.
struct FaceData{
FEFaceValues<dim> fe_face_values_cell;
FEFaceValues<dim> fe_face_values_neighbor;
FESubfaceValues<dim> fe_subface_values_cell;
FESubfaceValues<dim> fe_subface_values_neighbor;
std::vector<double> face_residual;
std::vector<double> dual_weights;
std::vector<double> face_values;
std::vector<double> neighbor_values;
FaceData(const FiniteElement<dim>& fe,
const Quadrature<dim-1>& face_quadrature);
};
void estimate_error(Vector<float>& error_indicators) const;
void integrate_over_cell(const SynchronousIterators<std_cxx11::tuple<
active_cell_iterator,Vector<float>::iterator>>& cell_and_error,
CellData& cell_data,
const Vector<double>& primal_solution,
const Vector<double>& dual_weights) const;
void integrate_over_face(active_cell_iterator& cell,
const unsigned int face_no,
FaceData& face_data,
Vector<double>& primal_solution,
Vector<double>& dual_weights,
FaceIntegrals& face_integrals) const;
};
template<int dim>
WeightedResidual<dim>::WeightedResidual(const FiniteElement<dim>& primal_fe,
const FiniteElement<dim>& dual_fe,
const Quadrature<dim>& quadrature,
const Quadrature<dim-1>& face_quadrature)
:
Base<dim>(),
PrimalSolver<dim>(primal_fe,quadrature,face_quadrature),
DualSolver<dim>(dual_fe,quadrature,face_quadrature)
{}
//constructor of CellData
template<int dim>
WeightedResidual<dim>::CellData::
CellData(const FiniteElement<dim>& fe,
const Quadrature<dim>& quadrature,
const Function<dim>& right_hand_side):
fe_values(fe,quadrature,update_values|
update_gradients|
update_quadrature_points|
update_JxW_values),
cell_residual(quadrature.size()),
rhs_values(quadrature.size()),
dual_weights(quadrature.size()),
right_hand_side(&right_hand_side),
cell_grads(quadrature.size())
{}
//constructor of FaceData
template<int dim>
WeightedResidual<dim>::FaceData::
FaceData(const FiniteElement<dim>& fe,
const Quadrature<dim-1>& face_quadrature):
fe_face_values_cell(fe,face_quadrature,
update_values|
update_quadrature_points|
update_JxW_values|
update_normal_vectors),
fe_face_values_neighbor(fe,face_quadrature,
update_values|
update_quadrature_points),
fe_subface_values_cell(fe,face_quadrature,
update_values|
update_quadrature_points|
update_JxW_values|
update_normal_vectors),
fe_subface_values_neighbor(fe,face_quadrature,
update_values|
update_quadrature_points),
face_residual(face_quadrature.size()),
dual_weights(face_quadrature.size()),
face_values(face_quadrature.size()),
neighbor_values(face_quadrature.size())
{ }
template<int dim>
void
WeightedResidual<dim>::initialize_problem()
{
start = std::clock();
PrimalSolver<dim>::initialize_problem(); //only use PrimalSolver to edit the triangulation
}
template<int dim>
void
WeightedResidual<dim>::solve_problem(){
//
PrimalSolver<dim>::solve_problem();
DualSolver<dim>::solve_problem();
}
template<int dim>
void
WeightedResidual<dim>::output_results(unsigned int cycle){
// Write the grid in eps format.
std::string filename = "primal_grid-";
filename += ('0' + cycle);
Assert (cycle < 10, ExcInternalError());
filename += ".eps";
deallog << "Writing grid to <" << filename << ">" << std::endl;
std::ofstream eps_output (filename.c_str());
GridOut grid_out;
grid_out.write_eps (Base<dim>::triangulation, eps_output);
//interpolate the dual_solution to primal fe space
Vector<double> dual_solution(PrimalSolver<dim>::dof_handler.n_dofs());
FETools::interpolate(DualSolver<dim>::dof_handler,
DualSolver<dim>::solution,
PrimalSolver<dim>::dof_handler,
dual_solution);
// Output of the solution in gnuplot format.
filename = "sol-";
filename += ('0' + cycle);
Assert (cycle < 10, ExcInternalError());
filename += ".gnuplot";
deallog << "Writing solution to <" << filename << ">" << std::endl;
std::ofstream gnuplot_output (filename.c_str());
DataOut<dim> data_out;
data_out.attach_dof_handler (PrimalSolver<dim>::dof_handler);
data_out.add_data_vector (PrimalSolver<dim>::solution, "primal_solution");
data_out.add_data_vector (dual_solution,"dual_solution"); //dual_solutino needs to be interpolated firstly
data_out.build_patches ();
data_out.write_gnuplot(gnuplot_output);
//write out dofs----time----true error in the target functional successively to the same file
std::ofstream curve_graph("curve_graph.txt", std::ios::app);
timer = std::clock();
double current_time = (double)(timer-start)/CLOCKS_PER_SEC;
if(cycle == 0)
curve_graph<<"dofs"
<<std::setw(30)<<"J(e)"
<<std::setw(30)<<"time"<<std::endl;
else
curve_graph<<n_dofs()
<<std::setw(30)<<std::setprecision(15)<<(return_functional()-0.192808353547215)
<<std::setw(30)<<std::setprecision(15)<<current_time<<std::endl;
}
template<int dim>
void
WeightedResidual<dim>::refine_grid(){
Vector<float> error_indicators(PrimalSolver<dim>::n_active_cells());
//You have to choose here which refinement_criterion to use in this code, choices include:
/****************************
dual_weighted_error_estimator,
global_refinement,
kelly_indicator,
derivative
****************************/
const RefinementCriterion refinement_criterion = dual_weighted_error_estimator;
switch(refinement_criterion)
{
// use the dual weighted error estimator as indicator to execute the refinement process
case dual_weighted_error_estimator:
{
estimate_error(error_indicators);
for(Vector<float>::iterator i=error_indicators.begin();i!=error_indicators.end();++i)
*i = std::fabs(*i);
GridRefinement::refine_and_coarsen_fixed_number(this->triangulation,error_indicators,0.3,0.1);
this->triangulation.execute_coarsening_and_refinement();
break;
}
case global_refinement:
{
this->triangulation.refine_global(1);
break;
}