11 #ifndef _PETSCSOLVERI_H_ 12 #define _PETSCSOLVERI_H_ 19 #include "NamespaceHeader.H" 21 #include <private/kspimpl.h> 22 #include <private/pcimpl.h> 29 :m_homogeneous(false),
37 m_nz_init_guess(false),
64 SNESDestroy( &m_snes );
111 CH_TIME(
"PetscSolver::create_mat_vec");
116 const int nc = a_phi.nComp();
118 MPI_Comm wcomm = Chombo_MPI::comm;
120 MPI_Comm wcomm = PETSC_COMM_SELF;
132 IntVect idghosts = a_phi.ghostVect();
135 MayDay::Error(
"PetscSolver<T>::create_mat_vec: No ghost cells in input LevelData<>.");
140 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
144 else data += box.
size(0)*box.
size(1);
149 const int NN = nc*(int)data;
152 ierr = MPI_Scan( &data, &result, 1, MPI_INT, MPI_SUM, wcomm );
158 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
166 gidsFab(iv,0) = (
Real)gid;
172 ierr = MatCreateMPIAIJ( wcomm, NN, NN, PETSC_DECIDE, PETSC_DECIDE,
173 nnzrow, PETSC_NULL, nnzrow/2, PETSC_NULL, &m_mat ); CHKERRQ(ierr);
174 ierr = MatSetFromOptions( m_mat ); CHKERRQ(ierr);
177 ierr = VecCreate( wcomm, &m_bb ); CHKERRQ(ierr);
178 ierr = VecSetFromOptions( m_bb ); CHKERRQ(ierr);
179 ierr = VecSetSizes( m_bb, NN, PETSC_DECIDE ); CHKERRQ(ierr);
180 ierr = VecDuplicate( m_bb, &m_rr ); CHKERRQ(ierr);
181 ierr = VecDuplicate( m_bb, &m_xx ); CHKERRQ(ierr);
204 MPI_Comm wcomm = Chombo_MPI::comm;
206 MPI_Comm wcomm = PETSC_COMM_SELF;
208 if ( m_defined == 0 )
218 ierr = formMatrix( m_mat, a_phi ); CHKERRQ(ierr);
221 if ( m_function && !m_snes )
223 ierr = SNESCreate( wcomm, &m_snes ); CHKERRQ(ierr);
224 ierr = SNESSetFunction( m_snes, m_rr, m_function, (
void*)&a_phi ); CHKERRQ(ierr);
225 ierr = SNESSetJacobian( m_snes, m_mat, m_mat, m_jacobian, (
void*)&a_phi ); CHKERRQ(ierr);
226 ierr = SNESSetFromOptions( m_snes ); CHKERRQ(ierr);
227 ierr = SNESGetKSP( m_snes, &ksp ); CHKERRQ(ierr);
228 ierr = SNESSetApplicationContext( m_snes, (
void*)
this ); CHKERRQ(ierr);
230 else if ( !m_function && !m_ksp )
233 KSPCreate( wcomm, &m_ksp );
234 KSPSetFromOptions( m_ksp );
235 ierr = KSPSetOperators(m_ksp,m_mat,m_mat,SAME_NONZERO_PATTERN); CHKERRQ(ierr);
236 ierr = KSPSetInitialGuessNonzero(m_ksp,
m_nz_init_guess ? PETSC_TRUE : PETSC_FALSE ); CHKERRQ(ierr);
242 PC pc;
const PCType type; PetscInt sz,ind,bs,n,m;
243 ierr = KSPGetPC( ksp, &pc ); CHKERRQ(ierr);
244 ierr = PCGetType( pc, &type ); CHKERRQ(ierr);
245 ierr = MatGetBlockSize( m_mat, &bs ); CHKERRQ( ierr );
246 if ( strcmp(type,PCGAMG) == 0 && bs > 1 )
251 ierr = MatGetLocalSize( m_mat, &m, &n ); CHKERRQ(ierr);
253 ierr = PetscMalloc( (sz+1)*
sizeof(PetscReal), &coords ); CHKERRQ(ierr);
254 for ( dit = a_phi.dataIterator(), ind = 0 ; dit.
ok() ; ++dit )
256 const Box &
box = a_phi.getBoxes()[dit];
261 for (
int k=0; k<
CH_SPACEDIM; k++) coords[ind++] = (PetscReal)iv[k];
265 ierr = PCSetCoordinates( pc,
CH_SPACEDIM, coords ); CHKERRQ(ierr);
266 ierr = PetscFree( coords ); CHKERRQ(ierr);
273 else if ( m_defined == 1 )
278 ierr = MatZeroEntries( m_mat ); CHKERRQ(ierr);
279 ierr = formMatrix( m_mat, a_phi ); CHKERRQ(ierr);
286 ierr = SNESGetKSP( m_snes, &ksp ); CHKERRQ(ierr);
288 ierr = KSPSetOperators(m_ksp,m_mat,m_mat,SAME_NONZERO_PATTERN); CHKERRQ(ierr);
308 const int nc = a_rhs.nComp();
310 MPI_Comm wcomm = Chombo_MPI::comm;
312 MPI_Comm wcomm = PETSC_COMM_SELF;
324 ierr = VecSet( m_xx, 0.); CHKERRQ(ierr);
325 ierr = VecSet( m_bb, 0.); CHKERRQ(ierr);
326 for ( dit = a_rhs.dataIterator() ; dit.
ok() ; ++dit )
339 int mm, ki = nc*(int)gidsFab(iv,0);
340 for (mm=0;mm<nc;mm++,ki++)
342 PetscScalar v = rhsFab(iv,mm);
354 ierr = VecSetValues(m_bb,1,&ki,&v,INSERT_VALUES); CHKERRQ(ierr);
359 ierr = VecSetValues(m_xx,1,&ki,&v,INSERT_VALUES); CHKERRQ(ierr);
364 ierr = VecAssemblyBegin( m_bb ); CHKERRQ(ierr);
365 ierr = VecAssemblyEnd( m_bb ); CHKERRQ(ierr);
368 ierr = VecAssemblyBegin( m_xx ); CHKERRQ(ierr);
369 ierr = VecAssemblyEnd( m_xx ); CHKERRQ(ierr);
377 ierr = MatNullSpaceCreate(wcomm,PETSC_TRUE,0,PETSC_NULL,&nullsp);CHKERRQ(ierr);
379 ierr = KSPSetNullSpace( m_ksp, nullsp ); CHKERRQ(ierr);
386 ierr = SNESSolve( m_snes, m_bb, m_xx ); CHKERRQ(ierr);
391 ierr = KSPSolve( m_ksp, m_bb, m_xx ); CHKERRQ(ierr);
397 ierr = putPetscInChombo( a_phi, m_xx ); CHKERRQ(ierr);
413 const int nc = a_phi.nComp();
415 ierr = VecGetArray(xx,&arr); CHKERRQ(ierr);
416 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
425 int mm, ki = nc*(int)gidsFab(iv,0);
426 for (mm=0;mm<nc;mm++,ki++)
428 phiFab(iv,mm) = arr[ki - nc*
m_gid0];
432 ierr = VecRestoreArray(xx,&arr); CHKERRQ(ierr);
443 const int nc = a_phi.nComp();
447 ierr = VecSet( out, 0.); CHKERRQ(ierr);
448 for ( dit = a_phi.dataIterator() ; dit.
ok() ; ++dit )
457 int mm, ki = nc*(int)gidsFab(iv,0);
458 for (mm=0;mm<nc;mm++,ki++)
460 PetscScalar v = phiFab(iv,mm);
463 ierr = VecSetValues(out,1,&ki,&v,INSERT_VALUES);
467 ierr = VecAssemblyBegin( out ); CHKERRQ(ierr);
468 ierr = VecAssemblyEnd( out ); CHKERRQ(ierr);
482 PetscScalar alpha = -1;
485 ierr = VecDuplicate(m_xx, &tempVec);
486 ierr = MatMult(m_mat, m_xx, tempVec);
487 ierr = VecAXPY(tempVec,alpha,m_bb);
491 Residual.
define( dbl, 0, idghosts );
493 ierr = putPetscInChombo( Residual, tempVec ); CHKERRQ(ierr);
494 VecDestroy( &tempVec );
509 const int nc = a_phi.nComp();
519 ierr = MatZeroEntries( m_mat ); CHKERRQ(ierr);
520 ierr = formMatrix( m_mat, a_phi ); CHKERRQ(ierr);
523 ierr = putChomboInPetsc( m_bb, a_phi ); CHKERRQ(ierr);
526 ierr = MatMult( m_mat, m_bb, m_xx ); CHKERRQ(ierr);
529 ierr = putPetscInChombo( a_out, m_xx ); CHKERRQ(ierr);
535 #include "NamespaceFooter.H" bool ok()
Definition: BoxIterator.H:215
#define CH_TIMERS(name)
Definition: CH_Timer.H:70
virtual void exchange(const Interval &comps)
Definition: LevelDataI.H:302
Definition: LinearSolver.H:28
IntVect size() const
size functions
Definition: Box.H:1814
#define CH_SPACEDIM
Definition: SPACE.H:52
#define CH_assert(cond)
Definition: CHArray.H:37
virtual void solve(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:95
int solve_private(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:297
void setVal(T a_x, const Box &a_bx, int a_nstart, int a_numcomp)
{ data modification functions}
Definition: BaseFabImplem.H:326
#define CH_START(tpointer)
Definition: CH_Timer.H:78
int m_gid0
Definition: PetscSolver.H:158
virtual void define(LinearOp< T > *a_operator, bool a_homogeneous=false)
Definition: PetscSolverI.H:78
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:110
Definition: DataIterator.H:140
iterates through the IntVects of a Box
Definition: BoxIterator.H:37
void print_memory_line(const char *)
#define CH_TIMER(name, tpointer)
Definition: CH_Timer.H:55
Definition: EBInterface.H:45
int applyOp(T &a_phi, const T &a_rhs)
Definition: PetscSolverI.H:507
virtual ~PetscSolver()
Definition: PetscSolverI.H:71
Real m_dx
Definition: PetscSolver.H:107
bool m_null
Definition: PetscSolver.H:154
Real computeResidual()
Definition: PetscSolverI.H:478
void setNull(bool n=true)
Definition: PetscSolverI.H:88
LevelData< BaseFab< bool > > m_bccode
Definition: PetscSolver.H:157
#define CH_TIME(name)
Definition: CH_Timer.H:59
virtual void defineData(T &a_data, const T &a_template)=0
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:70
virtual BaseFab< Real > & getRegFab(T &a_fab, DataIterator &dit)=0
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:31
Box get(const LayoutIndex &it) const
Definition: BoxLayout.H:681
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:92
PetscSolver()
Definition: PetscSolverI.H:28
static const IntVect Zero
Definition: IntVect.H:627
virtual Real addBCrhsValue(const IntVect &a_iv, const T &a_phi, DataIterator dit, const Real &coeff=1)
Definition: PetscSolver.H:136
bool m_nz_init_guess
Definition: PetscSolver.H:155
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
void begin()
Definition: BoxIterator.H:146
#define CH_STOP(tpointer)
Definition: CH_Timer.H:80
int setup_solver(const T &a_phi)
Definition: PetscSolverI.H:193
Definition: PetscSolver.H:38
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
void destroy()
Definition: PetscSolverI.H:44
void next()
Definition: BoxIterator.H:164
int create_mat_vec(const T &a_phi)
Definition: PetscSolverI.H:109
virtual Real dx() const
Definition: LinearSolver.H:130
bool m_homogeneous
Definition: PetscSolver.H:101
T m_gids
Definition: PetscSolver.H:156