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 28 of file ExternalPETScProblem.h.

Constructor & Destructor Documentation

◆ ExternalPETScProblem()

ExternalPETScProblem::ExternalPETScProblem ( const InputParameters &  params)

Definition at line 24 of file ExternalPETScProblem.C.

25  : ExternalProblem(params),
26  _sync_to_var_name(getParam<VariableName>("sync_variable")),
27  // ExternalPETScProblem always requires ExternalPetscSolverApp
28  _petsc_app(static_cast<ExternalPetscSolverApp &>(_app))
29 #if LIBMESH_HAVE_PETSC
30  ,
32 {
33  DM da;
34  TSGetDM(_ts, &da);
35  DMCreateGlobalVector(da, &_petsc_sol);
37 }
TS & _ts
PETSc solver.
TS & getExternalPETScTS()
Return a time-stepping (TS) component that holds all the ingredients of applicaiton.
Vec _petsc_sol
PETSc solver solution.
ExternalPetscSolverApp & _petsc_app
PETSC_EXTERN PetscErrorCode FormInitialSolution(TS, Vec, void *)
const VariableName & _sync_to_var_name
The name of the variable to transfer to.

◆ ~ExternalPETScProblem()

ExternalPETScProblem::~ExternalPETScProblem ( )
inline

Definition at line 33 of file ExternalPETScProblem.h.

33 { VecDestroy(&_petsc_sol); }
Vec _petsc_sol
PETSc solver solution.

Member Function Documentation

◆ converged()

virtual bool ExternalPETScProblem::converged ( )
inlineoverridevirtual

Definition at line 39 of file ExternalPETScProblem.h.

39 { return true; }

◆ externalSolve()

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

Definition at line 45 of file ExternalPETScProblem.C.

46 {
47 #if LIBMESH_HAVE_PETSC
48  _console << "PETSc External Solve!" << std::endl;
50 #endif
51 }
TS & _ts
PETSc solver.
PETSC_EXTERN PetscErrorCode externalPETScDiffusionFDMSolve(TS, Vec, PetscReal, PetscReal)
Vec _petsc_sol
PETSc solver solution.

◆ syncSolutions()

void ExternalPETScProblem::syncSolutions ( Direction  direction)
overridevirtual

Definition at line 54 of file ExternalPETScProblem.C.

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

Member Data Documentation

◆ _petsc_app

ExternalPetscSolverApp& ExternalPETScProblem::_petsc_app
private

Definition at line 44 of file ExternalPETScProblem.h.

◆ _petsc_sol

Vec ExternalPETScProblem::_petsc_sol
private

PETSc solver solution.

Definition at line 50 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 43 of file ExternalPETScProblem.h.

Referenced by syncSolutions().

◆ _ts

TS& ExternalPETScProblem::_ts
private

PETSc solver.

Definition at line 48 of file ExternalPETScProblem.h.

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


The documentation for this class was generated from the following files: