www.mooseframework.org
Public Member Functions | Private Attributes | List of all members
ExternalPETScProblem Class Reference

This is an interface to call a pure PETSc solver. More...

#include <ExternalPETScProblem.h>

Inheritance diagram for ExternalPETScProblem:
[legend]

Public Member Functions

 ExternalPETScProblem (const InputParameters &params)
 
 ~ExternalPETScProblem ()
 
virtual void externalSolve () override
 
virtual void syncSolutions (Direction) override
 
virtual bool converged () override
 

Private Attributes

const VariableName & _sync_to_var_name
 The name of the variable to transfer to. More...
 
ExternalPetscSolverApp_petsc_app
 
TS & _ts
 PETSc solver. More...
 
Vec _petsc_sol
 PETSc solver solution. More...
 

Detailed Description

This is an interface to call a pure PETSc solver.

We also sync the PETSc solution to moose variables, and then these variables can be coupled to other moose applications

Definition at line 27 of file ExternalPETScProblem.h.

Constructor & Destructor Documentation

◆ ExternalPETScProblem()

ExternalPETScProblem::ExternalPETScProblem ( const InputParameters &  params)

Definition at line 25 of file ExternalPETScProblem.C.

26  : ExternalProblem(params),
27  _sync_to_var_name(getParam<VariableName>("sync_variable")),
28  // ExternalPETScProblem always requires ExternalPetscSolverApp
29  _petsc_app(static_cast<ExternalPetscSolverApp &>(_app))
30 #if LIBMESH_HAVE_PETSC
31  ,
33 {
34  DM da;
35  TSGetDM(_ts, &da);
36  DMCreateGlobalVector(da, &_petsc_sol);
38 }

◆ ~ExternalPETScProblem()

ExternalPETScProblem::~ExternalPETScProblem ( )
inline

Definition at line 32 of file ExternalPETScProblem.h.

32 { VecDestroy(&_petsc_sol); }

Member Function Documentation

◆ converged()

virtual bool ExternalPETScProblem::converged ( )
inlineoverridevirtual

Definition at line 38 of file ExternalPETScProblem.h.

38 { return true; }

◆ externalSolve()

void ExternalPETScProblem::externalSolve ( )
overridevirtual
Initial value:
{
mooseError("You need to have PETSc installed to use ExternalPETScProblem")

Definition at line 46 of file ExternalPETScProblem.C.

47 {
48 #if LIBMESH_HAVE_PETSC
49  _console << "PETSc External Solve!" << std::endl;
51 #endif
52 }

◆ syncSolutions()

void ExternalPETScProblem::syncSolutions ( Direction  direction)
overridevirtual

Definition at line 55 of file ExternalPETScProblem.C.

56 {
57 #if LIBMESH_HAVE_PETSC
58  if (direction == Direction::FROM_EXTERNAL_APP)
59  {
60  _console << "syncSolutions from external petsc App" << std::endl;
61  DM da;
62  // xs: start grid point in x direction on local
63  // ys: start grid point in y direciton on local
64  // xm: number of grid points in x direciton on local
65  // ym: number of grid points in y direction on local
66  // Mx: number of grid points in x direction on all processors
67  PetscInt i, j, xs, ys, xm, ym, Mx;
68  PetscScalar ** _petsc_sol_array;
69  TSGetDM(_ts, &da);
70  DMDAGetInfo(da,
71  PETSC_IGNORE,
72  &Mx,
73  PETSC_IGNORE,
74  PETSC_IGNORE,
75  PETSC_IGNORE,
76  PETSC_IGNORE,
77  PETSC_IGNORE,
78  PETSC_IGNORE,
79  PETSC_IGNORE,
80  PETSC_IGNORE,
81  PETSC_IGNORE,
82  PETSC_IGNORE,
83  PETSC_IGNORE);
84  DMDAGetCorners(da, &xs, &ys, NULL, &xm, &ym, NULL);
85  DMDAVecGetArray(da, _petsc_sol, &_petsc_sol_array);
86 
87  // Take the solution from PETSc, and sync it to one MOOSE variable
88  // We currently support one variable only but it is straightforward
89  // to have multiple moose variables
90  MeshBase & to_mesh = mesh().getMesh();
91  auto & sync_to_var = getVariable(
92  0, _sync_to_var_name, Moose::VarKindType::VAR_ANY, Moose::VarFieldType::VAR_FIELD_STANDARD);
93 
94  for (j = ys; j < ys + ym; j++)
95  for (i = xs; i < xs + xm; i++)
96  {
97  Node * to_node = to_mesh.node_ptr(i + j * Mx);
98  if (to_node->n_comp(sync_to_var.sys().number(), sync_to_var.number()) > 1)
99  mooseError("Does not support multiple components");
100 
101  dof_id_type dof = to_node->dof_number(sync_to_var.sys().number(), sync_to_var.number(), 0);
102  // Copy the solution to the right location
103  sync_to_var.sys().solution().set(dof, _petsc_sol_array[j][i]);
104  }
105 
106  sync_to_var.sys().solution().close();
107 
108  DMDAVecRestoreArray(da, _petsc_sol, &_petsc_sol_array);
109  }
110  else if (direction == Direction::TO_EXTERNAL_APP)
111  {
112  _console << "syncSolutions to external petsc App " << std::endl;
113  // We could the similar thing to sync the solution back to PETSc.
114  }
115 #endif
116 }

Member Data Documentation

◆ _petsc_app

ExternalPetscSolverApp& ExternalPETScProblem::_petsc_app
private

Definition at line 43 of file ExternalPETScProblem.h.

◆ _petsc_sol

Vec ExternalPETScProblem::_petsc_sol
private

PETSc solver solution.

Definition at line 49 of file ExternalPETScProblem.h.

Referenced by ExternalPETScProblem(), externalSolve(), syncSolutions(), and ~ExternalPETScProblem().

◆ _sync_to_var_name

const VariableName& ExternalPETScProblem::_sync_to_var_name
private

The name of the variable to transfer to.

Definition at line 42 of file ExternalPETScProblem.h.

Referenced by syncSolutions().

◆ _ts

TS& ExternalPETScProblem::_ts
private

PETSc solver.

Definition at line 47 of file ExternalPETScProblem.h.

Referenced by ExternalPETScProblem(), externalSolve(), and syncSolutions().


The documentation for this class was generated from the following files:
ExternalPETScProblem::_sync_to_var_name
const VariableName & _sync_to_var_name
The name of the variable to transfer to.
Definition: ExternalPETScProblem.h:42
FormInitialSolution
PETSC_EXTERN PetscErrorCode FormInitialSolution(TS, Vec, void *)
Definition: PETScDiffusionFDM.C:432
externalPETScDiffusionFDMSolve
PETSC_EXTERN PetscErrorCode externalPETScDiffusionFDMSolve(TS, Vec, PetscReal, PetscReal)
Definition: PETScDiffusionFDM.C:99
ExternalPETScProblem::_petsc_app
ExternalPetscSolverApp & _petsc_app
Definition: ExternalPETScProblem.h:43
ExternalPETScProblem::_ts
TS & _ts
PETSc solver.
Definition: ExternalPETScProblem.h:47
ExternalPETScProblem::_petsc_sol
Vec _petsc_sol
PETSc solver solution.
Definition: ExternalPETScProblem.h:49
ExternalPetscSolverApp::getExternalPETScTS
TS & getExternalPETScTS()
Return a time-stepping (TS) component that holds all the ingredients of applicaiton.
Definition: ExternalPetscSolverApp.h:38