[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
target_functional_dw_finiteDifferences.cpp
1 #include <stdlib.h> /* srand, rand */
2 #include <iostream>
3 
4 #include <deal.II/base/conditional_ostream.h>
5 
6 #include <deal.II/dofs/dof_tools.h>
7 
8 #include <deal.II/grid/grid_generator.h>
9 #include <deal.II/grid/grid_refinement.h>
10 #include <deal.II/grid/grid_tools.h>
11 
12 #include <deal.II/numerics/vector_tools.h>
13 
14 #include <Sacado.hpp>
15 
16 #include "physics/physics_factory.h"
17 #include "physics/manufactured_solution.h"
18 #include "parameters/all_parameters.h"
19 #include "parameters/parameters.h"
20 #include "ode_solver/ode_solver_factory.h"
21 #include "dg/dg_factory.hpp"
22 #include "functional/target_functional.h"
23 
24 const double STEPSIZE = 1e-7;
25 const double TOLERANCE = 1e-4;
26 
27 #if PHILIP_DIM==1
28  using Triangulation = dealii::Triangulation<PHILIP_DIM>;
29 #else
30  using Triangulation = dealii::parallel::distributed::Triangulation<PHILIP_DIM>;
31 #endif
32 
33 template <int dim, int nstate, typename real>
34 class L2_Norm_Functional : public PHiLiP::TargetFunctional<dim, nstate, real>
35 {
36  public:
39  std::shared_ptr<PHiLiP::DGBase<dim,real>> dg_input,
40  const bool uses_solution_values = true,
41  const bool uses_solution_gradient = false)
43  {}
44 
45 };
46 
47 
48 template <int dim, int nstate>
49 void initialize_perturbed_solution(PHiLiP::DGBase<dim,double> &dg, const PHiLiP::Physics::PhysicsBase<dim,nstate,double> &physics)
50 {
51  dealii::LinearAlgebra::distributed::Vector<double> solution_no_ghost;
52  solution_no_ghost.reinit(dg.locally_owned_dofs, MPI_COMM_WORLD);
53  dealii::VectorTools::interpolate(dg.dof_handler, *physics.manufactured_solution_function, solution_no_ghost);
54  dg.solution = solution_no_ghost;
55 }
56 
57 int main(int argc, char *argv[])
58 {
59 
60  const int dim = PHILIP_DIM;
61  const int nstate = 1;
62  int fail_bool = false;
63 
64  // Initializing MPI
65  dealii::Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
66  const int this_mpi_process = dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD);
67  dealii::ConditionalOStream pcout(std::cout, this_mpi_process==0);
68 
69  // Initializing parameter handling
70  dealii::ParameterHandler parameter_handler;
72  PHiLiP::Parameters::AllParameters all_parameters;
73  all_parameters.parse_parameters(parameter_handler);
74 
75  // polynomial order and mesh size
76  const unsigned poly_degree = 1;
77 
78  // creating the grid
79  std::shared_ptr<Triangulation> grid = std::make_shared<Triangulation>(
80 #if PHILIP_DIM!=1
81  MPI_COMM_WORLD,
82 #endif
83  typename dealii::Triangulation<dim>::MeshSmoothing(
84  dealii::Triangulation<dim>::smoothing_on_refinement |
85  dealii::Triangulation<dim>::smoothing_on_coarsening));
86 
87  const unsigned int n_refinements = 2;
88  double left = 0.0;
89  double right = 2.0;
90  const bool colorize = true;
91 
92  dealii::GridGenerator::hyper_cube(*grid, left, right, colorize);
93  grid->refine_global(n_refinements);
94  const double random_factor = 0.2;
95  const bool keep_boundary = false;
96  if (random_factor > 0.0) dealii::GridTools::distort_random (random_factor, *grid, keep_boundary);
97 
98  pcout << "Grid generated and refined" << std::endl;
99 
100  // creating the dg
101  std::shared_ptr < PHiLiP::DGBase<dim, double> > dg = PHiLiP::DGFactory<dim,double>::create_discontinuous_galerkin(&all_parameters, poly_degree, grid);
102  pcout << "dg created" << std::endl;
103 
104  dg->allocate_system();
105  pcout << "dg allocated" << std::endl;
106 
107  const int n_refine = 2;
108  for (int i=0; i<n_refine;i++) {
109  dg->high_order_grid->prepare_for_coarsening_and_refinement();
110  grid->prepare_coarsening_and_refinement();
111  unsigned int icell = 0;
112  for (auto cell = grid->begin_active(); cell!=grid->end(); ++cell) {
113  icell++;
114  if (!cell->is_locally_owned()) continue;
115  if (icell < grid->n_global_active_cells()/2) {
116  cell->set_refine_flag();
117  }
118  }
119  grid->execute_coarsening_and_refinement();
120  bool mesh_out = (i==n_refine-1);
121  dg->high_order_grid->execute_coarsening_and_refinement(mesh_out);
122  }
123  dg->allocate_system ();
124 
125  // manufactured solution function
126  using FadType = Sacado::Fad::DFad<double>;
127  std::shared_ptr <PHiLiP::Physics::PhysicsBase<dim,nstate,double>> physics_double = PHiLiP::Physics::PhysicsFactory<dim, nstate, double>::create_Physics(&all_parameters);
128  pcout << "Physics created" << std::endl;
129 
130  // performing the interpolation for the intial conditions
131  initialize_perturbed_solution(*dg, *physics_double);
132  pcout << "solution initialized" << std::endl;
133 
134  // evaluating the derivative (using SACADO)
135  pcout << std::endl << "Starting AD... " << std::endl;
136  L2_Norm_Functional<dim,nstate,double> l2norm(dg,true,false);
137  dg->solution.add(1.0);
138  double l2error_mpi_sum2 = std::sqrt(l2norm.evaluate_functional(true,true));
139 
140  dealii::LinearAlgebra::distributed::Vector<double> dIdw = l2norm.dIdw;
141  dealii::LinearAlgebra::distributed::Vector<double> dIdX = l2norm.dIdX;
142 
143  pcout << std::endl << "Overall error (its ok since we added 1.0 to the target solution): " << l2error_mpi_sum2 << std::endl;
144 
145  // evaluating the derivative (using finite differences)
146  pcout << std::endl << "Starting FD dIdW... " << std::endl;
147  dealii::LinearAlgebra::distributed::Vector<double> dIdw_FD = l2norm.evaluate_dIdw_finiteDifferences(*dg, *physics_double, STEPSIZE);
148  // dIdw_FD.print(std::cout);
149 
150  pcout << std::endl << "Starting FD dIdX... " << std::endl;
151  dealii::LinearAlgebra::distributed::Vector<double> dIdX_FD = l2norm.evaluate_dIdX_finiteDifferences(*dg, *physics_double, STEPSIZE);
152 
153  // comparing the results and checking its within the specified tolerance
154  dealii::LinearAlgebra::distributed::Vector<double> dIdw_difference = dIdw;
155  dIdw_difference -= dIdw_FD;
156  double dIdW_L2_diff = dIdw_difference.l2_norm();
157  pcout << "L2 norm of FD-AD dIdW: " << dIdW_L2_diff << std::endl;
158 
159  // comparing the results and checking its within the specified tolerance
160  dealii::LinearAlgebra::distributed::Vector<double> dIdX_difference = dIdX;
161  dIdX_difference -= dIdX_FD;
162  double dIdX_L2_diff = dIdX_difference.l2_norm();
163  pcout << "L2 norm of FD-AD dIdX: " << dIdX_L2_diff << std::endl;
164 
165  fail_bool = dIdW_L2_diff > TOLERANCE || dIdX_L2_diff > TOLERANCE;
166  return fail_bool;
167 }
168 
const bool uses_solution_gradient
Will evaluate solution gradient at quadrature points.
Definition: functional.h:315
dealii::LinearAlgebra::distributed::Vector< real > dIdX
Vector for storing the derivatives with respect to each grid DoF.
Definition: functional.h:121
TargetFunctional base class.
const bool uses_solution_values
Will evaluate solution values at quadrature points.
Definition: functional.h:314
dealii::LinearAlgebra::distributed::Vector< real > dIdw
Vector for storing the derivatives with respect to each solution DoF.
Definition: functional.h:119
Files for the baseline physics.
Definition: ADTypes.hpp:10
Main parameter class that contains the various other sub-parameter classes.
dealii::LinearAlgebra::distributed::Vector< double > solution
Current modal coefficients of the solution.
Definition: dg_base.hpp:409
TargetFunctional(std::shared_ptr< DGBase< dim, real >> dg_input, const bool uses_solution_values=true, const bool uses_solution_gradient=true)
Constructor.
dealii::IndexSet locally_owned_dofs
Locally own degrees of freedom.
Definition: dg_base.hpp:398
L2_Norm_Functional(std::shared_ptr< PHiLiP::DGBase< dim, real >> dg_input, const bool uses_solution_values=true, const bool uses_solution_gradient=false)
Constructor.
Sacado::Fad::DFad< real > FadType
Sacado AD type for first derivatives.
static std::shared_ptr< DGBase< dim, real, MeshType > > create_discontinuous_galerkin(const Parameters::AllParameters *const parameters_input, const unsigned int degree, const unsigned int max_degree_input, const unsigned int grid_degree_input, const std::shared_ptr< Triangulation > triangulation_input)
Creates a derived object DG, but returns it as DGBase.
Definition: dg_factory.cpp:10
dealii::LinearAlgebra::distributed::Vector< real > evaluate_dIdw_finiteDifferences(DGBase< dim, real, dealii::parallel::distributed::Triangulation< dim > > &dg, const PHiLiP::Physics::PhysicsBase< dim, nstate, real > &physics, const double stepsize)
Definition: functional.cpp:984
dealii::LinearAlgebra::distributed::Vector< real > evaluate_dIdX_finiteDifferences(DGBase< dim, real, dealii::parallel::distributed::Triangulation< dim > > &dg, const PHiLiP::Physics::PhysicsBase< dim, nstate, real > &physics, const double stepsize)
static std::shared_ptr< PhysicsBase< dim, nstate, real > > create_Physics(const Parameters::AllParameters *const parameters_input, std::shared_ptr< ModelBase< dim, nstate, real > > model_input=nullptr)
Factory to return the correct physics given input file.
void parse_parameters(dealii::ParameterHandler &prm)
Retrieve parameters from dealii::ParameterHandler.
dealii::DoFHandler< dim > dof_handler
Finite Element Collection to represent the high-order grid.
Definition: dg_base.hpp:652
static void declare_parameters(dealii::ParameterHandler &prm)
Declare parameters that can be set as inputs and set up the default options.
std::shared_ptr< DGBase< dim, real, dealii::parallel::distributed::Triangulation< dim > > > dg
Smart pointer to DGBase.
Definition: functional.h:50
DGBase is independent of the number of state variables.
Definition: dg_base.hpp:82
std::shared_ptr< ManufacturedSolutionFunction< dim, real > > manufactured_solution_function
Manufactured solution function.
Definition: physics.h:71
virtual real evaluate_functional(const bool compute_dIdW=false, const bool compute_dIdX=false, const bool compute_d2I=false)
Evaluates the functional derivative with respect to the solution variable.
Definition: functional.cpp:795
dealii::ConditionalOStream pcout
Parallel std::cout that only outputs on mpi_rank==0.
Definition: functional.h:317