KASKADE 7 development version
Loading...
Searching...
No Matches
uzawa-old.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2024 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef UZAWA_H
14#define UZAWA_H
15
16#include <boost/fusion/sequence.hpp>
17
18#include "dune/istl/preconditioners.hh"
19#include "dune/istl/solvers.hh"
20
21#include "fem/linearspace.hh"
22
23namespace Kaskade
24{
29 template <class X, class Y>
30 void applyPreconditioner(Dune::Preconditioner<X,Y>& p, X& x, Y& y) {
31 p.pre(x,y);
32 p.apply(x,y);
33 p.post(x);
34 }
35
49 template<class X, class Y>
51 : public Dune::InverseOperator<LinearProductSpace<typename X::field_type,boost::fusion::vector<X,Y>>,
52 LinearProductSpace<typename X::field_type,boost::fusion::vector<X,Y>>>
53 {
54 public:
57 typedef typename X::field_type field_type;
61
74 template<class A, class SolA, class B, class Bt, class PB>
75 UzawaSolver (A& opA_, SolA& solA_, B& opB_, Bt& opBt_, PB& pb_,
76 double reduction_, int maxit_, int verbose_)
77 : opA(opA_)
78 , solA(solA_)
79 , opB(opB_)
80 , opBt(opBt_)
81 , precB(pb_)
82 , reduction(reduction_)
83 , maxit(maxit_)
84 , verbose(verbose_)
85 {
86 }
87
88
89 virtual void apply (Domain& x, Range& b, Dune::InverseOperatorResult& res) {
90 using namespace boost::fusion;
91 using namespace Dune;
92
93
94 Timer watch; // start a timer
95
96 InverseOperatorResult resA;
97 if (verbose>0) printf("=== UzawaSolver\n");
98
99 // Declare variables
100 X& u(at_c<0>(x.data));
101 X& f(at_c<0>(b.data));
102 X r(u), h(u), rSave(u);
103 Y& lambda(at_c<1>(x.data));
104 Y g(at_c<1>(b.data)), d(g), p(g);
105 Y tmp(g); // WARNING: needed only for applying P twice!
106
107 // initialize
108 r = f;
109 opBt.applyscaleadd(-1,lambda,r); // r = f-B'*u
110 solA.apply(u,r,resA); // Au = r
111 opB.applyscaleadd(-1,u,g); // g = g-Bu
112 applyPreconditioner(precB,tmp,g);
113 applyPreconditioner(precB,d,tmp); // d = Pg // WARNING: assumes precB^2 is preco
114 double sigma = spY.dot(g,d); // sigma = g'd
115 double sigma0 = sigma;
116
117 //std::cout << "sigma = " << sigma << "\n g=" << at_c<0>(g.data) << "d=" << at_c<0>(d.data);
118
119 // Perform stationary iteration.
120 int i=0;
121 for ( ; i<maxit && sigma>reduction*reduction*sigma0; i++) {
122
123 // std::cout << "d=P(g-Bu)=" << at_c<0>(d.data);
124
125 opBt.apply(d,r); // r = B'd
126 rSave = r;
127 solA.apply(h,r,resA); // Ah = r
128 r = rSave;
129
130 double alpha = sigma / spX.dot(h,r); // alpha = sigma / h'r
131 lambda.axpy(-alpha,d); // lambda = lambda-alpha d
132 u.axpy(alpha,h); // u = u+alpha h
133 opB.applyscaleadd(-alpha,h,g); // g = g-alpha Bh
134
135 applyPreconditioner(precB,tmp,g); // p = Pg
136 applyPreconditioner(precB,p,tmp); // WARNING
137
138 double sigmaOld = sigma;
139 sigma = spY.dot(g,p);
140 double beta = sigma / sigmaOld;
141
142
143 d *= beta; d.axpy(1,p); // d = beta d + p
144
145
146 if (verbose>1) {
147 if (i%30==0)
148 std::printf("%5s %14s %14s\n","Iter","Preco. Res.","Rate");
149 std::printf("%5d %14.4e %14.4e\n",i,std::sqrt(sigma),std::sqrt(beta));
150 }
151 }
152
153 // Fill statistics
154 res.clear();
155 res.iterations = i;
156 res.reduction = std::sqrt(sigma/sigma0);
157 res.converged = res.reduction<=reduction;
158 res.conv_rate = std::pow(res.reduction,1.0/i);
159 res.elapsed = watch.elapsed();
160
161
162 if (verbose>0) // final print
163 printf("=== rate=%g, time=%g, time/it=%g, iter=%d\n",res.conv_rate,res.elapsed,res.elapsed/i,i);
164 }
165
166 virtual void apply (Domain& x, Range& b, double reduction_, Dune::InverseOperatorResult& res)
167 {
168 reduction = reduction_;
169 this->apply(x,b,res);
170 }
171
172 void apply(Domain& x, Range& b)
173 {
174 Dune::InverseOperatorResult tmpResult;
175 apply(x,b,tmpResult);
176 }
177
183 virtual Dune::SolverCategory::Category category() const override
184 {
185 return Dune::SolverCategory::sequential;
186 }
187
188 private:
189 Dune::SeqScalarProduct<X> spX;
190 Dune::SeqScalarProduct<Y> spY;
191
192 Dune::LinearOperator<X,X>& opA;
193 Dune::InverseOperator<X,X>& solA;
194
195 Dune::LinearOperator<X,Y>& opB;
196 Dune::LinearOperator<Y,X>& opBt;
197 Dune::Preconditioner<Y,Y>& precB;
198
199 double reduction;
200 int maxit;
201 int verbose;
202 };
203}
204
205#endif
LinearProductSpace< typename X::field_type, boost::fusion::vector< X, Y > > domain_type
Definition uzawa-old.hh:55
domain_type range_type
Definition uzawa-old.hh:56
void apply(Domain &x, Range &b)
Definition uzawa-old.hh:172
virtual Dune::SolverCategory::Category category() const override
returns the category of the operator
Definition uzawa-old.hh:183
virtual void apply(Domain &x, Range &b, double reduction_, Dune::InverseOperatorResult &res)
Definition uzawa-old.hh:166
X::field_type field_type
Definition uzawa-old.hh:57
virtual void apply(Domain &x, Range &b, Dune::InverseOperatorResult &res)
Definition uzawa-old.hh:89
domain_type Domain
Definition uzawa-old.hh:59
UzawaSolver(A &opA_, SolA &solA_, B &opB_, Bt &opBt_, PB &pb_, double reduction_, int maxit_, int verbose_)
Definition uzawa-old.hh:75
void applyPreconditioner(Dune::Preconditioner< X, Y > &p, X &x, Y &y)
Convenience function for simple application of preconditioners. Calls pre(), apply(),...
Definition uzawa-old.hh:30