void Foam::GAMGPreconditioner::precondition
(
    solveScalarField& wA,
    const solveScalarField& rA_ss,
    const direction cmpt
) const
{
    wA = 0.0;
    solveScalarField AwA(wA.size());
    solveScalarField finestCorrection(wA.size());
    solveScalarField finestResidual(rA_ss);

    // Create coarse grid correction fields
    PtrList<solveScalarField> coarseCorrFields;

    // Create coarse grid sources
    PtrList<solveScalarField> coarseSources;

    // Create the smoothers for all levels
    PtrList<lduMatrix::smoother> smoothers;

    // Scratch fields if processor-agglomerated coarse level meshes
    // are bigger than original. Usually not needed
    solveScalarField ApsiScratch;
    solveScalarField finestCorrectionScratch;

    // Initialise the above data structures
    initVcycle
    (
        coarseCorrFields,
        coarseSources,
        smoothers,
        ApsiScratch,
        finestCorrectionScratch
    );


    scalarField rA_s;

    for (label cycle=0; cycle<nVcycles_; cycle++)
    {
        const scalarField& rA =
            ConstFieldWrapper<scalar, solveScalar>::get(rA_ss, rA_s);

        Vcycle
        (
            smoothers,
            wA,
            rA,
            AwA,
            finestCorrection,
            finestResidual,

            (ApsiScratch.size() ? ApsiScratch : AwA),
            (
                finestCorrectionScratch.size()
              ? finestCorrectionScratch
              : finestCorrection
            ),

            coarseCorrFields,
            coarseSources,
            cmpt
        );

        if (cycle < nVcycles_-1)
        {
            // Calculate finest level residual field
            matrix_.Amul(AwA, wA, interfaceBouCoeffs_, interfaces_, cmpt);
            finestResidual = rA_ss;
            finestResidual -= AwA;
        }
    }
}
Exemplo n.º 2
0
void LinearImplicitSystem::solve() {

  clock_t start_mg_time = clock();

  bool isThisFullCycle;
  unsigned grid0;

  if(_mg_type == F_CYCLE) {
    isThisFullCycle = 1;
    grid0 = 1;
  }
  else if(_mg_type == V_CYCLE){
    isThisFullCycle = 0;
    grid0 = _gridn;
  }
  else if(_mg_type == M_CYCLE){
    isThisFullCycle = 0;
    grid0 = _gridr;
  }
  else{
    std::cout << "wrong mg_type for this solver "<<std::endl;
    abort();
  }

  unsigned AMR_counter=0;

  for ( unsigned igridn = grid0; igridn <= _gridn; igridn++) {   //_igridn

    std::cout << std::endl << " ************* Level : " << igridn -1 << " *************\n" << std::endl;

    bool ThisIsAMR = (_mg_type == F_CYCLE && _AMRtest &&  AMR_counter<_maxAMRlevels && igridn==_gridn)?1:0;
    if(ThisIsAMR) _solution[igridn-1]->InitAMREps();

    Vcycle(igridn, isThisFullCycle );

    // ==============  AMR ==============
    if(ThisIsAMR){
      bool conv_test=0;
      if(_AMRnorm==0){
	conv_test=_solution[_gridn-1]->FlagAMRRegionBasedOnl2(_SolSystemPdeIndex,_AMRthreshold);
      }
      else if (_AMRnorm==1){
	conv_test=_solution[_gridn-1]->FlagAMRRegionBasedOnSemiNorm(_SolSystemPdeIndex,_AMRthreshold);
      }
      if(conv_test==0){
	_ml_msh->AddAMRMeshLevel();
	_ml_sol->AddSolutionLevel();
	AddSystemLevel();
	AMR_counter++;
      }
      else{
	_maxAMRlevels=AMR_counter;
	std::cout<<"The AMR solver has converged after "<<AMR_counter<<" refinements.\n";
      }
    }

    // ==============  Solution Prolongation ==============
    if (igridn < _gridn) {
      ProlongatorSol(igridn);
    }

  }

  std::cout << "\t     SOLVER TIME:\t       " << std::setw(11) << std::setprecision(6) << std::fixed
  <<static_cast<double>((clock()-start_mg_time))/CLOCKS_PER_SEC << std::endl;

}
Exemplo n.º 3
0
Foam::solverPerformance Foam::GAMGSolver::solve
(
    scalargpuField& psi,
    const scalargpuField& source,
    const direction cmpt
) const
{
    // Setup class containing solver performance data
    solverPerformance solverPerf(typeName, fieldName_);

    // Calculate A.psi used to calculate the initial residual
    scalargpuField Apsi(psi.size());
    matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);

    // Create the storage for the finestCorrection which may be used as a
    // temporary in normFactor
    scalargpuField finestCorrection(psi.size());

    // Calculate normalisation factor
    scalar normFactor = this->normFactor(psi, source, Apsi, finestCorrection);

    if (debug >= 2)
    {
        Pout<< "   Normalisation factor = " << normFactor << endl;
    }

    // Calculate initial finest-grid residual field
    scalargpuField finestResidual(source - Apsi);

    // Calculate normalised residual for convergence test
    solverPerf.initialResidual() = gSumMag
                                   (
                                       finestResidual,
                                       matrix().mesh().comm()
                                   )/normFactor;
    solverPerf.finalResidual() = solverPerf.initialResidual();


    // Check convergence, solve if not converged
    if
    (
        minIter_ > 0
        || !solverPerf.checkConvergence(tolerance_, relTol_)
    )
    {
        // Create coarse grid correction fields
        PtrList<scalargpuField> coarseCorrFields;

        // Create coarse grid sources
        PtrList<scalargpuField> coarseSources;

        // Create the smoothers for all levels
        PtrList<lduMatrix::smoother> smoothers;

        // Scratch fields if processor-agglomerated coarse level meshes
        // are bigger than original. Usually not needed
        scalargpuField scratch1;
        scalargpuField scratch2;

        // Initialise the above data structures
        initVcycle
        (
            coarseCorrFields,
            coarseSources,
            smoothers,
            scratch1,
            scratch2
        );

        do
        {
            Vcycle
            (
                smoothers,
                psi,
                source,
                Apsi,
                finestCorrection,
                finestResidual,

                (scratch1.size() ? scratch1 : Apsi),
                (scratch2.size() ? scratch2 : finestCorrection),

                coarseCorrFields,
                coarseSources,
                cmpt
            );

            // Calculate finest level residual field
            matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
            finestResidual = source;
            finestResidual -= Apsi;

            solverPerf.finalResidual() = gSumMag
                                         (
                                             finestResidual,
                                             matrix().mesh().comm()
                                         )/normFactor;

            if (debug >= 2)
            {
                solverPerf.print(Info.masterStream(matrix().mesh().comm()));
            }
        } while
        (
            (
                ++solverPerf.nIterations() < maxIter_
                && !solverPerf.checkConvergence(tolerance_, relTol_)
            )
            || solverPerf.nIterations() < minIter_
        );
    }

    return solverPerf;
}
Exemplo n.º 4
0
void NonLinearImplicitSystem::solve() {

  clock_t start_mg_time = clock();

  bool full_cycle;
  unsigned igrid0;

  if(_mg_type == F_CYCLE) {
    std::cout<< std::endl<<" *** Start MultiLevel Full-Cycle ***" << std::endl;
    full_cycle=1;
    igrid0=1;
  }
  else if(_mg_type == V_CYCLE){
    std::cout<< std::endl<<" *** Start MultiLevel V-Cycle ***" << std::endl;
    full_cycle=0;
    igrid0=_gridn;
  }
  else {
    std::cout<< std::endl<<" *** Start MultiLevel AMR-Cycle ***" << std::endl;
    full_cycle=0;
    igrid0=_gridr;
  }

  unsigned AMR_counter=0;

  for ( unsigned igridn=igrid0; igridn <= _gridn; igridn++) {   //_igridn

    std::cout << std::endl << " ****** Start Level Max " << igridn << " ******" << std::endl;
    clock_t start_nl_time = clock();

    bool ThisIsAMR = (_mg_type == F_CYCLE && _AMRtest &&  AMR_counter<_maxAMRlevels && igridn==_gridn)?1:0;
    if(ThisIsAMR) _solution[igridn-1]->InitAMREps();


    for ( unsigned nonLinearIterator = 0; nonLinearIterator < _n_max_nonlinear_iterations; nonLinearIterator++ ) { //non linear cycle
      std::cout << std::endl << " ********* Nonlinear iteration " << nonLinearIterator + 1 << " *********" << std::endl;

      Vcycle(igridn, full_cycle, nonLinearIterator );

      // ============== Test for non-linear Convergence ==============
      bool isnonlinearconverged = IsNonLinearConverged(igridn-1);
      if (isnonlinearconverged)
	nonLinearIterator = _n_max_nonlinear_iterations+1;
    }

    if(ThisIsAMR){
      bool conv_test=0;
      if(_AMRnorm==0){
	conv_test=_solution[_gridn-1]->FlagAMRRegionBasedOnl2(_SolSystemPdeIndex,_AMRthreshold);
      }
      else if (_AMRnorm==1){
	conv_test=_solution[_gridn-1]->FlagAMRRegionBasedOnSemiNorm(_SolSystemPdeIndex,_AMRthreshold);
      }
      if(conv_test==0){
	_ml_msh->AddAMRMeshLevel();
	_ml_sol->AddSolutionLevel();
	AddSystemLevel();
	AMR_counter++;
      }
      else{
	_maxAMRlevels=AMR_counter;
	std::cout<<"The AMR solver has converged after "<<AMR_counter<<" refinements.\n";
      }
    }

    if (igridn < _gridn) {
      ProlongatorSol(igridn);
    }

    std::cout << std::endl << " ****** Nonlinear-Cycle TIME: " << std::setw(11) << std::setprecision(6) << std::fixed
    <<static_cast<double>((clock()-start_nl_time))/CLOCKS_PER_SEC << std::endl;

    std::cout << std::endl << " ****** End Level Max "<< igridn << " ******" << std::endl;


  }

  std::cout << std::endl << " *** MultiGrid TIME: " << std::setw(11) << std::setprecision(6) << std::fixed
  <<static_cast<double>((clock()-start_mg_time))/CLOCKS_PER_SEC << std::endl;


}