25 #include "grins_config.h"
27 #ifdef GRINS_HAVE_CPPUNIT
29 #include <libmesh/ignore_warnings.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/TestCase.h>
32 #include <libmesh/restore_warnings.h>
35 #include "grins_test_paths.h"
48 #include "libmesh/parsed_function.h"
49 #include "libmesh/qoi_set.h"
50 #include "libmesh/steady_solver.h"
51 #include "libmesh/mesh_refinement.h"
52 #include "libmesh/elem.h"
55 #include <libmesh/ignore_warnings.h>
83 const std::string filename = std::string(GRINS_TEST_UNIT_INPUT_SRCDIR)+
"/integrated_function_quad9.in";
86 libMesh::Point origin(0.0,0.0);
89 std::vector<libMesh::Real> theta;
91 theta.push_back(0.25);
93 for (
unsigned int t=0; t<theta.size(); t++)
95 libMesh::Real L = 3.0/std::cos(theta[t]);
96 libMesh::Real sintheta = std::sin(theta[t]);
97 libMesh::Real costheta = std::cos(theta[t]);
99 std::vector<std::string> functions;
100 std::vector<libMesh::Real> calc_answers;
103 functions.push_back(
"1");
104 calc_answers.push_back(L);
107 functions.push_back(
"x");
108 calc_answers.push_back(0.5/costheta*3.0*3.0);
109 functions.push_back(
"y");
110 calc_answers.push_back( sintheta/2.0*L*L );
113 functions.push_back(
"x*(y^2)");
114 calc_answers.push_back(0.25*std::pow(L,4)*std::pow(sintheta,2)*costheta);
115 functions.push_back(
"(4/3)*(x^3)+10");
116 calc_answers.push_back( (4.0/12.0)*pow(costheta,3)*std::pow(L,4)+10*L );
120 CPPUNIT_ASSERT_EQUAL(functions.size(), calc_answers.size());
125 for (
unsigned int i=0; i<functions.size(); i++)
129 comp_qoi.add_qoi(integ_func);
132 comp_qoi.init(*
_input,*system);
133 system->attach_qoi(&comp_qoi);
134 system->assemble_qoi();
136 for (
unsigned int i=0; i<calc_answers.size(); i++)
137 CPPUNIT_ASSERT_DOUBLES_EQUAL( calc_answers[i],
_sim->get_qoi_value(i),libMesh::TOLERANCE );
154 const std::string filename = std::string(GRINS_TEST_UNIT_INPUT_SRCDIR)+
"/integrated_function_quad9.in";
157 libMesh::Point origin(0.0,0.0);
160 std::vector<libMesh::Real> theta;
161 theta.push_back(0.0);
162 theta.push_back(0.25);
164 for (
unsigned int t=0; t<theta.size(); t++)
166 libMesh::Real costheta = std::cos(theta[t]);
167 libMesh::Real sintheta = std::sin(theta[t]);
168 libMesh::Real L = 3.0/costheta;
170 std::vector<std::string> functions;
171 std::vector<libMesh::Real> calc_answers;
172 std::vector<libMesh::Real> tolerance;
173 std::vector<bool> converged;
176 functions.push_back(
"sin(x)+cos(y)");
179 calc_answers.push_back( L-std::cos(L)+1.0 );
181 calc_answers.push_back( ( (std::sin(L*sintheta))/sintheta ) - ( (std::cos(L*costheta)-1.0)/costheta ) );
183 tolerance.push_back(1e-9);
184 converged.push_back(
false);
187 functions.push_back(
"exp(x)");
188 calc_answers.push_back( (std::exp(3.0) - 1.0) / costheta);
189 tolerance.push_back(1e-8);
190 converged.push_back(
false);
192 std::vector<std::vector<libMesh::Real> > errors(functions.size());
193 std::vector<std::vector<libMesh::Real> > h_vals(functions.size());
196 GRINS::SharedPtr<libMesh::EquationSystems> es =
_sim->get_equation_system();
198 CPPUNIT_ASSERT_EQUAL(functions.size(), calc_answers.size());
207 GRINS::SharedPtr<GRINS::RayfireMesh> ref_rayfire(
new GRINS::RayfireMesh(origin,theta[t]));
208 ref_rayfire->init( system->get_mesh() );
210 for (
unsigned int i=0; i<functions.size(); i++)
214 comp_qoi.add_qoi(integ_func);
217 comp_qoi.init(*
_input,*system);
218 system->attach_qoi(&comp_qoi);
220 libMesh::MeshRefinement mr(system->get_mesh());
222 unsigned int num_converged = 0;
223 unsigned int iter = 0;
226 unsigned int max_iter = 7;
232 std::vector<libMesh::dof_id_type> elems_in_rayfire;
233 ref_rayfire->elem_ids_in_rayfire(elems_in_rayfire);
235 system->assemble_qoi();
238 libMesh::Real h = -1.0;
239 for (
unsigned int i=0; i<elems_in_rayfire.size(); i++)
241 libMesh::Real l = (ref_rayfire->map_to_rayfire_elem(elems_in_rayfire[i]))->length(0,1);
246 CPPUNIT_ASSERT(h > 0.0);
249 for (
unsigned int i=0; i<calc_answers.size(); i++)
253 libMesh::Real err = std::abs(
_sim->get_qoi_value(i) - calc_answers[i] );
254 if (err < tolerance[i])
258 h_vals[i].push_back(std::log10(h));
259 errors[i].push_back(std::log10(err));
263 h_vals[i].push_back(std::log10(h));
264 errors[i].push_back(std::log10(err));
270 if (num_converged < calc_answers.size())
272 if (iter++ > max_iter+1 )
273 libmesh_error_msg(
"Exceeded maximum iterations");
275 for (
unsigned int i=0; i<elems_in_rayfire.size(); i++)
276 system->get_mesh().elem_ref(elems_in_rayfire[i]).set_refinement_flag(libMesh::Elem::RefinementState::REFINE);
278 mr.refine_elements();
281 for (
unsigned int i=0; i<elems_in_rayfire.size(); i++)
282 CPPUNIT_ASSERT( !( system->get_mesh().elem_ref(elems_in_rayfire[i]).active() ) );
285 ref_rayfire->reinit(system->get_mesh());
288 libMesh::DifferentiableQoI* diff_qoi = system->get_qoi();
295 }
while(num_converged < calc_answers.size());
298 for (
unsigned int i=0; i<functions.size(); i++)
308 const std::string filename = std::string(GRINS_TEST_UNIT_INPUT_SRCDIR)+
"/integrated_function_qoi_quad9.in";
311 libMesh::Real theta = 0.25;
312 libMesh::Real L = 10.0/std::cos(theta);
313 libMesh::Real costheta = std::cos(theta);
317 libMesh::Real calc_answer = (4.0/12.0)*pow(costheta,3)*std::pow(L,4)+10*L;
321 CPPUNIT_ASSERT_DOUBLES_EQUAL( calc_answer,
_sim->get_qoi_value(0),libMesh::TOLERANCE );
327 const std::string filename = std::string(GRINS_TEST_UNIT_INPUT_SRCDIR)+
"/integrated_function_quad9.in";
330 libMesh::Point origin(0.0,0.0);
331 libMesh::Real theta = 0.0;
334 GRINS::SharedPtr<libMesh::EquationSystems> es =
_sim->get_equation_system();
336 libMesh::MeshRefinement mr(system->get_mesh());
344 libMesh::Real costheta = std::cos(theta);
347 std::string
function =
"x";
348 libMesh::Real calc_answer = 0.5/costheta*3.0*3.0;
354 system->attach_qoi(&comp_qoi);
357 system->assemble_qoi();
360 CPPUNIT_ASSERT_DOUBLES_EQUAL( calc_answer,
_sim->get_qoi_value(0),libMesh::TOLERANCE );
363 libMesh::DifferentiableQoI* diff_qoi = system->get_qoi();
368 std::vector<libMesh::dof_id_type> elems_in_rayfire;
369 integrated_func->get_rayfire().elem_ids_in_rayfire(elems_in_rayfire);
370 unsigned int num_rayfire_elems = elems_in_rayfire.size();
371 CPPUNIT_ASSERT_EQUAL((
unsigned int)3,num_rayfire_elems);
373 for (
unsigned int i=0; i<elems_in_rayfire.size(); i++)
374 system->get_mesh().elem_ref(elems_in_rayfire[i]).set_refinement_flag(libMesh::Elem::RefinementState::REFINE);
376 mr.refine_elements();
383 system->assemble_qoi();
386 std::vector<libMesh::dof_id_type> refined_elems_in_rayfire;
387 integrated_func->get_rayfire().elem_ids_in_rayfire(refined_elems_in_rayfire);
388 unsigned int num_refined_rayfire_elems = refined_elems_in_rayfire.size();
389 CPPUNIT_ASSERT_EQUAL((
unsigned int)6,num_refined_rayfire_elems);
390 CPPUNIT_ASSERT_DOUBLES_EQUAL( calc_answer,
_sim->get_qoi_value(0),libMesh::TOLERANCE );
395 GRINS::SharedPtr<GRINS::Simulation>
_sim;
401 _input.reset(
new GetPot(filename));
403 const char*
const argv =
"unit_driver";
404 GetPot empty_command_line( (
const int)1,&argv );
419 #endif // GRINS_HAVE_CPPUNIT
CPPUNIT_TEST_SUITE_REGISTRATION(AntiochAirNASA9ThermoTest)
CPPUNIT_TEST_SUITE(IntegratedFunctionTest)
CPPUNIT_TEST(test_exact_answer)
libMesh::Parallel::Communicator * TestCommWorld
const QoIBase & get_qoi(unsigned int qoi_index) const
void test_convergence()
A quartic convergence rate is identified by graphing log(error) vs.
void qoi_from_input_file()
void reinit_through_system()
Here, we use CompositeQoI::reinit() to reinitialize the rayfire post-refinement.
virtual void reinit()
Override FEMSystem::reinit.
void check_convergence_rate(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
Check the convergence rate of the supplied data to within the given absolute tolerance.
void test_exact_answer()
Functions that can be integrated exactly using Gauss Quadrature.
Interface with libMesh for solving Multiphysics problems.
virtual void add_qoi(const QoIBase &qoi)
GRINS::SharedPtr< GRINS::Simulation > _sim
virtual void init(const GetPot &input, const MultiphysicsSystem &system)
Method to allow QoI to cache any system information needed for QoI calculation, for example...
static void clear()
Clears the var_map()
GRINS::SharedPtr< GetPot > _input
void init_sim(const std::string &filename)
Initialize the GetPot and Simulation class objects.
virtual void reinit(MultiphysicsSystem &system)
Reinitialize qoi.