Liebe Gitlab-Nutzer, lieber Gitlab-Nutzer,
es ist nun möglich sich mittels des ZIH-Logins/LDAP an unserem Dienst anzumelden. Die Konten der externen Nutzer:innen sind über den Reiter "Standard" erreichbar.
Die Administratoren


Dear Gitlab user,
it is now possible to log in to our service using the ZIH login/LDAP. The accounts of external users can be accessed via the "Standard" tab.
The administrators

PetscSolverCahnHilliard.cc 6.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



#include "PetscSolverCahnHilliard.h"
#include "parallel/PetscHelper.h"
#include "parallel/PetscSolverGlobalMatrix.h"

namespace AMDiS {

  using namespace std;

  /// solve Cahn-Hilliard Preconditioner
  PetscErrorCode pcChShell(PC pc, Vec b, Vec x) // solve Px=b
  {FUNCNAME("PCApply()");
  
   void *ctx;
    PCShellGetContext(pc, &ctx);
    CahnHilliardData* data = static_cast<CahnHilliardData*>(ctx);
    
    Vec b1, b2, x1, x2;
    VecNestGetSubVec(b, 0, &b1);
    VecNestGetSubVec(b, 1, &b2);

    VecNestGetSubVec(x, 0, &x1);
    VecNestGetSubVec(x, 1, &x2);

    Vec y1, y2;
    VecDuplicate(b1, &y1);
    VecDuplicate(b2, &y2);
    
//     MatGetDiagonal(data->matM, y2);
//     VecReciprocal(y2);
//     VecPointwiseMult(y1, y2, b1);
    KSPSolve(data->kspMass, b1, y1); 			// M*y1 = b1    
    MatMultAdd(data->matMinusDeltaK, y1, b2, x1); 	// -> x1 := b2-delta*K*y1
    
    KSPSolve(data->kspLaplace, x1, y2); 			// (M+eps*sqrt(delta))*y2 = x1
    MatMult(data->matM, y2, x1); 			// x1 := M*y2
    
    KSPSolve(data->kspLaplace, x1, x2);			// (M+eps*sqrt(delta))*x2 = x1
    double factor = (*data->eps)/sqrt(*data->delta);
    VecCopy(x2, x1); 					// x1 := x2
    VecAXPBYPCZ(x1, 1.0, factor, -factor, y1, y2);	// x1 = 1*y1 + factor*y2 - factor*x1
    
    VecDestroy(&y1);
    VecDestroy(&y2);

  }
  

  PetscSolverCahnHilliard::PetscSolverCahnHilliard(string name, double *epsPtr, double *deltaPtr)
    : PetscSolverGlobalBlockMatrix(name),
      massMatrixSolver(NULL),
      laplaceMatrixSolver(NULL),
      deltaKMatrixSolver(NULL),
      useOldInitialGuess(false),
      eps(epsPtr),
      delta(deltaPtr) { 	
	    Parameters::get(initFileStr + "->use old initial guess", useOldInitialGuess);
      }


  void PetscSolverCahnHilliard::solvePetscMatrix(SystemVector &vec, 
						 AdaptInfo *adaptInfo)
  {
    FUNCNAME("PetscSolverCahnHilliard::solvePetscMatrix()");

/*    if (useOldInitialGuess) {
      VecSet(getVecSolInterior(), 0.0);
      
      for (int i = 0; i < solution->getSize(); i++)
      {
	Vec tmp;
        VecNestGetSubVec(getVecSolInterior(), i, &tmp);
	setDofVector(tmp, solution->getDOFVector(i));
      }
      
      vecSolAssembly();
      KSPSetInitialGuessNonzero(kspInterior, PETSC_TRUE);
    }
*/ KSPSetInitialGuessNonzero(kspInterior, PETSC_TRUE);
    PetscSolverGlobalBlockMatrix::solvePetscMatrix(vec, adaptInfo);
  }
      
  void PetscSolverCahnHilliard::initSolver(KSP &ksp)
  {
    FUNCNAME("PetscSolverCahnHilliard::initSolver()");

    // Create FGMRES based outer solver
    KSPCreate(meshDistributor->getMpiComm(0), &ksp);
    KSPSetOperators(ksp, getMatInterior(), getMatInterior(), SAME_NONZERO_PATTERN);
    KSPMonitorSet(ksp, KSPMonitorTrueResidualNorm, PETSC_NULL, PETSC_NULL);
    petsc_helper::setSolver(ksp, "ch_", KSPFGMRES, PCNONE, 1e-6, 1e-8, 1000);
         KSPSetFromOptions(ksp);
	 
  }


  void PetscSolverCahnHilliard::initPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverCahnHilliard::initPreconditioner()");
    MSG("PetscSolverCahnHilliard::initPreconditioner()\n");

//     KSPSetUp(kspInterior);

    PCSetType(pc, PCSHELL);
    PCShellSetApply(pc, pcChShell);
    PCShellSetContext(pc, &matShellContext);

    const FiniteElemSpace *feSpace = componentSpaces[0];
    
    // === matrix M ===	
    DOFMatrix massMatrix(feSpace, feSpace);
    Operator massOp(feSpace, feSpace);
    Simple_ZOT zot;
    massOp.addTerm(&zot);
    massMatrix.assembleOperator(massOp);
    massMatrixSolver = createSubSolver(0, "mass_");
    massMatrixSolver->fillPetscMatrix(&massMatrix);
    
    
    // === matrix (M + eps*sqrt(delta)*K) ===
    DOFMatrix laplaceMatrix(feSpace, feSpace);
    Operator laplaceOp(feSpace, feSpace);
    laplaceOp.addTerm(&zot); // M
    Simple_SOT sot2((*eps)*sqrt(*delta));
    laplaceOp.addTerm(&sot2); // eps*sqrt(delta)*K
    laplaceMatrix.assembleOperator(laplaceOp);
    laplaceMatrixSolver = createSubSolver(0, "laplace_");
    laplaceMatrixSolver->fillPetscMatrix(&laplaceMatrix);

    
    // === matrix (-delta*K) ===
    DOFMatrix deltaKMatrix(feSpace, feSpace);
    Operator laplaceOp2(feSpace, feSpace);
    Simple_SOT sot(-(*delta));
    laplaceOp2.addTerm(&sot); // -delta*K
    deltaKMatrix.assembleOperator(laplaceOp2);
    deltaKMatrixSolver = createSubSolver(0, "laplace2_");
    deltaKMatrixSolver->fillPetscMatrix(&deltaKMatrix);

    
    // === Setup solver ===
    matShellContext.kspMass = massMatrixSolver->getSolver();
    matShellContext.kspLaplace = laplaceMatrixSolver->getSolver();
    matShellContext.matM = massMatrixSolver->getMatInterior();    
    matShellContext.matMinusDeltaK = deltaKMatrixSolver->getMatInterior();   
    matShellContext.eps = eps;
    matShellContext.delta = delta;
    
    matShellContext.mpiCommGlobal= &(meshDistributor->getMpiComm(0));

    petsc_helper::setSolver(matShellContext.kspMass, "", KSPCG, PCJACOBI, 0.0, 1e-14, 2);
//     petsc_helper::setSolver(matShellContext.kspMass, "mass_", KSPRICHARDSON, PCLU, 0.0, 1e-14, 1);
//     {
//             PC pc;
//       KSPGetPC(matShellContext.kspMass, &pc);
//       PCFactorSetMatSolverPackage(pc, MATSOLVERMUMPS);
//     }

    petsc_helper::setSolver(matShellContext.kspLaplace, "laplace_", KSPRICHARDSON, PCHYPRE, 0.0, 1e-14, 1);
//     petsc_helper::setSolver(matShellContext.kspLaplace, "laplace_", KSPRICHARDSON, PCLU, 0.0, 1e-14, 1);
//     {
//             PC pc;
//       KSPGetPC(matShellContext.kspLaplace, &pc);
//       PCFactorSetMatSolverPackage(pc, MATSOLVERMUMPS);
//     }
     PCSetFromOptions(pc);
  }

  
  PetscSolver* PetscSolverCahnHilliard::createSubSolver(int component,
							string kspPrefix)
  {
    FUNCNAME("PetscSolverCahnHilliard::createSubSolver()");

    vector<const FiniteElemSpace*> fe;
    fe.push_back(componentSpaces[component]);

    PetscSolver* subSolver = new PetscSolverGlobalMatrix("");
    subSolver->setKspPrefix(kspPrefix.c_str());
    subSolver->setMeshDistributor(meshDistributor, 0);
    subSolver->init(fe, fe);

    ParallelDofMapping &subDofMap = subSolver->getDofMap();
    subDofMap[0] = dofMap[component];
    subDofMap.update();

    return subSolver;
  }
  
  void PetscSolverCahnHilliard::exitPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverNavierStokes::exitPreconditioner()");
    
    massMatrixSolver->destroyMatrixData();
    laplaceMatrixSolver->destroyMatrixData();
    deltaKMatrixSolver->destroyMatrixData();

    massMatrixSolver->destroyVectorData();
    laplaceMatrixSolver->destroyVectorData();
    deltaKMatrixSolver->destroyVectorData();

    
    delete massMatrixSolver;
    massMatrixSolver = NULL;

    delete laplaceMatrixSolver;
    laplaceMatrixSolver = NULL;

    delete deltaKMatrixSolver;
    deltaKMatrixSolver = NULL;
  }


}