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 
12 registerMooseObject("ExternalPetscSolverApp", ExternalPETScProblem);
13 
14 template <>
15 InputParameters
17 {
18  InputParameters params = validParams<ExternalProblem>();
19  params.addRequiredParam<VariableName>("sync_variable",
20  "The variable PETSc external solution will be synced to");
21  return params;
22 }
23 
24 ExternalPETScProblem::ExternalPETScProblem(const InputParameters & params)
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  ,
31  _ts(_petsc_app.getExternalPETScTS())
32 {
33  DM da;
34  TSGetDM(_ts, &da);
35  DMCreateGlobalVector(da, &_petsc_sol);
37 }
38 #else
39 {
40  mooseError("You need to have PETSc installed to use ExternalPETScProblem");
41 }
42 #endif
43 
44 void
46 {
47 #if LIBMESH_HAVE_PETSC
48  _console << "PETSc External Solve!" << std::endl;
50 #endif
51 }
52 
53 void
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 }
This is an interface to call a pure PETSc solver.
virtual void syncSolutions(Direction) override
This is a demo used to demonstrate how to couple an external app through the MOOSE wrapper APP...
virtual void externalSolve() override
TS & _ts
PETSc solver.
PETSC_EXTERN PetscErrorCode externalPETScDiffusionFDMSolve(TS, Vec, PetscReal, PetscReal)
Vec _petsc_sol
PETSc solver solution.
registerMooseObject("ExternalPetscSolverApp", ExternalPETScProblem)
PETSC_EXTERN PetscErrorCode FormInitialSolution(TS, Vec, void *)
const VariableName & _sync_to_var_name
The name of the variable to transfer to.
ExternalPETScProblem(const InputParameters &params)
InputParameters validParams< ExternalPETScProblem >()