/***********************************************************************//** * @brief Test unbinned optimizer ***************************************************************************/ void TestGCTAOptimize::test_unbinned_optimizer(void) { // Declare observations GObservations obs; GCTAObservation run; // Load unbinned CTA observation test_try("Load unbinned CTA observation"); try { run.load_unbinned(cta_events); run.response(cta_irf,cta_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(cta_model_xml); // Perform LM optimization double fit_results[] = {83.6331, 0, 22.0145, 0, 5.656246512e-16, 1.91458426e-17, -2.484100472, -0.02573396361, 300000, 0, 1, 0, 2.993705325, 0.03572658413, 6.490832107e-05, 1.749021094e-06, -1.833584022, -0.01512223495, 1000000, 0, 1, 0}; test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(100); obs.optimize(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar& par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.real_value(), fit_results[j++], 5.0e-5, msg); test_value(par.real_error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test binned optimizer ***************************************************************************/ void TestGCTAOptimize::test_binned_optimizer(void) { // Declare observations GObservations obs; GCTAObservation run; // Load binned CTA observation test_try("Load binned CTA observation"); try { run.load_binned(cta_cntmap); run.response(cta_irf,cta_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(cta_model_xml); // Perform LM optimization double fit_results[] = {83.6331, 0, 22.0145, 0, 5.616410411e-16, 1.904730785e-17, -2.481781246, -0.02580905077, 300000, 0, 1, 0, 2.933677595, 0.06639644824, 6.550723074e-05, 1.945714239e-06, -1.833781187, -0.0161464076, 1000000, 0, 1, 0}; test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(100); obs.optimize(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar& par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.real_value(), fit_results[j++], 5.0e-5, msg); test_value(par.real_error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/*************************************************************************** * @brief GSkyRegionCircle_construct ***************************************************************************/ void TestGSky::test_GSkyRegionCircle_construct(void) { // Define region for comparison GSkyDir refdir_radeczerozero = GSkyDir(); refdir_radeczerozero.radec_deg(0,0); double refradius = 10.; // Test constructing: test_try("Test constructor"); try { GSkyRegionCircle circle(refdir_radeczerozero,refradius); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test constructing with radius 0 test_try("Test constructor2"); try { GSkyRegionCircle circle(refdir_radeczerozero,-1); test_try_failure(); } catch (GException::invalid_argument &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test constructing with radius 0 test_try("Test radius assignment after"); try { GSkyRegionCircle circle(refdir_radeczerozero,refradius); circle.radius(-1.0); test_try_failure(); } catch (GException::invalid_argument &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Check radius assignment GSkyRegionCircle refregion(refdir_radeczerozero,refradius); double refradius_check = refregion.radius(); test_value(refradius,refradius_check,1.0e-10, "Test radius assignment"); // Check solid angle assignment double solidangle_check = refregion.solidangle(); double solidangle = 2*gammalib::pi*(1- std::cos(refradius /180 * gammalib::pi)); test_value(solidangle_check,solidangle,1.0e-10, "Test solid angle assignment"); //exit test return; }
/***********************************************************************//** * @brief Test matrix functions * * Tests matrix functions. ***************************************************************************/ void TestGSparseMatrix::matrix_functions(void) { // Minimum double min = m_test.min(); // Check mimimum double value = 0.0; test_value(min, value, 0.0, "Test minimum function"); // Maximum double max = m_test.max(); // Check maximum value = g_matrix[0]; for (int i = 1; i < g_elements; ++i) { if (g_matrix[i] > value) { value = g_matrix[i]; } } test_value(max, value, 0.0, "Test maximum function"); // Sum double sum = m_test.sum(); // Check sum value = 0.0; for (int i = 0; i < g_elements; ++i) { value += g_matrix[i]; } test_value(sum, value, 1.0e-20, "Test sum function"); // Transpose function GSparseMatrix test1 = transpose(m_test); test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix_trans(test1, 1.0, 0.0), "Test transpose(GSparseMatrix) function", "Unexpected transposed matrix:\n"+test1.print()); // Transpose method test1 = m_test; test1.transpose(); test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix_trans(test1, 1.0, 0.0), "Test GSparseMatrix.transpose() method", "Unexpected transposed matrix:\n"+test1.print()); // Convert to general matrix GSparseMatrix test2 = GSparseMatrix(m_test); test_assert(check_matrix(m_test), "Test source matrix"); test_assert(check_matrix(test2, 1.0, 0.0), "Test GSparseMatrix(GSparseMatrix) constructor", "Unexpected GSparseMatrix:\n"+test2.print()); // Return return; }
/***********************************************************************//** * @brief Test binned optimizer * * @param[in] datadir Directory of test data. * @param[in] irf Instrument response function. * @param[in] fit_results Expected fit result. * * Verifies the ability optimize binned Fermi/LAT data. ***************************************************************************/ void TestGLATOptimize::test_one_binned_optimizer(const std::string& datadir, const std::string& irf, const double* fit_results) { // Set filenames std::string lat_srcmap = datadir+"/srcmap.fits"; std::string lat_expmap = datadir+"/binned_expmap.fits"; std::string lat_ltcube = datadir+"/ltcube.fits"; std::string lat_model_xml = datadir+"/source_model.xml"; // Setup GObservations for optimizing GObservations obs; GLATObservation run; test_try("Setup for optimization"); try { run.load_binned(lat_srcmap, lat_expmap, lat_ltcube); run.response(irf, lat_caldb); obs.append(run); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Load models from XML file obs.models(lat_model_xml); // Setup LM optimizer test_try("Perform LM optimization"); try { GOptimizerLM opt; opt.max_iter(1000); obs.optimize(opt); obs.errors(opt); test_try_success(); for (int i = 0, j = 0; i < obs.models().size(); ++i) { const GModel* model = obs.models()[i]; for (int k = 0; k < model->size(); ++k) { GModelPar par = (*model)[k]; std::string msg = "Verify optimization result for " + par.print(); test_value(par.value(), fit_results[j++], 5.0e-5, msg); test_value(par.error(), fit_results[j++], 5.0e-5, msg); } } } catch (std::exception &e) { test_try_failure(e); } // Exit test return; }
/***********************************************************************//** * @brief Test value assignment ***************************************************************************/ void TestGMatrixSymmetric::assign_values(void) { // Setup 3x3 matrix GMatrixSymmetric test(3,3); // Assignment individual values for (int i = 0; i < 3; ++i) { for (int k = 0; k < 3; ++k) { double value = i*2.0 + k*2.0; test(i,k) = value; } } // Check assignment of individual values for (int i = 0; i < 3; ++i) { for (int k = 0; k < 3; ++k) { double value = i*2.0 + k*2.0; test_value(test(i,k), value, 1.0e-10, "Test matrix element assignment"); } } // Check value assignment const double ref = 37.89; test = ref; for (int i = 0; i < 3; ++i) { for (int k = 0; k < 3; ++k) { test_value(test(i,k), ref, 1.0e-10, "Test matrix element assignment"); } } // Verify range checking #ifdef G_RANGE_CHECK test_try("Verify range checking"); try { test.at(3,3) = 1.0; test_try_failure("Expected GException::out_of_range exception."); } catch (GException::out_of_range &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } #endif // Return return; }
bool test_object_initz( struct semantic* semantic, struct var* var ) { struct multi_value_test test; init_multi_value_test( &test, var->dim, var->type, var->is_constant_init, false ); if ( var->initial->multi ) { if ( ! ( var->dim || ! var->type->primitive ) ) { struct multi_value* multi_value = ( struct multi_value* ) var->initial; s_diag( semantic, DIAG_POS_ERR, &multi_value->pos, "using brace initializer on scalar variable" ); s_bail( semantic ); } bool resolved = test_multi_value( semantic, &test, ( struct multi_value* ) var->initial ); if ( ! resolved ) { return false; } // Update size of implicit dimension. if ( var->dim && ! var->dim->size_node ) { var->dim->size = test.count; } } else { bool resolved = test_value( semantic, &test, var->dim, var->type, ( struct value* ) var->initial ); if ( ! resolved ) { return false; } } var->initial_has_str = test.has_string; return true; }
/***********************************************************************//** * @brief Test CTA psf computation * * The Psf computation is tested by integrating numerically the Psf * function. Integration is done in a rather simplistic way, by stepping * radially away from the centre. The integration is done for a set of * energies from 0.1-10 TeV. ***************************************************************************/ void TestGCTAResponse::test_response_psf(void) { // Load response GCTAResponse rsp; rsp.caldb(cta_caldb); rsp.load(cta_irf); // Integrate Psf GEnergy eng; for (double e = 0.1; e < 10.0; e *= 2.0) { eng.TeV(e); double r = 0.0; double dr = 0.001; int steps = int(1.0/dr); double sum = 0.0; for (int i = 0; i < steps; ++i) { r += dr; sum += rsp.psf(r*deg2rad, 0.0, 0.0, 0.0, 0.0, eng.log10TeV()) * twopi * std::sin(r*deg2rad) * dr*deg2rad; } test_value(sum, 1.0, 0.001, "PSF integration for "+eng.print()); } // Return return; }
/***********************************************************************//** * @brief Test CTA Npred computation * * Tests the Npred computation for the diffuse source model. This is done * by loading the model from the XML file and by calling the * GCTAObservation::npred method which in turn calls the * GCTAResponse::npred_diffuse method. The test takes a few seconds. ***************************************************************************/ void TestGCTAResponse::test_response_npred_diffuse(void) { // Set reference value double ref = 11212.26274; // Set parameters double src_ra = 201.3651; double src_dec = -43.0191; double roi_rad = 4.0; // Setup ROI centred on Cen A with a radius of 4 deg GCTARoi roi; GCTAInstDir instDir; instDir.radec_deg(src_ra, src_dec); roi.centre(instDir); roi.radius(roi_rad); // Setup pointing on Cen A GSkyDir skyDir; skyDir.radec_deg(src_ra, src_dec); GCTAPointing pnt; pnt.dir(skyDir); // Setup dummy event list GGti gti; GEbounds ebounds; GTime tstart(0.0); GTime tstop(1800.0); GEnergy emin; GEnergy emax; emin.TeV(0.1); emax.TeV(100.0); gti.append(tstart, tstop); ebounds.append(emin, emax); GCTAEventList events; events.roi(roi); events.gti(gti); events.ebounds(ebounds); // Setup dummy CTA observation GCTAObservation obs; obs.ontime(1800.0); obs.livetime(1600.0); obs.deadc(1600.0/1800.0); obs.response(cta_irf, cta_caldb); obs.events(&events); obs.pointing(pnt); // Load models for Npred computation GModels models(cta_rsp_xml); // Perform Npred computation double npred = obs.npred(models, NULL); // Test Npred test_value(npred, ref, 1.0e-5, "Diffuse Npred computation"); // Return return; }
int main(void) { int i,j,k; pmemset = memset; for (i = 0; i < 16; i++) for (j = 0; j < 64; j++) test_align(i,j); test_value('c'); test_value(0); test_value(-1); test_value(-5); test_value(0xab); return t_status; }
void tb::entry() { cout << "Begin Simulation" << endl; reset_sig = 1; cont1 = 0; i1 = 0; i2 = 0; single_cycle; reset_sig = 0; i1 = 5; single_cycle; single_cycle; single_cycle; single_cycle; single_cycle; single_cycle; set_value(cont1,1); single_cycle; single_cycle; test_value(o1,2); test_value(o2,3); // 2nd iteration. Test 'else' clause. i1 = 0; i2 = 0; cont1 = 0; single_cycle; single_cycle; single_cycle; single_cycle; test_value(o1,2); test_value(o2,3); long_wait; cout << "End Simulation" << endl; sc_stop(); }
void test_cursor( Cursor n , std::string const& value , size_t arity , size_t height , size_t level ) { EXPECT_EQ( n.size() , arity ); EXPECT_EQ( n.height() , height ); EXPECT_EQ( n.level() , level ); test_value( *n , value ); EXPECT_TRUE( n.valid() ); EXPECT_FALSE( n.invalid() ); }
/***********************************************************************//** * @brief Test CTA npsf computation ***************************************************************************/ void TestGCTAResponse::test_response_npsf(void) { // Setup CTA response GCTAResponse rsp; rsp.caldb(cta_caldb); rsp.load(cta_irf); // Setup npsf computation GSkyDir srcDir; GEnergy srcEng; GTime srcTime; GCTAPointing pnt; GCTARoi roi; GCTAInstDir instDir; instDir.radec_deg(0.0, 0.0); roi.centre(instDir); roi.radius(2.0); srcEng.TeV(0.1); // Test PSF centred on ROI srcDir.radec_deg(0.0, 0.0); double npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi); test_value(npsf, 1.0, 1.0e-3, "PSF(0,0) integration"); // Test PSF offset but inside ROI srcDir.radec_deg(1.0, 1.0); npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi); test_value(npsf, 1.0, 1.0e-3, "PSF(1,1) integration"); // Test PSF outside and overlapping ROI srcDir.radec_deg(0.0, 2.0); npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi); test_value(npsf, 0.492373, 1.0e-3, "PSF(0,2) integration"); // Test PSF outside ROI srcDir.radec_deg(2.0, 2.0); npsf = rsp.npsf(srcDir, srcEng.log10TeV(), srcTime, pnt, roi); test_value(npsf, 0.0, 1.0e-3, "PSF(2,2) integration"); // Return return; }
void test_spread(int64_t start, int64_t before, int64_t after) { int64_t i; printf( "Testing range: %" PRId64 " to %" PRId64 ". Spread: % " PRId64 "\n", start - before, start + after, before + after); for (i = start - before; i < start + after; i++) test_value(i); }
int main() { test_value(0); test_value(100.1); test_value(-100.1); test_value(.5); test_value(-.5); test_value(1 - 1e-16); test_value(-(1 - 1e-16)); return 0; }
/***********************************************************************//** * @brief Test a complex value * * @param[in] value Complex value to test. * @param[in] expected Expected complex value. * @param[in] name Test case name. * @param[in] message Test case message. * * Test if the @p value is equal to the @p expected value within a relative * precision of 1.0e-7. ***************************************************************************/ void GTestSuite::test_value(const std::complex<double>& value, const std::complex<double>& expected, const std::string& name, const std::string& message) { // Compute precision double eps = (expected != 0.0) ? 1.0e-7 * std::abs(expected) : 1.0e-7; // Test double precision value test_value(value, expected, eps, name, message); // Return return; }
/***********************************************************************//** * @brief Test GTimes ***************************************************************************/ void TestGObservation::test_times(void) { // Test void constructor test_try("Void constructor"); try { GTimes times; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GTimes starting from an empty object GTimes times; test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Add a time times.append(GTime()); test_value(times.size(), 1, "GTimes should have 1 time."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Remove time times.remove(0); test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Append two times times.append(GTime()); times.append(GTime()); test_value(times.size(), 2, "GTimes should have 2 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Clear object times.clear(); test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Insert two times times.insert(0, GTime()); times.insert(0, GTime()); test_value(times.size(), 2, "GTimes should have 2 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Extend times times.extend(times); test_value(times.size(), 4, "GTimes should have 4 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Return return; }
/***********************************************************************//** * @brief Test XML element access **************************************************************************/ void TestGXml::test_GXml_access(void) { // Test root document access GXml xml; xml.load(m_xml_file); test_value(xml.size(), 3, "Check number of child elements"); // Test node access for (int i = 0; i < xml.size(); ++i) { GXmlNode* ptr = xml[i]; test_assert(ptr != 0, "Check XML node access"); } test_value(xml.elements(), 1, "Check number of child elements"); // Test node access for (int i = 0; i < xml.elements(); ++i) { GXmlNode* ptr = xml.element(i); test_assert(ptr != 0, "Check XML element access"); } test_value(xml.elements("source_library"), 1, "Check number of child elements"); // Test element access for (int i = 0; i < xml.elements("source_library"); ++i) { GXmlElement* ptr = xml.element("source_library", i); test_value(ptr->name(), "source_library", "Check element name"); } // Test hierarchy access GXmlElement* ptr = NULL; ptr = xml.element("source_library"); test_value(ptr->name(), "source_library", "Check hierarchy level 1"); ptr = xml.element("source_library > source"); test_value(ptr->name(), "source", "Check hierarchy level 2"); ptr = xml.element("source_library > source > spectrum"); test_value(ptr->name(), "spectrum", "Check hierarchy level 3"); ptr = xml.element("source_library > source > spectrum > parameter[2]"); test_value(ptr->name(), "parameter", "Check hierarchy level 4"); test_value(ptr->attribute("name"), "PivotEnergy", "Check for attribute"); // Return return; }
/***********************************************************************//** * @brief Test GPhotons ***************************************************************************/ void TestGObservation::test_photons(void) { // Test void constructor test_try("Void constructor"); try { GPhotons photons; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GPhotons starting from an empty object GPhotons photons; test_value(photons.size(), 0, "GPhotons should have zero size."); test_assert(photons.is_empty(), "GPhotons should be empty."); // Add one photon photons.append(GPhoton()); test_value(photons.size(), 1, "GPhotons should have 1 photon."); test_assert(!photons.is_empty(), "GPhotons should not be empty."); // Remove photon photons.remove(0); test_value(photons.size(), 0, "GPhotons should have zero size."); test_assert(photons.is_empty(), "GPhotons should be empty."); // Append two photons photons.append(GPhoton()); photons.append(GPhoton()); test_value(photons.size(), 2, "GPhotons should have 2 photons."); test_assert(!photons.is_empty(), "GPhotons should not be empty."); // Clear object photons.clear(); test_value(photons.size(), 0, "GPhotons should have zero size."); test_assert(photons.is_empty(), "GPhotons should be empty."); // Insert two photons photons.insert(0, GPhoton()); photons.insert(0, GPhoton()); test_value(photons.size(), 2, "GPhotons should have 2 photons."); test_assert(!photons.is_empty(), "GPhotons should not be empty."); // Extend photons photons.extend(photons); test_value(photons.size(), 4, "GPhotons should have 4 photons."); test_assert(!photons.is_empty(), "GPhotons should not be empty."); // Return return; }
/*************************************************************************** * @brief Test GSkyRegions input and output ***************************************************************************/ void TestGSky::test_GSkyRegions_io(void) { // Set filenames const std::string filename = "data/test_circle_region.reg"; // Allocate regions GSkyRegions regions; // Test regions loading test_try("Test regions loading"); try { regions.load(filename); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Check if region was loaded correctly GSkyRegionCircle* circle = dynamic_cast<GSkyRegionCircle*>(regions[0]); test_assert(circle->type() == "Circle", "Region is not a circle"); test_value(circle->radius(), 10.0, 1.0e-10); test_value(circle->ra(), 0.1, 1.0e-10); test_value(circle->dec(), -35.6, 1.0e-10); // Test regions saving test_try("Test regions saving"); try { regions.save("region.reg"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test regions reloading test_try("Test regions reloading"); try { regions.load("region.reg"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Check if region was loaded correctly circle = dynamic_cast<GSkyRegionCircle*>(regions[0]); test_assert(circle->type() == "Circle", "Region is not a circle"); test_value(circle->radius(), 10.0, 1.0e-10); test_value(circle->ra(), 0.1, 1.0e-10); test_value(circle->dec(), -35.6, 1.0e-10); // Exit test return; }
int main(int argc, char** argv) { test_no_arg(); test_no_value(); test_value(); test_long_alias(); test_short_alias(); test_stop_after_arguments(); test_arguments(); test_arguments_first(); test_arguments_first_marker(); test_arguments_many_markers(); test_arguments_issue_2(); test_arguments_issue_3(); test_all(); return 0; }
/***********************************************************************//** * @brief Test CTA Aeff computation ***************************************************************************/ void TestGCTAResponse::test_response_aeff(void) { // Load response GCTAResponse rsp; rsp.caldb(cta_caldb); rsp.load(cta_irf); // Sum over effective area for control GEnergy eng; double sum = 0.0; double ref = 154124059000.00006; //!< Adjust to actual value for (int i = 0; i < 30; ++i) { eng.TeV(pow(10.0, -1.7 + 0.1*double(i))); double aeff = rsp.aeff(0.0, 0.0, 0.0, 0.0, eng.log10TeV()); //std::cout << eng << " " << eng.log10TeV() << " " << aeff << std::endl; sum += aeff; } test_value(sum, ref, 0.1, "Effective area verification"); // Return return; }
bool test_multi_value_child( struct semantic* semantic, struct multi_value_test* test, struct multi_value* multi_value, struct initial* initial ) { bool capacity = ( ( test->dim && ( ! test->dim->size_node || test->count < test->dim->size ) ) || test->member ); if ( ! capacity ) { s_diag( semantic, DIAG_POS_ERR, &multi_value->pos, "too many values in brace initializer" ); s_bail( semantic ); } if ( initial->multi ) { // There needs to be an element or member to initialize. bool deeper = ( ( test->dim && ( test->dim->next || ! test->type->primitive ) ) || ( test->member && ( test->member->dim || ! test->member->type->primitive ) ) ); if ( ! deeper ) { s_diag( semantic, DIAG_POS_ERR, &multi_value->pos, "too many brace initializers" ); s_bail( semantic ); } struct multi_value_test nested; init_multi_value_test( &nested, ( test->dim ? test->dim->next : test->member->dim ), ( test->dim ? test->type : test->member->type ), test->constant, true ); bool resolved = test_multi_value( semantic, &nested, ( struct multi_value* ) initial ); if ( nested.has_string ) { test->has_string = true; } return resolved; } else { return test_value( semantic, test, ( test->dim ? test->dim->next : test->member->dim ), ( test->dim ? test->type : test->member->type ), ( struct value* ) initial ); } }
/***********************************************************************//** * @brief Test GEbounds ***************************************************************************/ void TestGObservation::test_ebounds(void) { // Test void constructor test_try("Void constructor"); try { GEbounds ebds; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate energy boudaries starting from an empty object GEbounds ebds; test_value(ebds.size(), 0, "GEbounds should have zero size."); test_assert(ebds.is_empty(), "GEbounds should be empty."); test_value(ebds.emin().MeV(), 0.0, 1.0e-10, "Minimum energy should be 0."); test_value(ebds.emax().MeV(), 0.0, 1.0e-10, "Maximum energy should be 0."); // Add empty interval ebds.append(GEnergy(1.0, "MeV"), GEnergy(1.0, "MeV")); test_value(ebds.size(), 0, "GEbounds should have zero size."); test_assert(ebds.is_empty(), "GEbounds should be empty."); test_value(ebds.emin().MeV(), 0.0, 1.0e-10, "Minimum energy should be 0."); test_value(ebds.emax().MeV(), 0.0, 1.0e-10, "Maximum energy should be 0."); // Add one interval ebds.append(GEnergy(1.0, "MeV"), GEnergy(10.0, "MeV")); test_value(ebds.size(), 1, "GEbounds should have 1 element."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 10.0, 1.0e-10, "Maximum energy should be 10."); // Remove interval ebds.remove(0); test_value(ebds.size(), 0, "GEbounds should have zero size."); test_assert(ebds.is_empty(), "GEbounds should be empty."); test_value(ebds.emin().MeV(), 0.0, 1.0e-10, "Minimum energy should be 0."); test_value(ebds.emax().MeV(), 0.0, 1.0e-10, "Maximum energy should be 0."); // Append two overlapping intervals ebds.append(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); ebds.append(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Clear object ebds.clear(); test_value(ebds.size(), 0, "GEbounds should have zero size."); test_assert(ebds.is_empty(), "GEbounds should be empty."); test_value(ebds.emin().MeV(), 0.0, 1.0e-10, "Minimum energy should be 0."); test_value(ebds.emax().MeV(), 0.0, 1.0e-10, "Maximum energy should be 0."); // Append two overlapping intervals in inverse order ebds.clear(); ebds.append(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); ebds.append(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Insert two overlapping intervals ebds.clear(); ebds.insert(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); ebds.insert(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Insert two overlapping intervals in inverse order ebds.clear(); ebds.insert(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); ebds.insert(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Merge two overlapping intervals ebds.clear(); ebds.merge(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); ebds.merge(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); test_value(ebds.size(), 1, "GEbounds should have 1 element."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Merge two overlapping intervals in inverse order ebds.clear(); ebds.merge(GEnergy(10.0, "MeV"), GEnergy(1000.0, "MeV")); ebds.merge(GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); test_value(ebds.size(), 1, "GEbounds should have 1 element."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Check linear boundaries ebds.clear(); ebds.set_lin(2, GEnergy(1.0, "MeV"), GEnergy(3.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin(0).MeV(), 1.0, 1.0e-10, "Bin 0 minimum energy should be 1."); test_value(ebds.emin(1).MeV(), 2.0, 1.0e-10, "Bin 1 minimum energy should be 2."); test_value(ebds.emax(0).MeV(), 2.0, 1.0e-10, "Bin 0 maximum energy should be 2."); test_value(ebds.emax(1).MeV(), 3.0, 1.0e-10, "Bin 1 maximum energy should be 3."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 3.0, 1.0e-10, "Maximum energy should be 3."); // Check logarithmic boundaries ebds.clear(); ebds.set_log(2, GEnergy(1.0, "MeV"), GEnergy(100.0, "MeV")); test_value(ebds.size(), 2, "GEbounds should have 2 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin(0).MeV(), 1.0, 1.0e-10, "Bin 0 minimum energy should be 1."); test_value(ebds.emin(1).MeV(), 10.0, 1.0e-10, "Bin 1 minimum energy should be 10."); test_value(ebds.emax(0).MeV(), 10.0, 1.0e-10, "Bin 0 maximum energy should be 10."); test_value(ebds.emax(1).MeV(), 100.0, 1.0e-10, "Bin 1 maximum energy should be 100."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 100.0, 1.0e-10, "Maximum energy should be 100."); // Check boundary extension GEbounds ext(1, GEnergy(100.0, "MeV"), GEnergy(1000.0, "MeV")); ebds.extend(ext); test_value(ebds.size(), 3, "GEbounds should have 3 elements."); test_assert(!ebds.is_empty(), "GEbounds should not be empty."); test_value(ebds.emin(0).MeV(), 1.0, 1.0e-10, "Bin 0 minimum energy should be 1."); test_value(ebds.emin(1).MeV(), 10.0, 1.0e-10, "Bin 1 minimum energy should be 10."); test_value(ebds.emin(2).MeV(), 100.0, 1.0e-10, "Bin 2 minimum energy should be 100."); test_value(ebds.emax(0).MeV(), 10.0, 1.0e-10, "Bin 0 maximum energy should be 10."); test_value(ebds.emax(1).MeV(), 100.0, 1.0e-10, "Bin 1 maximum energy should be 100."); test_value(ebds.emax(2).MeV(), 1000.0, 1.0e-10, "Bin 1 maximum energy should be 1000."); test_value(ebds.emin().MeV(), 1.0, 1.0e-10, "Minimum energy should be 1."); test_value(ebds.emax().MeV(), 1000.0, 1.0e-10, "Maximum energy should be 1000."); // Return return; }
/***********************************************************************//** * @brief Test observations optimizer. * * @param[in] mode Testing mode. * * This method supports two testing modes: 0 = unbinned and 1 = binned. ***************************************************************************/ void TestOpenMP::test_observations_optimizer(const int& mode) { // Create Test Model GTestModelData model; // Create Models conteners GModels models; models.append(model); // Time iterval GTime tmin(0.0); GTime tmax(1800.0); // Rate : events/sec double rate = RATE; // Create observations GObservations obs; // Add some observation for (int i = 0; i < 6; ++i) { // Random Generator GRan ran; ran.seed(i); // Allocate events pointer GEvents *events; // Create either a event list or an event cube if (mode == UN_BINNED) { events = model.generateList(rate,tmin,tmax,ran); } else { events = model.generateCube(rate,tmin,tmax,ran); } // Create an observation GTestObservation ob; ob.id(gammalib::str(i)); // Add events to the observation ob.events(*events); ob.ontime(tmax.secs()-tmin.secs()); obs.append(ob); // Delete events pointer delete events; } // Add the model to the observation obs.models(models); // Create a GLog for show the interations of optimizer. GLog log; // Create an optimizer. GOptimizerLM opt(log); opt.max_stalls(50); // Optimize obs.optimize(opt); // Get the result GModelPar result = (*(obs.models()[0]))[0]; // Check if converged test_assert(opt.status()==0, "Check if converged", "Optimizer did not converge"); // Check if value is correct test_value(result.factor_value(),RATE,result.factor_error()*3); // Return return; }
/***********************************************************************//** * @brief Test GEnergies ***************************************************************************/ void TestGObservation::test_energies(void) { // Test void constructor test_try("Void constructor"); try { GEnergies energies; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GEnergies starting from an empty object GEnergies energies; test_value(energies.size(), 0, "GEnergies should have zero size."); test_assert(energies.is_empty(), "GEnergies should be empty."); // Add an energy energies.append(GEnergy()); test_value(energies.size(), 1, "GEnergies should have 1 energy."); test_assert(!energies.is_empty(), "GEnergies should not be empty."); // Remove energy energies.remove(0); test_value(energies.size(), 0, "GEnergies should have zero size."); test_assert(energies.is_empty(), "GEnergies should be empty."); // Append two energies energies.append(GEnergy()); energies.append(GEnergy()); test_value(energies.size(), 2, "GEnergies should have 2 energies."); test_assert(!energies.is_empty(), "GEnergies should not be empty."); // Clear object energies.clear(); test_value(energies.size(), 0, "GEnergies should have zero size."); test_assert(energies.is_empty(), "GEnergies should be empty."); // Insert two energies energies.insert(0, GEnergy()); energies.insert(0, GEnergy()); test_value(energies.size(), 2, "GEnergies should have 2 energies."); test_assert(!energies.is_empty(), "GEnergies should not be empty."); // Extend energies energies.extend(energies); test_value(energies.size(), 4, "GEnergies should have 4 energies."); test_assert(!energies.is_empty(), "GEnergies should not be empty."); // Create 4 energies energies.clear(); for (int i = 0; i < 4; ++i) { energies.append(GEnergy(double(i), "MeV")); } for (int i = 0; i < 4; ++i) { test_value(energies[i].MeV(), double(i)); } // Save and reload energies test_try("Saving and loading"); try { energies.save("test_energies.fits", true); energies.clear(); energies.load("test_energies.fits"); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } for (int i = 0; i < 4; ++i) { test_value(energies[i].MeV(), double(i)); } // Test load constructor test_try("Load constructor"); try { GEnergies energies2("test_energies.fits"); for (int i = 0; i < 4; ++i) { test_value(energies[i].MeV(), double(i)); } test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Return return; }
/***********************************************************************//** * @brief Test GTime ***************************************************************************/ void TestGObservation::test_time(void) { // Test void constructor test_try("Void constructor"); try { GTime time; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test copy constructor test_try("Copy constructor"); try { GTime time; GTime time2(time); test_try_success(); test_assert(time == time2, "Time differs after using copy constructor."); } catch (std::exception &e) { test_try_failure(e); } // Test time constructor (seconds) test_try("Time constructor (seconds)"); try { GTime time(1800.01); test_try_success(); test_value(time.secs(), 1800.01); test_value(time.days(), 1800.01/86400.0); } catch (std::exception &e) { test_try_failure(e); } // Test time constructor (days) test_try("Time constructor (days)"); try { GTime time(41.7, "days"); test_try_success(); test_value(time.days(), 41.7); test_value(time.secs(), 41.7*86400.0); } catch (std::exception &e) { test_try_failure(e); } // Test access methods double mjd_ref = 55197.000766018518519; double jd_ref = 2455197.500766018518519; double t = 123456.789; GTime time(t); test_value(time.jd(), t/86400.0 + jd_ref); test_value(time.mjd(), t/86400.0 + mjd_ref); test_value(time.secs(), t); test_value(time.days(), t/86400.0); // Test set method time.jd(57.9); test_value(time.jd(), 57.9); time.mjd(57.9); test_value(time.mjd(), 57.9); time.secs(57.9); test_value(time.secs(), 57.9); time.days(57.9); test_value(time.days(), 57.9); // Test convert method time.secs(t); test_value(time.convert(GTimeReference(55197.000766018518519, "days", "TT", "LOCAL")), t/86400.0); test_value(time.convert(GTimeReference(0.0, "s", "TT", "LOCAL")), t + mjd_ref*86400.0, 1.0e-6); //!< Poor precision on OpenSolaris // Test set method time.set(12.3, GTimeReference(55197.000766018518519, "days", "TT", "LOCAL")); test_value(time.days(), 12.3); time.set(12.3, GTimeReference(0.0, "secs", "TT", "LOCAL")); test_value(time.secs(), 12.3 - mjd_ref*86400.0); // Test operators GTime a(13.72); GTime b(6.28); test_value((a+b).secs(), 20.00); test_value((a-b).secs(), 7.44); test_value((a*3.3).secs(), 45.276); test_value((3.3*a).secs(), 45.276); test_value((a/13.72).secs(), 1.0); test_assert(a == a, "Equality operator corrupt."); test_assert(a != b, "Non-equality operator corrupt."); test_assert(a > b, "Greater than operator corrupt."); test_assert(a >= b, "Greater than or equal operator corrupt."); test_assert(b < a, "Less than operator corrupt."); test_assert(b <= a, "Less than or equal operator corrupt."); // Return return; }
/***********************************************************************//** * @brief Test GTimeReference ***************************************************************************/ void TestGObservation::test_time_reference(void) { // Test void constructor test_try("Void constructor"); try { GTimeReference reference; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test copy constructor test_try("Copy constructor"); try { GTimeReference reference; GTimeReference reference2(reference); test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Test reference constructor test_try("Reference constructor"); try { GTimeReference reference(55197.0, "s", "TT", "LOCAL"); test_try_success(); test_value(reference.mjdref(), 55197.0); test_assert(reference.timeunit() == "s", "Time unit was \""+reference.timeunit()+"\", expected \"s\""); test_assert(reference.timesys() == "TT", "Time system was \""+reference.timesys()+"\", expected \"TT\""); test_assert(reference.timeref() == "LOCAL", "Time reference was \""+reference.timeref()+"\", expected \"LOCAL\""); } catch (std::exception &e) { test_try_failure(e); } // Test reference constructor test_try("Reference constructor (split reference)"); try { GTimeReference reference(55197, 0.000766018518519, "s", "TT", "LOCAL"); test_try_success(); test_value(reference.mjdref(), 55197.000766018518519); test_assert(reference.timeunit() == "s", "Time unit was \""+reference.timeunit()+"\", expected \"s\""); test_assert(reference.timesys() == "TT", "Time system was \""+reference.timesys()+"\", expected \"TT\""); test_assert(reference.timeref() == "LOCAL", "Time reference was \""+reference.timeref()+"\", expected \"LOCAL\""); } catch (std::exception &e) { test_try_failure(e); } // Test FITS file writing GTimeReference reference(55197.000766018518519, "s", "TT", "LOCAL"); GFits fits; GFitsBinTable table; reference.write(table); fits.append(table); fits.saveto("test_time_reference.fits", true); fits.close(); // Read back from FITS file and check values fits.open("test_time_reference.fits"); const GFitsTable& hdu = *fits.table(1); GTimeReference value(hdu); fits.close(); test_value(value.mjdref(), reference.mjdref()); test_value(value.mjdrefi(), reference.mjdrefi()); test_value(value.mjdreff(), reference.mjdreff()); test_assert(value.timeunit() == reference.timeunit(), "Time unit was \""+value.timeunit()+"\", expected "+reference.timeunit()+"."); test_assert(value.timesys() == reference.timesys(), "Time system was \""+value.timesys()+"\", expected "+reference.timesys()+"."); test_assert(value.timeref() == reference.timeref(), "Time reference was \""+value.timeref()+"\", expected "+reference.timeref()+"."); // Return return; }
/***********************************************************************//** * @brief Test GGti ***************************************************************************/ void TestGObservation::test_gti(void) { // Test void constructor test_try("Void constructor"); try { GGti gti; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GTIs starting from an empty object GGti gti; test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Add empty interval gti.append(GTime(1.0), GTime(1.0)); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Add one interval gti.append(GTime(1.0), GTime(10.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 10.0, 1.0e-10, "Stop time should be 10."); // Remove interval gti.remove(0); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Append two overlapping intervals gti.append(GTime(1.0), GTime(100.0)); gti.append(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Clear object gti.clear(); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Append two overlapping intervals in inverse order gti.clear(); gti.append(GTime(10.0), GTime(1000.0)); gti.append(GTime(1.0), GTime(100.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Insert two overlapping intervals gti.clear(); gti.insert(GTime(1.0), GTime(100.0)); gti.insert(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Insert two overlapping intervals in inverse order gti.clear(); gti.insert(GTime(10.0), GTime(1000.0)); gti.insert(GTime(1.0), GTime(100.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Merge two overlapping intervals gti.clear(); gti.merge(GTime(1.0), GTime(100.0)); gti.merge(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Merge two overlapping intervals in inverse order gti.clear(); gti.merge(GTime(10.0), GTime(1000.0)); gti.merge(GTime(1.0), GTime(100.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Check extension gti.clear(); gti.append(GTime(1.0), GTime(10.0)); gti.append(GTime(10.0), GTime(100.0)); GGti ext; ext.append(GTime(100.0), GTime(1000.0)); gti.extend(ext); test_value(gti.size(), 3, "GGti should have 3 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart(0).secs(), 1.0, 1.0e-10, "Bin 0 start time should be 1."); test_value(gti.tstart(1).secs(), 10.0, 1.0e-10, "Bin 1 start time should be 10."); test_value(gti.tstart(2).secs(), 100.0, 1.0e-10, "Bin 2 start time should be 100."); test_value(gti.tstop(0).secs(), 10.0, 1.0e-10, "Bin 0 stop time should be 10."); test_value(gti.tstop(1).secs(), 100.0, 1.0e-10, "Bin 1 stop time should be 100."); test_value(gti.tstop(2).secs(), 1000.0, 1.0e-10, "Bin 2 stop time should be 1000."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Return return; }
/***********************************************************************//** * @brief Test Cholesky decomposition ***************************************************************************/ void TestGMatrixSparse::matrix_cholesky(void) { // Setup matrix for Cholesky decomposition GMatrixSparse chol_test(5,5); chol_test(0,0) = 1.0; chol_test(0,1) = 0.2; chol_test(0,2) = 0.2; chol_test(0,3) = 0.2; chol_test(0,4) = 0.2; chol_test(1,0) = 0.2; chol_test(2,0) = 0.2; chol_test(3,0) = 0.2; chol_test(4,0) = 0.2; chol_test(1,1) = 1.0; chol_test(2,2) = 1.0; chol_test(3,3) = 1.0; chol_test(4,4) = 1.0; // Try to solve now (should not work) test_try("Try Cholesky solver without factorisation"); try { GVector vector(5); vector = chol_test.cholesky_solver(vector); test_try_failure("Expected GException::matrix_not_factorised exception."); } catch (GException::matrix_not_factorised &e) { test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Perform Cholesky decomposition GMatrixSparse cd = chol_test.cholesky_decompose(); // Test Cholesky solver (first test) GVector e0(5); GVector a0(5); e0[0] = 1.0; a0[0] = 1.0; a0[1] = 0.2; a0[2] = 0.2; a0[3] = 0.2; a0[4] = 0.2; GVector s0 = cd.cholesky_solver(a0); double res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test cholesky_solver() method - 1"); // Test Cholesky solver (second test) e0[0] = 0.0; e0[1] = 1.0; a0[0] = 0.2; a0[1] = 1.0; a0[2] = 0.0; a0[3] = 0.0; a0[4] = 0.0; s0 = cd.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test cholesky_solver() method - 2"); // Test Cholesky solver (third test) e0[1] = 0.0; e0[2] = 1.0; a0[1] = 0.0; a0[2] = 1.0; s0 = cd.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test cholesky_solver() method - 3"); // Test Cholesky solver (forth test) e0[2] = 0.0; e0[3] = 1.0; a0[2] = 0.0; a0[3] = 1.0; s0 = cd.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test cholesky_solver() method - 4"); // Test Cholesky solver (fifth test) e0[3] = 0.0; e0[4] = 1.0; a0[3] = 0.0; a0[4] = 1.0; s0 = cd.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test cholesky_solver() method - 5"); // Setup matrix for Cholesky decomposition with zero row/col GMatrixSparse chol_test_zero(6,6); chol_test_zero(0,0) = 1.0; chol_test_zero(0,1) = 0.2; chol_test_zero(0,2) = 0.2; chol_test_zero(0,4) = 0.2; chol_test_zero(0,5) = 0.2; chol_test_zero(1,0) = 0.2; chol_test_zero(2,0) = 0.2; chol_test_zero(4,0) = 0.2; chol_test_zero(5,0) = 0.2; chol_test_zero(1,1) = 1.0; chol_test_zero(2,2) = 1.0; chol_test_zero(4,4) = 1.0; chol_test_zero(5,5) = 1.0; // Test compressed Cholesky decomposition GMatrixSparse cd_zero = chol_test_zero.cholesky_decompose(); // Test compressed Cholesky solver (first test) e0 = GVector(6); a0 = GVector(6); e0[0] = 1.0; a0[0] = 1.0; a0[1] = 0.2; a0[2] = 0.2; a0[4] = 0.2; a0[5] = 0.2; s0 = cd_zero.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test compressed cholesky_solver() method - 1"); // Test compressed Cholesky solver (second test) e0[0] = 0.0; e0[1] = 1.0; a0[0] = 0.2; a0[1] = 1.0; a0[2] = 0.0; a0[4] = 0.0; a0[5] = 0.0; s0 = cd_zero.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test compressed cholesky_solver() method - 2"); // Test compressed Cholesky solver (third test) e0[1] = 0.0; e0[2] = 1.0; a0[1] = 0.0; a0[2] = 1.0; s0 = cd_zero.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test compressed cholesky_solver() method - 3"); // Test compressed Cholesky solver (forth test) e0[2] = 0.0; e0[4] = 1.0; a0[2] = 0.0; a0[4] = 1.0; s0 = cd_zero.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test compressed cholesky_solver() method - 4"); // Test compressed Cholesky solver (fifth test) e0[4] = 0.0; e0[5] = 1.0; a0[4] = 0.0; a0[5] = 1.0; s0 = cd_zero.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test compressed cholesky_solver() method - 5"); // Setup matrix for Cholesky decomposition with zero row/col (unsymmetric case) GMatrixSparse chol_test_zero2(6,5); chol_test_zero2(0,0) = 1.0; chol_test_zero2(0,1) = 0.2; chol_test_zero2(0,2) = 0.2; chol_test_zero2(0,3) = 0.2; chol_test_zero2(0,4) = 0.2; chol_test_zero2(1,0) = 0.2; chol_test_zero2(2,0) = 0.2; chol_test_zero2(4,0) = 0.2; chol_test_zero2(5,0) = 0.2; chol_test_zero2(1,1) = 1.0; chol_test_zero2(2,2) = 1.0; chol_test_zero2(4,3) = 1.0; chol_test_zero2(5,4) = 1.0; // Test compressed Cholesky decomposition (unsymmetric case) GMatrixSparse cd_zero2 = chol_test_zero2.cholesky_decompose(); // Test compressed Cholesky solver (unsymmetric case) e0 = GVector(5); a0 = GVector(6); e0[0] = 1.0; a0[0] = 1.0; a0[1] = 0.2; a0[2] = 0.2; a0[4] = 0.2; a0[5] = 0.2; s0 = cd_zero2.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test unsymmetric compressed cholesky_solver() method - 1"); // Test compressed Cholesky solver (unsymmetric case) e0[0] = 0.0; e0[1] = 1.0; a0[0] = 0.2; a0[1] = 1.0; a0[2] = 0.0; a0[4] = 0.0; a0[5] = 0.0; s0 = cd_zero2.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test unsymmetric compressed cholesky_solver() method - 2"); // Test compressed Cholesky solver (unsymmetric case) e0[1] = 0.0; e0[2] = 1.0; a0[1] = 0.0; a0[2] = 1.0; s0 = cd_zero2.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test unsymmetric compressed cholesky_solver() method - 3"); // Test compressed Cholesky solver (unsymmetric case) e0[2] = 0.0; e0[3] = 1.0; a0[2] = 0.0; a0[4] = 1.0; s0 = cd_zero2.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test unsymmetric compressed cholesky_solver() method - 4"); // Test compressed Cholesky solver (unsymmetric case) e0[3] = 0.0; e0[4] = 1.0; a0[4] = 0.0; a0[5] = 1.0; s0 = cd_zero2.cholesky_solver(a0); res = max(abs(s0-e0)); test_value(res, 0.0, 1.0e-15, "Test unsymmetric compressed cholesky_solver() method - 5"); // Test Cholesky inverter (inplace) GMatrixSparse unit(5,5); unit(0,0) = 1.0; unit(1,1) = 1.0; unit(2,2) = 1.0; unit(3,3) = 1.0; unit(4,4) = 1.0; GMatrixSparse chol_test_inv = chol_test.cholesky_invert(); GMatrixSparse ci_product = chol_test * chol_test_inv; GMatrixSparse ci_residuals = ci_product - unit; res = (ci_residuals.abs()).max(); test_value(res, 0.0, 1.0e-15, "Test Cholesky inverter"); // Test Cholesky inverter /* chol_test_inv = chol_test.cholesky_invert(); ci_product = chol_test * chol_test_inv; ci_residuals = ci_product - unit; res = (ci_residuals.abs()).max(); test_value(res, 0.0, 1.0e-15, "Test Cholesky inverter"); */ // Test Cholesky inverter for compressed matrix unit = GMatrixSparse(6,6); unit(0,0) = 1.0; unit(1,1) = 1.0; unit(2,2) = 1.0; unit(4,4) = 1.0; unit(5,5) = 1.0; GMatrixSparse chol_test_zero_inv = chol_test_zero.cholesky_invert(); GMatrixSparse ciz_product = chol_test_zero * chol_test_zero_inv; GMatrixSparse ciz_residuals = ciz_product - unit; res = (ciz_residuals.abs()).max(); test_value(res, 0.0, 1.0e-15, "Test compressed matrix Cholesky inverter"); // Return return; }