Skip to content

Commit 36f3860

Browse files
committed
Update for nested iteration (space-time-adaptive prototype)
1 parent 794b4d0 commit 36f3860

File tree

3 files changed

+76
-35
lines changed

3 files changed

+76
-35
lines changed

ugbase/bridge/disc_bridges/algebra_bridge.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ static void DomainAlgebra(Registry& reg, string parentGroup)
549549
.add_method("enable_adaptive_refinement", &T::enable_adaptive_refinement)
550550
.add_method("disable_adaptive_refinement", &T::disable_adaptive_refinement)
551551
.add_method("config_string", &T::config_string)
552+
553+
.add_method("set_associated_space", &T::set_associated_space)
554+
.add_method("set_absolute_tolerance", &T::set_absolute_tolerance)
552555
.set_construct_as_smart_pointer(true);
553556
reg.add_class_to_group(name, "NestedIterationSolver", tag);
554557
}

ugbase/lib_disc/operator/linear_operator/nested_iteration/nested_iteration.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "lib_disc/assemble_interface.h"
4747

4848
#include "lib_disc/spatial_disc/domain_disc.h"
49+
#include "lib_disc/function_spaces/metric_spaces.h"
4950
#include "lib_disc/function_spaces/error_elem_marking_strategy.h"
5051

5152
#include "lib_algebra/operator/debug_writer.h"
@@ -138,7 +139,13 @@ class NestedIterationSolver
138139
//! set marking strategy
139140
void set_refinement_marking(SmartPtr<IElementMarkingStrategy<TDomain> > m) {m_spRefinementMarking =m; }
140141
void set_coarsening_marking(SmartPtr<IElementMarkingStrategy<TDomain> > m) {m_spCoarseningMarking =m; }
141-
void set_tolerance(number tol) {m_TOL = tol;}
142+
void set_tolerance(number tol)
143+
{
144+
m_TOL = tol;
145+
UG_LOG("NI::set_tolerance" << m_TOL <<std::endl);
146+
}
147+
148+
142149

143150
//! indicates, if grids should be refined further (if desired accuracy has not been achieved)
144151
bool use_adaptive_refinement() const {return m_bUseAdaptiveRefinement;}
@@ -150,6 +157,17 @@ class NestedIterationSolver
150157

151158
void set_max_steps(int steps) {m_maxSteps = steps;}
152159
number last_error() const {return m_lastError;}
160+
161+
void set_associated_space(SmartPtr<IGridFunctionSpace<grid_function_type> > spSpace)
162+
{ m_spAssociatedSpace = spSpace;}
163+
164+
void set_absolute_tolerance(number atol)
165+
{
166+
167+
m_absTOL = atol;
168+
UG_LOG("NI::set_absolute_tolerance" << m_absTOL <<std::endl);
169+
}
170+
153171
///@}
154172

155173
/// for debug output
@@ -159,7 +177,7 @@ class NestedIterationSolver
159177
{m_spElemError = spErrEta;}
160178

161179
protected:
162-
//number estimate_error(const grid_function_type& u);
180+
void estimate_and_mark_domain(const grid_function_type& u, SmartPtr<IElementMarkingStrategy<TDomain> > spMarking, bool bClearMarks = true);
163181
number refine_domain(const grid_function_type& u);
164182
number coarsen_domain(const grid_function_type& u);
165183

@@ -191,10 +209,18 @@ class NestedIterationSolver
191209
SmartPtr<IElementMarkingStrategy<TDomain> > m_spCoarseningMarking;
192210
bool m_bUseAdaptiveRefinement;
193211

194-
number m_TOL;
195212
int m_maxSteps;
196213
number m_lastError;
197214

215+
/// tolerance
216+
number m_TOL;
217+
218+
/// associated norm (for relative error)
219+
SmartPtr<IGridFunctionSpace<grid_function_type> > m_spAssociatedSpace;
220+
221+
/// absolute tolerance (for relative error)
222+
number m_absTOL;
223+
198224
/// (optional) debug info for adaptive refinement
199225
SmartPtr<grid_function_type> m_spElemError;
200226

ugbase/lib_disc/operator/linear_operator/nested_iteration/nested_iteration_impl.h

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ NestedIterationSolver(SmartPtr<ILinearOperatorInverse<vector_type> > LinearSolve
6262
m_spAss(NULL),
6363
m_dgbCall(0),
6464
m_lastNumSteps(0),
65-
m_bUseAdaptiveRefinement(true)
65+
m_bUseAdaptiveRefinement(true), m_TOL(1e-3), m_absTOL(1e-12), m_maxSteps(100)
6666
{};
6767

6868
template <typename TDomain, typename TAlgebra>
@@ -74,7 +74,8 @@ NestedIterationSolver() :
7474
m_spAss(NULL),
7575
m_dgbCall(0),
7676
m_lastNumSteps(0),
77-
m_bUseAdaptiveRefinement(true)
77+
m_bUseAdaptiveRefinement(true),
78+
m_TOL(1e-3), m_absTOL(1e-12), m_maxSteps(100)
7879
{};
7980

8081
template <typename TDomain, typename TAlgebra>
@@ -86,7 +87,7 @@ NestedIterationSolver(SmartPtr<IOperator<vector_type> > N) :
8687
m_spAss(NULL),
8788
m_dgbCall(0),
8889
m_lastNumSteps(0),
89-
m_bUseAdaptiveRefinement(true)
90+
m_bUseAdaptiveRefinement(true), m_TOL(1e-3), m_absTOL(1e-12), m_maxSteps(100)
9091
{
9192
init(N);
9293
};
@@ -102,7 +103,7 @@ NestedIterationSolver(SmartPtr<IAssemble<TAlgebra> > spAss) :
102103
m_dgbCall(0),
103104
m_lastNumSteps(0),
104105
m_baseLevel(0),
105-
m_bUseAdaptiveRefinement(true)
106+
m_bUseAdaptiveRefinement(true), m_TOL(1e-3), m_absTOL(1e-12), m_maxSteps(100)
106107
{
107108
m_N = SmartPtr<AssembledOperator<TAlgebra> >(new AssembledOperator<TAlgebra>(m_spAss));
108109
};
@@ -150,12 +151,12 @@ bool NestedIterationSolver<TDomain,TAlgebra>::prepare(vector_type& u)
150151
//! Refines domain and provides error estimate.
151152
/*! Values depend on m_spDomErr */
152153
template <typename TDomain, typename TAlgebra>
153-
number NestedIterationSolver<TDomain,TAlgebra>::refine_domain(const grid_function_type& u)
154+
void NestedIterationSolver<TDomain,TAlgebra>::estimate_and_mark_domain(const grid_function_type& u, SmartPtr<IElementMarkingStrategy<TDomain> > spMarking, bool bClearMarks)
154155
{
155156
UG_LOG("Computing error... "<< std::endl);
156157

157158
typedef DomainDiscretization<TDomain, TAlgebra> domain_disc_type;
158-
// typedef IDomainErrorIndicator<TAlgebra> domain_indicator_type;
159+
// typedef IDomainErrorIndicator<TAlgebra> domain_indicator_type;
159160

160161
SmartPtr<domain_disc_type> spDomainEstimator = m_spDomErr.template cast_dynamic<domain_disc_type>();
161162

@@ -165,19 +166,21 @@ number NestedIterationSolver<TDomain,TAlgebra>::refine_domain(const grid_functio
165166

166167
// compute error
167168
if (m_spElemError.valid()) {
169+
// debug version
168170
spDomainEstimator->calc_error(u, u.dd(), &(*m_spElemError));
169171
} else {
172+
// standard version
170173
spDomainEstimator->calc_error(u, u.dd());
171174
}
175+
172176
// set (new) marks
173-
m_spRefiner->clear_marks();
174-
spDomainEstimator->mark_with_strategy(*m_spRefiner, m_spRefinementMarking);
175-
UG_LOG("Estimated error: " << m_spRefinementMarking->global_estimated_error());
177+
if (bClearMarks) m_spRefiner->clear_marks();
178+
spDomainEstimator->mark_with_strategy(*m_spRefiner, spMarking);
179+
UG_LOG("Estimated error: " << spMarking->global_estimated_error());
180+
181+
176182

177-
const number err = m_spRefinementMarking->global_estimated_error();
178-
if(err >= m_TOL) {m_spRefiner->refine();}
179183

180-
return err;
181184
}
182185

183186

@@ -286,14 +289,14 @@ bool NestedIterationSolver<TDomain,TAlgebra>::apply(vector_type& u)
286289

287290
// Solve linearized system.
288291
try{
289-
NESTED_ITER_PROFILE_BEGIN(NewtonApplyLinSolver);
290-
if(!m_spLinearSolver->apply(u, *spD))
291-
{
292-
UG_LOG("ERROR in 'NestedIterationSolver::apply': Cannot apply Inverse Linear "
293-
"Operator for Jacobi-Operator.\n");
294-
return false;
295-
}
296-
NESTED_ITER_PROFILE_END();
292+
NESTED_ITER_PROFILE_BEGIN(NewtonApplyLinSolver);
293+
if(!m_spLinearSolver->apply(u, *spD))
294+
{
295+
UG_LOG("ERROR in 'NestedIterationSolver::apply': Cannot apply Inverse Linear "
296+
"Operator for Jacobi-Operator.\n");
297+
return false;
298+
}
299+
NESTED_ITER_PROFILE_END();
297300
}UG_CATCH_THROW("NestedIterationSolver::apply: Application of Linear Solver failed.");
298301

299302
// Adjust solution.
@@ -315,25 +318,34 @@ bool NestedIterationSolver<TDomain,TAlgebra>::apply(vector_type& u)
315318
break;
316319
}
317320

318-
// Refine domain and check error.
319-
number err = this->refine_domain(usol);
321+
// Estimate and mark domain.
322+
this->estimate_and_mark_domain(usol, m_spRefinementMarking);
320323

321-
if (m_spElemError.valid())
322-
{
323-
// write defect for debug
324-
std::string name("NESTED_ITER_Error");
325-
name.append(ext);
326-
VTKOutput<TDomain::dim> outError;
327-
outError.print(name.c_str(), *m_spElemError);
324+
// OPTIONAL: write defect for debug
325+
if (m_spElemError.valid())
326+
{
327+
std::string name("NESTED_ITER_Error");
328+
name.append(ext);
329+
VTKOutput<TDomain::dim> outError;
330+
outError.print(name.c_str(), *m_spElemError);
328331
}
329332

333+
// Check error.
334+
const number err = m_spRefinementMarking->global_estimated_error();
335+
double desiredTOL = (m_spAssociatedSpace.valid()) ? m_TOL*m_spAssociatedSpace->norm(usol) + m_absTOL : m_TOL;
336+
337+
UG_LOG("NI *** Desired tolerance: " << m_TOL << " * "<< m_spAssociatedSpace->norm(usol) << " + "<< m_absTOL <<std::endl);
338+
330339
// Quit or continue?
331-
if(err < m_TOL)
340+
if(err < desiredTOL)
332341
{
333-
UG_LOG("SUCCESS: Error smaller than tolerance: " << err << " < "<< m_TOL << std::endl);
342+
// Quit.
343+
UG_LOG("SUCCESS: Error smaller than tolerance: " << err << " < "<< desiredTOL << std::endl);
334344
break;
335345
} else {
336-
UG_LOG("FAILED: Error (still) greater than tolerance: " << err << " > "<< m_TOL << std::endl);
346+
// Refine and continue.
347+
UG_LOG("FAILED: Error (still) greater than tolerance: " << err << " > "<< desiredTOL << std::endl);
348+
m_spRefiner->refine();
337349
}
338350
}
339351

0 commit comments

Comments
 (0)