www.mooseframework.org
ExternalPETScProblem.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "ExternalPETScProblem.h"
11 #include "SystemBase.h"
12 
13 registerMooseObject("ExternalPetscSolverApp", ExternalPETScProblem);
14 
15 template <>
16 InputParameters
18 {
19  InputParameters params = validParams<ExternalProblem>();
20  params.addRequiredParam<VariableName>("sync_variable",
21  "The variable PETSc external solution will be synced to");
22  return params;
23 }
24 
25 ExternalPETScProblem::ExternalPETScProblem(const InputParameters & params)
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  ,
32  _ts(_petsc_app.getExternalPETScTS())
33 {
34  DM da;
35  TSGetDM(_ts, &da);
36  DMCreateGlobalVector(da, &_petsc_sol);
38 }
39 #else
40 {
41  mooseError("You need to have PETSc installed to use ExternalPETScProblem");
42 }
43 #endif
44 
45 void
47 {
48 #if LIBMESH_HAVE_PETSC
49  _console << "PETSc External Solve!" << std::endl;
51 #endif
52 }
53 
54 void
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 }
ExternalPETScProblem::_sync_to_var_name
const VariableName & _sync_to_var_name
The name of the variable to transfer to.
Definition: ExternalPETScProblem.h:42
ExternalPETScProblem::syncSolutions
virtual void syncSolutions(Direction) override
Definition: ExternalPETScProblem.C:55
FormInitialSolution
PETSC_EXTERN PetscErrorCode FormInitialSolution(TS, Vec, void *)
Definition: PETScDiffusionFDM.C:432
ExternalPETScProblem::ExternalPETScProblem
ExternalPETScProblem(const InputParameters &params)
Definition: ExternalPETScProblem.C:25
externalPETScDiffusionFDMSolve
PETSC_EXTERN PetscErrorCode externalPETScDiffusionFDMSolve(TS, Vec, PetscReal, PetscReal)
Definition: PETScDiffusionFDM.C:99
ExternalPETScProblem::_ts
TS & _ts
PETSc solver.
Definition: ExternalPETScProblem.h:47
ExternalPETScProblem.h
registerMooseObject
registerMooseObject("ExternalPetscSolverApp", ExternalPETScProblem)
ExternalPETScProblem::_petsc_sol
Vec _petsc_sol
PETSc solver solution.
Definition: ExternalPETScProblem.h:49
ExternalPETScProblem
This is an interface to call a pure PETSc solver.
Definition: ExternalPETScProblem.h:27
ExternalPetscSolverApp
This is a demo used to demonstrate how to couple an external app through the MOOSE wrapper APP.
Definition: ExternalPetscSolverApp.h:25
validParams< ExternalPETScProblem >
InputParameters validParams< ExternalPETScProblem >()
Definition: ExternalPETScProblem.C:17
ExternalPETScProblem::externalSolve
virtual void externalSolve() override
Definition: ExternalPETScProblem.C:46