26 #include "NamespaceHeader.H" 106 for (
size_t i = 0; i < m_observers.size(); ++i)
116 virtual void createCoarser(T& a_coarse,
const T& a_fine,
bool ghosted) = 0;
125 virtual void relax(T& a_correction,
const T& a_residual,
int a_iterations) = 0 ;
133 virtual void restrictResidual(T& a_resCoarse, T& a_phiFine,
const T& a_rhsFine) = 0;
140 virtual void prolongIncrement(T& a_phiThisLevel,
const T& a_correctCoarse) = 0;
149 if (std::find(m_observers.begin(), m_observers.end(), a_observer) == m_observers.end())
151 m_observers.push_back(a_observer);
152 m_coarseningFactors.push_back(-1);
162 typename std::vector<MGLevelOpObserver<T>*>::iterator iter =
163 std::find(m_observers.begin(), m_observers.end(), a_observer);
164 if (iter != m_observers.end())
166 (*iter)->clearObservee();
167 m_observers.erase(iter);
169 size_t index = std::distance(m_observers.begin(), iter);
170 m_coarseningFactors.erase(m_coarseningFactors.begin() + index);
185 int a_coarseningFactor)
190 if (std::find(m_observers.begin(), m_observers.end(), a_operator) == m_observers.end())
193 addObserver(a_operator);
196 m_coarseningFactors.back() = a_coarseningFactor;
206 for (
int i = 0; i < m_observers.size(); ++i)
208 if (m_coarseningFactors[i] != -1)
218 m_observers[i]->operatorChanged(*
this);
231 int a_coarseningFactor)
238 return m_observers.size();
280 bool a_homoOnly =
true) = 0;
324 virtual void solve(T& a_phi,
const T& a_rhs,
Real a_tolerance,
int a_maxIterations,
int verbosity= 0);
334 virtual void oneCycle(T& a_e,
const T& a_res);
338 void init(
const T& a_correction,
const T& a_residual);
341 void cycle(
int a_depth, T& a_correction,
const T& a_residual);
356 int m_depth, m_defaultDepth,
m_pre, m_post, m_bottom, m_cycle, m_numMG;
448 if (a_finestOp == NULL)
450 nextOp = a_factory.
MGnewOp(a_domain, 0,
true);
458 while (nextOp != NULL)
465 if ((
m_depth < a_maxDepth) || (a_maxDepth < 0))
474 m_op.
back()->addCoarserObserver(nextOp, 2);
509 for (
int i = 2; i <
m_depth; i++)
522 this->
init(a_phi, a_rhs);
524 T correction, residual;
525 m_op[0]->create(correction, a_phi);
526 m_op[0]->create(residual, a_rhs);
527 m_op[0]->setToZero(a_phi);
528 m_op[0]->residual(residual, a_phi, a_rhs,
false);
530 Real errorno =
m_op[0]->norm(residual, 0);
533 pout() <<
"multigrid::solve initial residual = " << errorno << std::endl;
535 Real compval = a_tolerance*errorno;
536 Real epsilon = 1.0e-16;
537 compval =
Max(compval, epsilon);
538 Real error = errorno;
540 while ((error > compval) && (error > a_tolerance*errorno) && (iter < a_maxIterations))
542 m_op[0]->setToZero(correction);
543 m_op[0]->residual(residual, a_phi, a_rhs,
false);
544 error =
m_op[0]->norm(residual, 0);
547 pout() <<
"multigrid::solve iter = " << iter <<
", residual = " << error << std::endl;
550 this->
cycle(0, correction, residual);
551 m_op[0]->incr(a_phi, correction, 1.0);
557 pout() <<
"multigrid::solve final residual = " << error << std::endl;
569 CH_TIME(
"Multigrid::oneCycle");
574 cycle(0, a_e, a_residual);
578 T correction, residual;
579 m_op[0]->create(correction, a_e);
580 m_op[0]->create(residual, a_residual);
582 m_op[0]->residual(residual, a_e, a_residual);
584 m_op[0]->setToZero(correction);
585 this->
cycle(0, correction, residual);
587 m_op[0]->incr(a_e, correction, 1.0);
605 m_op[depth ]->relax(correction, residual, 1);
621 m_op[depth ]->restrictResidual(*(
m_residual[depth+1]), correction, residual);
629 m_op[depth ]->relax(correction, residual,
m_pre);
631 for (
int img = 0; img < cycles; img++)
633 m_op[depth ]->restrictResidual(*(
m_residual[depth+1]), correction, residual);
643 m_op[depth ]->relax(correction, residual,
m_post);
647 m_op[depth ]->relax( correction, residual,
m_pre );
649 m_op[depth ]->restrictResidual(*(
m_residual[depth+1]), correction, residual);
652 for (
int img = 0; img < cycles; img++)
658 m_op[depth ]->relax(correction, residual,
m_post);
668 std::vector<bool>::reverse_iterator it =
m_ownOp.rbegin();
669 for (
int i=
m_op.
size()-1; i>=0; --i, ++it)
671 if (*it)
delete m_op[i];
685 template <
typename T>
694 m_op->removeObserver(
this);
699 #include "NamespaceFooter.H" std::ostream & pout()
Use this in place of std::cout for program output.
void addObserver(MGLevelOpObserver< T > *a_observer)
Definition: MultiGrid.H:147
MGLevelOp()
Constructor.
Definition: MultiGrid.H:94
Definition: LinearSolver.H:28
void clear()
Definition: MultiGrid.H:666
int m_cycle
Definition: MultiGrid.H:356
#define CH_SPACEDIM
Definition: SPACE.H:52
#define CH_assert(cond)
Definition: CHArray.H:37
A class to facilitate interaction with physical boundary conditions.
Definition: ProblemDomain.H:130
Vector< T *> m_residual
Definition: MultiGrid.H:373
int m_defaultDepth
Definition: MultiGrid.H:356
MGLevelOpObserver & operator=(const MGLevelOpObserver &)
MGLevelOpObserver()
Base level Constructor. Called by all subclasses.
Definition: MultiGrid.H:41
Vector< MGLevelOp< T > *> getAllOperators()
Definition: MultiGrid.H:396
one dimensional dynamic array
Definition: Vector.H:52
void removeObserver(MGLevelOpObserver< T > *a_observer)
Definition: MultiGrid.H:160
void setObservee(MGLevelOp< T > *a_observee)
Definition: MultiGrid.H:60
virtual ~MGLevelOpFactory()
Destructor.
Definition: MultiGrid.H:269
void init(const T &a_correction, const T &a_residual)
Definition: MultiGrid.H:500
virtual void define(MGLevelOpFactory< T > &a_factory, LinearSolver< T > *a_bottomSolver, const ProblemDomain &a_domain, int a_maxDepth=-1, MGLevelOp< T > *a_finestLevelOp=NULL)
Function to define a MultiGrid object.
Definition: MultiGrid.H:427
Vector< MGLevelOp< T > * > m_op
Definition: MultiGrid.H:372
virtual void oneCycle(T &a_e, const T &a_res)
Execute ONE v-cycle of multigrid.
Definition: MultiGrid.H:567
bool m_homogeneous
Definition: MultiGrid.H:357
int m_bottomCells
Definition: MultiGrid.H:369
Definition: MultiGrid.H:36
std::vector< MGLevelOpObserver< T > * > m_observers
List of observers for the operator.
Definition: MultiGrid.H:244
MultiGrid()
Definition: MultiGrid.H:405
void cycle(int a_depth, T &a_correction, const T &a_residual)
Definition: MultiGrid.H:596
int m_post
Definition: MultiGrid.H:356
std::vector< bool > m_ownOp
Definition: MultiGrid.H:375
void setBottomSolver(LinearSolver< T > *a_bottomSolver)
Definition: MultiGrid.H:491
void resize(unsigned int isize)
Definition: Vector.H:323
ProblemDomain m_topLevelDomain
Definition: MultiGrid.H:371
void push_back(const T &in)
Definition: Vector.H:283
int m_depth
Definition: MultiGrid.H:356
void clear()
Definition: Vector.H:165
T & back()
Definition: Vector.H:256
#define CH_TIME(name)
Definition: CH_Timer.H:59
Definition: MultiGrid.H:259
bool m_defined
Definition: MultiGrid.H:367
double Real
Definition: REAL.H:33
Definition: MultiGrid.H:30
int numObservers() const
Returns the number of objects observing this operator.
Definition: MultiGrid.H:236
size_t size() const
Definition: Vector.H:177
int m_bottom
Definition: MultiGrid.H:356
MultiGrid(const MultiGrid< T > &a_opin)
Definition: MultiGrid.H:378
virtual void finerOperatorChanged(const MGLevelOp< T > &a_operator, int a_coarseningFactor)
Definition: MultiGrid.H:230
void clearObservee()
Definition: MultiGrid.H:68
static void Error(const char *const a_msg=m_nullString, int m_exitCode=CH_DEFAULT_ERROR_CODE)
Print out message to cerr and exit with the specified exit code.
void operator=(const MultiGrid< T > &a_opin)
Definition: MultiGrid.H:383
Definition: MultiGrid.H:296
Vector< T *> m_correction
Definition: MultiGrid.H:374
Definition: LinearSolver.H:158
virtual MGLevelOp< T > * MGnewOp(const ProblemDomain &a_FineindexSpace, int a_depth, bool a_homoOnly=true)=0
void addCoarserObserver(MGLevelOp< T > *a_operator, int a_coarseningFactor)
Definition: MultiGrid.H:184
virtual ~MultiGrid()
Definition: MultiGrid.H:419
MGLevelOpFactory()
Base class constructor.
Definition: MultiGrid.H:264
void notifyObserversOfChange()
This should be called whenever the operator's data is updated.
Definition: MultiGrid.H:201
T Max(const T &a_a, const T &a_b)
Definition: Misc.H:39
std::vector< int > m_coarseningFactors
Multigrid coarsening factors.
Definition: MultiGrid.H:247
virtual ~MGLevelOp()
Destructor.
Definition: MultiGrid.H:103
MGLevelOp< T > * m_op
Definition: MultiGrid.H:76
const Box & domainBox() const
Returns the logical computational domain.
Definition: ProblemDomain.H:868
virtual ~MGLevelOpObserver()
Destructor.
Definition: MultiGrid.H:686
virtual void operatorChanged(const MGLevelOp< T > &a_operator)
Definition: MultiGrid.H:53
int m_pre
Definition: MultiGrid.H:356
virtual void solve(T &a_phi, const T &a_rhs, Real a_tolerance, int a_maxIterations, int verbosity=0)
Definition: MultiGrid.H:519
LinearSolver< T > * m_bottomSolver
Definition: MultiGrid.H:358