ug4
Loading...
Searching...
No Matches
constrained_linear_iterator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 * Author: Markus Breit
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__LIB_ALGEBRA__OPERATOR__INTERFACE__CONSTRAINED_LINEAR_ITERATOR__
34#define __H__LIB_ALGEBRA__OPERATOR__INTERFACE__CONSTRAINED_LINEAR_ITERATOR__
35
40#include "preconditioner.h"
41
42#include <type_traits>
43
44namespace ug {
45
46
62template <typename TDomain, typename TAlgebra, typename TLinIt, typename = void>
63class ConstrainedLinearIterator : public TLinIt
64{
65 public:
67 typedef typename TAlgebra::vector_type vector_type;
68 typedef TLinIt base_type;
69
70 using TLinIt::init;
71
72 public:
77
82
88
91
92 protected:
94 virtual const char* name() const
95 {
96 return base_type::name();
97 }
98
100 {
101 return base_type::init(J, u);
102 }
103
105 {
106 return base_type::init(L);
107 }
108
109 virtual bool apply(vector_type& c, const vector_type& d)
110 {
111 // perform normal step
112 if (!base_type::apply(c, d)) return false;
113
114 // apply constraints on correction
115 if (!m_spDomDisc.valid())
116 UG_THROW("Domain discretization passed to ConstrainedLinearIterator is not valid.\n"
117 "Cannot apply any constraints.")
118
119 gf_type* gf = dynamic_cast<gf_type*>(&c);
120 if (gf && gf->dof_distribution().valid())
121 {
122 size_t nConstr = m_spDomDisc->num_constraints();
123
124 // first Dirichlet (hanging nodes might be constrained by Dirichlet nodes)
125 for (size_t i = 0; i < nConstr; i++)
126 if (m_spDomDisc->constraint(i)->type() & CT_DIRICHLET)
127 m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_DIRICHLET, m_time);
128
129 // then other interpolation
130 for (size_t i = 0; i < nConstr; i++)
131 if (m_spDomDisc->constraint(i)->type() & CT_CONSTRAINTS)
132 m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_CONSTRAINTS, m_time);
133
134 // then hanging nodes
135 for (size_t i = 0; i < nConstr; i++)
136 if (m_spDomDisc->constraint(i)->type() & CT_HANGING)
137 m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_HANGING, m_time);
138
139 // and Dirichlet again (Dirichlet nodes might also be hanging)
140 for (size_t i = 0; i < nConstr; i++)
141 if (m_spDomDisc->constraint(i)->type() & CT_DIRICHLET)
142 m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_DIRICHLET, m_time);
143 }
144 else UG_THROW("Vector is not a grid function. This is not supported.")
145
146 return true;
147 }
148
150 {
152 return impl(c, d);
153 }
154
155 private:
156 template <typename S, typename = void>//, typename otherDummy = void>
158 {
160
163
165 {
166 // FIXME: This implementation is not correct.
167 // As the defect is calculated from possibly erroneous corrections, there is no telling
168 // how to adjust the defect. Except for setting it to zero everywhere, which, by the way,
169 // is NOT what adjust_defect does, in general. We would need something like
170 // adjust_defect_linearSolver() here to distinguish between the non-linear defect and
171 // the residual in the linear solver.
172 // We CANNOT use our own apply() here and then update the defect ourselves as the
173 // ILinearIterator does not have an update method nor any way of applying the underlying
174 // linear operator.
175
176 // perform normal step
177 if (!cli.base_type::apply_update_defect(c, d)) return false;
178
179 // apply constraints on correction and defect
180 if (!cli.m_spDomDisc.valid())
181 UG_THROW("Domain discretization passed to ConstrainedLinearIterator is not valid.\n"
182 "Cannot apply any constraints.")
183
184 gf_type* gf = dynamic_cast<gf_type*>(&c);
185 if (gf && gf->dof_distribution().valid())
186 {
187 size_t nConstr = cli.m_spDomDisc->num_constraints();
188
189 // corrections
190 // first Dirichlet (hanging nodes might be constrained by Dirichlet nodes)
191 for (size_t i = 0; i < nConstr; i++)
192 if (cli.m_spDomDisc->constraint(i)->type() & CT_DIRICHLET)
193 cli.m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_DIRICHLET, cli.m_time);
194
195 // then other interpolation
196 for (size_t i = 0; i < nConstr; i++)
197 if (cli.m_spDomDisc->constraint(i)->type() & CT_CONSTRAINTS)
198 cli.m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_CONSTRAINTS, cli.m_time);
199
200 // then hanging nodes
201 for (size_t i = 0; i < nConstr; i++)
202 if (cli.m_spDomDisc->constraint(i)->type() & CT_HANGING)
203 cli.m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_HANGING, cli.m_time);
204
205 // and Dirichlet again (Dirichlet nodes might also be hanging)
206 for (size_t i = 0; i < nConstr; i++)
207 if (cli.m_spDomDisc->constraint(i)->type() & CT_DIRICHLET)
208 cli.m_spDomDisc->constraint(i)->adjust_correction(c, gf->dof_distribution(), CT_DIRICHLET, cli.m_time);
209
210 // residuals
211 // hanging nodes first
212 for (size_t i = 0; i < nConstr; i++)
213 if (cli.m_spDomDisc->constraint(i)->type() & CT_HANGING)
214 cli.m_spDomDisc->constraint(i)->adjust_linear_residual(d, c, gf->dof_distribution(), CT_HANGING, cli.m_time);
215
216 // then other constraints
217 for (size_t i = 0; i < nConstr; i++)
218 if (cli.m_spDomDisc->constraint(i)->type() & CT_CONSTRAINTS)
219 cli.m_spDomDisc->constraint(i)->adjust_linear_residual(d, c, gf->dof_distribution(), CT_CONSTRAINTS, cli.m_time);
220
221 // and Dirichlet last
222 for (size_t i = 0; i < nConstr; i++)
223 if (cli.m_spDomDisc->constraint(i)->type() & CT_DIRICHLET)
224 cli.m_spDomDisc->constraint(i)->adjust_linear_residual(d, c, gf->dof_distribution(), CT_DIRICHLET, cli.m_time);
225
226 }
227 else UG_THROW("Vector is not a grid function. This is not supported.")
228
229 return true;
230 }
231 };
232
233
234 // special implementation for IPreconditioner
235 template <typename S>
237 typename std::enable_if<std::is_base_of<IPreconditioner<TAlgebra>, S>::value>::type>
238 {
240
243
245 {
246 // apply precond and constraints
247 if (!cli.apply(c, d)) return false;
248
249 // calculate new defect from preconditioner defect operator
250 cli.m_spDefectOperator->apply_sub(d, c);
251
252 return true;
253 }
254 };
255
256 friend struct apply_update_defect_impl<TLinIt>;
257
258
259 public:
261 virtual bool supports_parallel() const
262 {
263 return base_type::supports_parallel();
264 }
265
267 void set_time(number time)
268 {
269 m_time = time;
270 };
271
272 protected:
276};
277
278
279
280
281} // end namespace ug
282
283
284#endif // __H__LIB_ALGEBRA__OPERATOR__INTERFACE__CONSTRAINED_LINEAR_ITERATOR__
Definition smart_pointer.h:107
Definition constrained_linear_iterator.h:64
virtual const char * name() const
returns the name of iterator
Definition constrained_linear_iterator.h:94
virtual bool init(SmartPtr< ILinearOperator< vector_type, vector_type > > J, const vector_type &u)
Definition constrained_linear_iterator.h:99
ConstrainedLinearIterator(const ConstrainedLinearIterator< TDomain, TAlgebra, TLinIt > &parent)
clone constructor
Definition constrained_linear_iterator.h:79
GridFunction< TDomain, TAlgebra > gf_type
Definition constrained_linear_iterator.h:66
virtual bool supports_parallel() const
returns if parallel solving is supported
Definition constrained_linear_iterator.h:261
virtual bool init(SmartPtr< ILinearOperator< vector_type, vector_type > > L)
Definition constrained_linear_iterator.h:104
SmartPtr< ILinearIterator< vector_type > > m_spLinIt
Definition constrained_linear_iterator.h:273
virtual SmartPtr< ILinearIterator< vector_type > > clone()
clone
Definition constrained_linear_iterator.h:84
TLinIt base_type
Definition constrained_linear_iterator.h:68
virtual bool apply(vector_type &c, const vector_type &d)
Definition constrained_linear_iterator.h:109
virtual ~ConstrainedLinearIterator()
destructor
Definition constrained_linear_iterator.h:90
TAlgebra::vector_type vector_type
Definition constrained_linear_iterator.h:67
number m_time
Definition constrained_linear_iterator.h:275
SmartPtr< IDomainDiscretization< TAlgebra > > m_spDomDisc
Definition constrained_linear_iterator.h:274
virtual bool apply_update_defect(vector_type &c, vector_type &d)
Definition constrained_linear_iterator.h:149
void set_time(number time)
setter for time
Definition constrained_linear_iterator.h:267
ConstrainedLinearIterator(SmartPtr< IDomainDiscretization< TAlgebra > > domDisc)
constructor
Definition constrained_linear_iterator.h:74
represents numerical solutions on a grid using an algebraic vector
Definition grid_function.h:121
Interface for domain discretization.
Definition domain_disc_interface.h:136
describes a linear mapping X->Y
Definition linear_operator.h:80
#define UG_THROW(msg)
Definition error.h:57
double number
Definition types.h:124
Definition smart_pointer.h:817
the ug namespace
@ CT_DIRICHLET
Definition ass_tuner.h:59
@ CT_HANGING
Definition ass_tuner.h:57
@ CT_CONSTRAINTS
Definition ass_tuner.h:58
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition smart_pointer.h:839
Definition constrained_linear_iterator.h:158
ConstrainedLinearIterator & cli
Definition constrained_linear_iterator.h:159
apply_update_defect_impl(ConstrainedLinearIterator &_cli)
Definition constrained_linear_iterator.h:161
bool operator()(vector_type &c, vector_type &d)
Definition constrained_linear_iterator.h:164