4 #include <deal.II/base/conditional_ostream.h> 6 #include <deal.II/dofs/dof_tools.h> 8 #include <deal.II/grid/grid_generator.h> 9 #include <deal.II/grid/grid_refinement.h> 10 #include <deal.II/grid/grid_tools.h> 12 #include <deal.II/numerics/vector_tools.h> 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" 24 const double STEPSIZE = 1e-7;
25 const double TOLERANCE = 1e-4;
28 using Triangulation = dealii::Triangulation<PHILIP_DIM>;
30 using Triangulation = dealii::parallel::distributed::Triangulation<PHILIP_DIM>;
33 template <
int dim,
int nspecies,
int nstate,
typename real>
48 template <
int dim,
int nspecies,
int nstate>
51 dealii::LinearAlgebra::distributed::Vector<double> solution_no_ghost;
57 int main(
int argc,
char *argv[])
60 const int dim = PHILIP_DIM;
61 const int nspecies = 1;
63 int fail_bool =
false;
66 dealii::Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
67 const int this_mpi_process = dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD);
68 dealii::ConditionalOStream
pcout(std::cout, this_mpi_process==0);
71 dealii::ParameterHandler parameter_handler;
77 const unsigned poly_degree = 1;
80 std::shared_ptr<Triangulation> grid = std::make_shared<Triangulation>(
84 typename dealii::Triangulation<dim>::MeshSmoothing(
85 dealii::Triangulation<dim>::smoothing_on_refinement |
86 dealii::Triangulation<dim>::smoothing_on_coarsening));
88 const unsigned int n_refinements = 2;
91 const bool colorize =
true;
93 dealii::GridGenerator::hyper_cube(*grid, left, right, colorize);
94 grid->refine_global(n_refinements);
95 const double random_factor = 0.2;
96 const bool keep_boundary =
false;
97 if (random_factor > 0.0) dealii::GridTools::distort_random (random_factor, *grid, keep_boundary);
99 pcout <<
"Grid generated and refined" << std::endl;
103 pcout <<
"dg created" << std::endl;
105 dg->allocate_system();
106 pcout <<
"dg allocated" << std::endl;
108 const int n_refine = 2;
109 for (
int i=0; i<n_refine;i++) {
110 dg->high_order_grid->prepare_for_coarsening_and_refinement();
111 grid->prepare_coarsening_and_refinement();
112 unsigned int icell = 0;
113 for (
auto cell = grid->begin_active(); cell!=grid->end(); ++cell) {
115 if (!cell->is_locally_owned())
continue;
116 if (icell < grid->n_global_active_cells()/2) {
117 cell->set_refine_flag();
120 grid->execute_coarsening_and_refinement();
121 bool mesh_out = (i==n_refine-1);
122 dg->high_order_grid->execute_coarsening_and_refinement(mesh_out);
124 dg->allocate_system ();
127 using FadType = Sacado::Fad::DFad<double>;
129 pcout <<
"Physics created" << std::endl;
132 initialize_perturbed_solution(*dg, *physics_double);
133 pcout <<
"solution initialized" << std::endl;
136 pcout << std::endl <<
"Starting AD... " << std::endl;
138 dg->solution.add(1.0);
141 dealii::LinearAlgebra::distributed::Vector<double>
dIdw = l2norm.
dIdw;
142 dealii::LinearAlgebra::distributed::Vector<double>
dIdX = l2norm.
dIdX;
144 pcout << std::endl <<
"Overall error (its ok since we added 1.0 to the target solution): " << l2error_mpi_sum2 << std::endl;
147 pcout << std::endl <<
"Starting FD dIdW... " << std::endl;
151 pcout << std::endl <<
"Starting FD dIdX... " << std::endl;
155 dealii::LinearAlgebra::distributed::Vector<double> dIdw_difference =
dIdw;
156 dIdw_difference -= dIdw_FD;
157 double dIdW_L2_diff = dIdw_difference.l2_norm();
158 pcout <<
"L2 norm of FD-AD dIdW: " << dIdW_L2_diff << std::endl;
161 dealii::LinearAlgebra::distributed::Vector<double> dIdX_difference =
dIdX;
162 dIdX_difference -= dIdX_FD;
163 double dIdX_L2_diff = dIdX_difference.l2_norm();
164 pcout <<
"L2 norm of FD-AD dIdX: " << dIdX_L2_diff << std::endl;
166 fail_bool = dIdW_L2_diff > TOLERANCE || dIdX_L2_diff > TOLERANCE;
dealii::LinearAlgebra::distributed::Vector< real > evaluate_dIdw_finiteDifferences(DGBase< dim, nspecies, real, dealii::parallel::distributed::Triangulation< dim > > &dg, const PHiLiP::Physics::PhysicsBase< dim, nspecies, nstate, real > &physics, const double stepsize)
TargetFunctional base class.
Files for the baseline physics.
std::shared_ptr< DGBase< dim, nspecies, real, dealii::parallel::distributed::Triangulation< dim > > > dg
Smart pointer to DGBase.
std::shared_ptr< ManufacturedSolutionFunction< dim, nspecies, real > > manufactured_solution_function
Manufactured solution function.
Main parameter class that contains the various other sub-parameter classes.
dealii::LinearAlgebra::distributed::Vector< real > evaluate_dIdX_finiteDifferences(DGBase< dim, nspecies, real, dealii::parallel::distributed::Triangulation< dim > > &dg, const PHiLiP::Physics::PhysicsBase< dim, nspecies, nstate, real > &physics, const double stepsize)
TargetFunctional(std::shared_ptr< DGBase< dim, nspecies, real >> dg_input, const bool uses_solution_values=true, const bool uses_solution_gradient=true)
Constructor.
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.
dealii::LinearAlgebra::distributed::Vector< real > dIdX
Vector for storing the derivatives with respect to each grid DoF.
dealii::DoFHandler< dim > dof_handler
Finite Element Collection to represent the high-order grid.
const bool uses_solution_values
Will evaluate solution values at quadrature points.
dealii::IndexSet locally_owned_dofs
Locally own degrees of freedom.
const bool uses_solution_gradient
Will evaluate solution gradient at quadrature points.
static std::shared_ptr< DGBase< dim, nspecies, 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.
dealii::LinearAlgebra::distributed::Vector< double > solution
Current modal coefficients of the solution.
L2_Norm_Functional(std::shared_ptr< PHiLiP::DGBase< dim, nspecies, real >> dg_input, const bool uses_solution_values=true, const bool uses_solution_gradient=false)
Constructor.
void parse_parameters(dealii::ParameterHandler &prm)
Retrieve parameters from dealii::ParameterHandler.
dealii::ConditionalOStream pcout
Parallel std::cout that only outputs on mpi_rank==0.
Sacado::Fad::DFad< real > FadType
Sacado AD type for first derivatives.
static std::shared_ptr< PhysicsBase< dim, nspecies, nstate, real > > create_Physics(const Parameters::AllParameters *const parameters_input, std::shared_ptr< ModelBase< dim, nspecies, nstate, real > > model_input=nullptr)
Factory to return the correct physics given input file.
static void declare_parameters(dealii::ParameterHandler &prm)
Declare parameters that can be set as inputs and set up the default options.
DGBase is independent of the number of state variables.
dealii::LinearAlgebra::distributed::Vector< real > dIdw
Vector for storing the derivatives with respect to each solution DoF.