11 #ifndef _PETSCSOLVERI_H_ 12 #define _PETSCSOLVERI_H_ 19 #include "NamespaceHeader.H" 27 :m_homogeneous(false),
35 m_nz_init_guess(false),
110 const T& rhs = a_rhs;
121 const T& rhs = a_rhs;
133 CH_TIME(
"PetscSolver::create_mat_vec");
138 const PetscInt nc = a_phi.nComp();
140 MPI_Comm wcomm = Chombo_MPI::comm;
142 MPI_Comm wcomm = PETSC_COMM_SELF;
150 IntVect idghosts = a_phi.ghostVect();
153 MayDay::Error(
"PetscSolver<T>::create_mat_vec: No ghost cells in input LevelData<>.");
162 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
169 const PetscInt NN = nc*data;
173 PetscDataTypeToMPIDataType(PETSC_INT,&mtype);
174 MPI_Scan( &data, &result, 1, mtype, MPI_SUM, wcomm );
180 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
195 PetscInt *d_nnz=PETSC_NULL, *o_nnz=PETSC_NULL;
202 MPI_Comm wcomm = Chombo_MPI::comm;
204 PetscDataTypeToMPIDataType(PETSC_INT,&mtype);
205 MPI_Allreduce(&nn,&nglobal,1,mtype,MPI_SUM,wcomm);
209 ierr = PetscMalloc( (NN+1)*
sizeof(PetscInt), &d_nnz ); CHKERRQ(ierr);
210 ierr = PetscMalloc( (NN+1)*
sizeof(PetscInt), &o_nnz ); CHKERRQ(ierr);
211 for (PetscInt kk=0;kk<NN;kk++) d_nnz[kk] = o_nnz[kk] = 0;
216 for (PetscInt kk=0;kk<NN;kk++)
218 if (d_nnz[kk] > NN) d_nnz[kk] = NN;
219 if (o_nnz[kk] > nglobal-NN) o_nnz[kk] = nglobal-NN;
224 ierr = MatCreate(wcomm,&
m_mat);CHKERRQ(ierr);
228 pout() <<
"debug::matsetsize NN = " << NN << endl;
229 ierr = MatSetSizes(
m_mat,NN,NN,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
230 pout() <<
"debug::matblocksize nc = " << nc << endl;
231 ierr = MatSetBlockSize(
m_mat,nc);CHKERRQ(ierr);
232 ierr = MatSetType(
m_mat,MATAIJ);CHKERRQ(ierr);
234 ierr = MatSetFromOptions(
m_mat ); CHKERRQ(ierr);
235 pout() <<
"debug::matsetpreallocation nnzrow = " << nnzrow << endl;
236 ierr = MatSeqAIJSetPreallocation(
m_mat,nnzrow, d_nnz);CHKERRQ(ierr);
237 ierr = MatMPIAIJSetPreallocation(
m_mat,nnzrow, d_nnz, nnzrow/2, o_nnz);CHKERRQ(ierr);
238 ierr = MatSetOption(
m_mat,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE) ;CHKERRQ(ierr);
240 #if defined(PETSC_HAVE_HYPRE) 241 ierr = MatHYPRESetPreallocation(
m_mat,nnzrow, d_nnz, nnzrow/2, o_nnz);CHKERRQ(ierr);
246 ierr = PetscFree( d_nnz ); CHKERRQ(ierr);
247 ierr = PetscFree( o_nnz ); CHKERRQ(ierr);
255 ierr = VecDuplicate(
m_bb, &
m_rr ); CHKERRQ(ierr);
278 MPI_Comm wcomm = Chombo_MPI::comm;
280 MPI_Comm wcomm = PETSC_COMM_SELF;
298 #if PETSC_VERSION_GE(3,6,0) 299 strcat (str,
"pc_gamg_square_graph 20");
301 strcat (str,
"pc_gamg_square_graph true");
303 #if PETSC_VERSION_GE(3,7,0) 304 ierr = PetscOptionsInsertString(PETSC_NULL,str);CHKERRQ(ierr);
306 ierr = PetscOptionsInsertString(str);CHKERRQ(ierr);
311 PetscBool ism = PETSC_FALSE;
312 #if PETSC_VERSION_GE(3,7,0) 313 PetscOptionsGetBool(PETSC_NULL,
m_prestring,
"-ksp_monitor",&ism,PETSC_NULL);
315 PetscOptionsGetBool(
m_prestring,
"-ksp_monitor",&ism,PETSC_NULL);
319 ierr = SNESCreate( wcomm, &
m_snes ); CHKERRQ(ierr);
324 ierr = SNESSetFromOptions(
m_snes ); CHKERRQ(ierr);
325 ierr = SNESGetKSP(
m_snes, &ksp ); CHKERRQ(ierr);
328 ierr = KSPMonitorSet(ksp,
ksp_monitor_pout,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
334 KSPCreate( wcomm, &
m_ksp );
339 ierr = KSPSetFromOptions(
m_ksp);CHKERRQ(ierr);
344 #if PETSC_VERSION_GE(3,5,0) 347 ierr = KSPSetOperators(
m_ksp,
m_mat,
m_mat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
349 ierr = KSPSetInitialGuessNonzero(
m_ksp,
m_nz_init_guess ? PETSC_TRUE : PETSC_FALSE );CHKERRQ(ierr);
356 PetscInt sz,ind,bs,n,m;
357 #if PETSC_VERSION_LT(3,4,0) & PETSC_VERSION_RELEASE 362 ierr = KSPGetPC( ksp, &pc ); CHKERRQ(ierr);
363 ierr = PCGetType( pc, &type ); CHKERRQ(ierr);
364 ierr = MatGetBlockSize(
m_mat, &bs ); CHKERRQ( ierr );
365 if ( strcmp(type,PCGAMG) == 0 && bs > 1 )
370 ierr = MatGetLocalSize(
m_mat, &m, &n ); CHKERRQ(ierr);
372 ierr = PetscMalloc( (sz+1)*
sizeof(PetscReal), &coords ); CHKERRQ(ierr);
373 for ( dit = a_phi.dataIterator(), ind = 0 ; dit.
ok() ; ++dit )
375 const Box &
box = a_phi.getBoxes()[dit];
380 for (PetscInt k=0; k<
CH_SPACEDIM; k++) coords[ind++] = (PetscReal)iv[k];
385 ierr = PetscFree( coords ); CHKERRQ(ierr);
397 ierr = MatZeroEntries(
m_mat ); CHKERRQ(ierr);
405 ierr = SNESGetKSP(
m_snes, &ksp ); CHKERRQ(ierr);
407 #if PETSC_VERSION_GE(3,5,0) 408 ierr = KSPSetOperators(ksp,
m_mat,
m_mat); CHKERRQ(ierr);
410 ierr = KSPSetOperators(ksp,
m_mat,
m_mat,SAME_NONZERO_PATTERN); CHKERRQ(ierr);
430 const PetscInt nc = a_rhs.nComp();
432 MPI_Comm wcomm = Chombo_MPI::comm;
434 MPI_Comm wcomm = PETSC_COMM_SELF;
439 MPI_Barrier(Chombo_MPI::comm);
449 ierr = VecSetOption(
m_bb,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
450 ierr = VecSetOption(
m_xx,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
452 ierr = VecSet(
m_xx, 0.);CHKERRQ(ierr);
453 ierr = VecSet(
m_bb, 0.);CHKERRQ(ierr);
457 #pragma omp parallel for private(addV,ierr) 458 for (
int mybox=0;mybox<nbox; mybox++)
469 PetscInt mm, ki = nc*gidsFab(iv,0);
470 for (mm=0;mm<nc;mm++,ki++)
472 PetscScalar v = rhsFab(iv,mm);
482 ierr = VecSetValues(
m_bb,1,&ki,&v,INSERT_VALUES);
484 ierr = VecSetValues(
m_xx,1,&ki,&v,INSERT_VALUES);
489 ierr = VecAssemblyBegin(
m_bb );CHKERRQ(ierr);
490 ierr = VecAssemblyEnd(
m_bb );CHKERRQ(ierr);
493 ierr = VecAssemblyBegin(
m_xx );CHKERRQ(ierr);
494 ierr = VecAssemblyEnd(
m_xx );CHKERRQ(ierr);
501 ierr = MatNullSpaceCreate(wcomm,PETSC_TRUE,0,PETSC_NULL,&nullsp);CHKERRQ(ierr);
503 ierr = MatSetNullSpace(
m_mat, nullsp );CHKERRQ(ierr);
508 MPI_Barrier(Chombo_MPI::comm);
534 CH_TIME(
"PetscSolver::apply_mfree");
538 MatShellGetContext(A, &ctx);
543 PetscFunctionReturn(0);
551 CH_TIME(
"PetscSolver::solve_mfree_private");
554 MPI_Comm wcomm = Chombo_MPI::comm;
556 MPI_Comm wcomm = PETSC_COMM_SELF;
564 ierr = MatGetSize(
m_mat, &M, &N);CHKERRQ(ierr);
CH_assert(M == N);
565 ierr = MatGetLocalSize(
m_mat, &m, &n);CHKERRQ(ierr);
567 ierr = MatCreateShell(wcomm,m,n,N,N,(
void *)
this,&L);CHKERRQ(ierr);
568 ierr = MatShellSetOperation(L,MATOP_MULT,(
void(*)(
void))
apply_mfree);
579 ierr = VecSetOption(
m_bb,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
580 ierr = VecSetOption(
m_xx,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
581 ierr = VecSet(
m_xx, 0.);CHKERRQ(ierr);
582 ierr = VecSet(
m_bb, 0.);CHKERRQ(ierr);
584 const PetscInt nc = a_rhs.nComp();
588 for (
int mybox=0;mybox<nbox; mybox++)
599 PetscInt mm, ki = nc*gidsFab(iv,0);
600 for (mm=0;mm<nc;mm++,ki++)
602 PetscScalar v = rhsFab(iv,mm);
612 ierr = VecSetValues(
m_bb,1,&ki,&v,INSERT_VALUES); CHKERRQ(ierr);
614 ierr = VecSetValues(
m_xx,1,&ki,&v,INSERT_VALUES); CHKERRQ(ierr);
619 ierr = VecAssemblyBegin(
m_bb );CHKERRQ(ierr);
620 ierr = VecAssemblyEnd(
m_bb );CHKERRQ(ierr);
621 ierr = VecAssemblyBegin(
m_xx );CHKERRQ(ierr);
622 ierr = VecAssemblyEnd(
m_xx );CHKERRQ(ierr);
625 KSPCreate( wcomm, &ksp );
630 ierr = KSPSetFromOptions( ksp );CHKERRQ(ierr);
632 #if PETSC_VERSION_GE(3,7,0) 633 PetscOptionsGetBool(PETSC_NULL,
m_prestring,
"-ksp_monitor",&ism,PETSC_NULL);
635 PetscOptionsGetBool(
m_prestring,
"-ksp_monitor",&ism,PETSC_NULL);
640 ierr = KSPMonitorSet(ksp,
ksp_monitor_pout,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr);
642 #if PETSC_VERSION_GE(3,5,0) 643 ierr = KSPSetOperators(ksp,L,
m_mat); CHKERRQ(ierr);
645 ierr = KSPSetOperators(ksp,L,
m_mat,SAME_NONZERO_PATTERN); CHKERRQ(ierr);
648 ierr = KSPSetInitialGuessNonzero(ksp,
m_nz_init_guess ? PETSC_TRUE : PETSC_FALSE ); CHKERRQ(ierr);
651 PC pc; PetscInt sz,ind,bs,n,m;
652 #if PETSC_VERSION_LT(3,4,0) & PETSC_VERSION_RELEASE 657 ierr = KSPGetPC( ksp, &pc ); CHKERRQ(ierr);
658 ierr = PCGetType( pc, &type ); CHKERRQ(ierr);
659 ierr = MatGetBlockSize(
m_mat, &bs ); CHKERRQ( ierr );
660 if ( strcmp(type,PCGAMG) == 0 && bs > 1 )
665 ierr = MatGetLocalSize(
m_mat, &m, &n ); CHKERRQ(ierr);
667 ierr = PetscMalloc( (sz+1)*
sizeof(PetscReal), &coords ); CHKERRQ(ierr);
668 for ( dit = a_phi.dataIterator(), ind = 0 ; dit.
ok() ; ++dit )
670 const Box &
box = a_phi.getBoxes()[dit];
675 for (PetscInt k=0; k<
CH_SPACEDIM; k++) coords[ind++] = (PetscReal)iv[k];
680 ierr = PetscFree( coords ); CHKERRQ(ierr);
685 ierr = KSPSolve( ksp,
m_bb,
m_xx );CHKERRQ(ierr);
705 const PetscScalar *arr;
707 const PetscInt nc = a_phi.nComp();
709 ierr = VecGetArrayRead(xx,&arr); CHKERRQ(ierr);
713 for (
int mybox=0;mybox<nbox; mybox++)
723 PetscInt mm, ki = nc*gidsFab(iv,0);
724 for (mm=0;mm<nc;mm++,ki++)
726 phiFab(iv,mm) = arr[ki - nc*
m_gid0];
730 ierr = VecRestoreArrayRead(xx,&arr); CHKERRQ(ierr);
738 CH_TIME(
"PetscSolver::putChomboInPetsc");
742 const PetscInt nc = a_phi.nComp();
746 ierr = VecSet( out, 0.); CHKERRQ(ierr);
750 for (
int mybox=0;mybox<nbox; mybox++)
760 PetscInt mm, ki = nc*gidsFab(iv,0);
761 for (mm=0;mm<nc;mm++,ki++)
763 PetscScalar v = phiFab(iv,mm);
765 if (std::isnan(v)) v = 0;
766 ierr = VecSetValues(out,1,&ki,&v,INSERT_VALUES);
771 ierr = VecAssemblyBegin( out ); CHKERRQ(ierr);
772 ierr = VecAssemblyEnd( out ); CHKERRQ(ierr);
786 PetscScalar alpha = -1;
789 ierr = VecDuplicate(
m_xx, &tempVec);CHKERRQ(ierr);
790 ierr = MatMult(
m_mat,
m_xx, tempVec);CHKERRQ(ierr);
791 ierr = VecAXPY(tempVec,alpha,
m_bb);CHKERRQ(ierr);
795 Residual.
define( dbl, 0, idghosts );
798 VecDestroy( &tempVec );
816 ierr = VecNorm(
m_rr,NORM_INFINITY,&norm);CHKERRQ(ierr);
847 #include "NamespaceFooter.H" std::ostream & pout()
Use this in place of std::cout for program output.
bool ok()
Definition: BoxIterator.H:281
#define CH_TIMERS(name)
Definition: CH_Timer.H:133
Definition: LinearSolver.H:28
virtual void define(Real a_dx, bool a_homogeneous=true)
Definition: PetscSolverI.H:81
#define CH_SPACEDIM
Definition: SPACE.H:51
#define CH_assert(cond)
Definition: CHArray.H:37
KSP m_ksp
Definition: PetscSolver.H:221
virtual void solve(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:106
int solve_private(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:420
PetscInt m_gid0
Definition: PetscSolver.H:241
void setVal(T a_x, const Box &a_bx, int a_nstart, int a_numcomp)
{ data modification functions}
Definition: BaseFabImplem.H:399
Real norm(const BoxLayoutData< FArrayBox > &A, const Interval &interval, const int &p=2)
#define CH_START(tpointer)
Definition: CH_Timer.H:145
static PetscErrorCode ksp_monitor_pout(KSP ksp, PetscInt it, PetscReal rnorm, void *ctx)
Definition: PetscSolver.H:65
virtual int formMatrix(Mat, const T *=0, PetscInt my0=0, PetscInt nloc=0, PetscInt *d=0, PetscInt *o=0)=0
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:117
Definition: DataIterator.H:190
virtual void solve_mfree(T &a_phi, const T &a_rhs, LinearOp< T > *a_op)
Definition: PetscSolverI.H:116
iterates through the IntVects of a Box
Definition: BoxIterator.H:37
virtual Real addBCrhsValue(const IntVect &a_iv, const T &a_phi, const DataIndex &a_datInd, const Real &coeff=1)
Definition: PetscSolver.H:208
virtual void exchange(void)
Simplest case – do all components.
Definition: LevelDataI.H:470
#define CH_TIMER(name, tpointer)
Definition: CH_Timer.H:63
Definition: EBInterface.H:45
virtual int applyOp(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:826
virtual ~PetscSolver()
Definition: PetscSolverI.H:74
virtual BaseFab< Real > & getRegFab(T &a_fab, const DataIndex &a_datInd)=0
Real m_dx
Definition: PetscSolver.H:172
virtual bool supportNNZExact() const
Definition: PetscSolver.H:151
bool m_null
Definition: PetscSolver.H:233
void const char const int const int const int const Real const Real * A
Definition: Lapack.H:83
int size() const
Definition: DataIterator.H:218
Real computeResidual()
Definition: PetscSolverI.H:782
PetscErrorCode putPetscInChombo(T &a_phi, const Vec xx)
Definition: PetscSolverI.H:702
void setNull(bool n=true)
Definition: PetscSolverI.H:99
LevelData< BaseFab< bool > > m_bccode
Definition: PetscSolver.H:235
LinearOp< T > * m_op_mfree
Definition: PetscSolver.H:95
#define CH_TIME(name)
Definition: CH_Timer.H:82
int solve_mfree_private(T &a_phi, const T &a_rhs, LinearOp< T > *a_op)
Definition: PetscSolverI.H:547
Mat m_mat
Definition: PetscSolver.H:216
Vec m_bb
Definition: PetscSolver.H:219
PetscErrorCode(* m_function)(SNES, Vec, Vec, void *)
Definition: PetscSolver.H:224
double Real
Definition: REAL.H:33
virtual void define(const DisjointBoxLayout &dp, int comps, const IntVect &ghost=IntVect::Zero, const DataFactory< T > &a_factory=DefaultDataFactory< T >())
Definition: LevelDataI.H:93
LevelData< BaseFab< PetscInt > > m_gids
Definition: PetscSolver.H:240
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:30
Box get(const LayoutIndex &it) const
Definition: BoxLayout.H:716
const IntVect & ghostVect() const
Definition: LevelData.H:186
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.
virtual int getNNZPerRow() const
Definition: PetscSolver.H:144
Real normInfinity(const T &a_phi) const
Definition: PetscSolverI.H:810
PetscSolver()
Definition: PetscSolverI.H:26
static const IntVect Zero
Definition: IntVect.H:654
virtual void clear(T &a_lhs)
Definition: LinearSolver.H:70
T m_phi_mfree
Definition: PetscSolver.H:96
bool m_nz_init_guess
Definition: PetscSolver.H:234
char m_prestring[32]
Definition: PetscSolver.H:232
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
static PetscErrorCode apply_mfree(Mat A, Vec x, Vec f)
Definition: PetscSolverI.H:532
virtual void create(T &a_lhs, const T &a_rhs)=0
Vec m_xx
Definition: PetscSolver.H:219
Definition: DataIndex.H:112
T m_Lphi_mfree
Definition: PetscSolver.H:97
const DisjointBoxLayout & disjointBoxLayout() const
Definition: LevelData.H:225
void begin()
Definition: BoxIterator.H:150
#define CH_STOP(tpointer)
Definition: CH_Timer.H:150
int setup_solver(const T &a_phi)
Definition: PetscSolverI.H:267
PetscErrorCode putChomboInPetsc(Vec out, const T &a_phi)
Definition: PetscSolverI.H:736
Definition: PetscSolver.H:36
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
void destroy()
Definition: PetscSolverI.H:43
PetscErrorCode(* m_jacobian)(SNES, Vec, Mat *, Mat *, MatStructure *, void *)
Definition: PetscSolver.H:228
void const char const int * M
Definition: Lapack.H:83
void next()
Definition: BoxIterator.H:168
void const char const int const int * N
Definition: Lapack.H:83
SNES m_snes
Definition: PetscSolver.H:220
int create_mat_vec(const T &a_phi)
Definition: PetscSolverI.H:131
PetscInt m_defined
Definition: PetscSolver.H:223
virtual Real dx() const
Definition: LinearSolver.H:128
Vec m_rr
Definition: PetscSolver.H:219
bool m_homogeneous
Definition: PetscSolver.H:166