1+ /*
2+ * Copyright (c) 2019 - 2025 Geode-solutions
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ * SOFTWARE.
21+ *
22+ */
23+
24+ #include < geode/stochastic/common.hpp>
25+ #include < geode/stochastic/sampling/mcmc/helpers/simulation_monitor.hpp>
26+ #include < geode/stochastic/sampling/mcmc/helpers/simulation_printer.hpp>
27+
28+ #include < filesystem>
29+ #include < fstream>
30+
31+ namespace
32+ {
33+ void test_print_statistics (
34+ const geode::SimulationPrinterConfigurator& config )
35+ {
36+ geode::Logger::info ( " [TEST] SimulationPrinter print statistics" );
37+
38+ geode::SimulationPrinter printer ( config );
39+
40+ // --- Test print_statistics
41+ std::vector< double > stats = { 1.0 , 2.5 , 3.7 };
42+ printer.print_statistics (
43+ stats, " EnergyTerm1;EnergyTerm2;EnergyTerm3" );
44+
45+ const std::filesystem::path temp_folder = config.output_folder ;
46+ const auto stats_path = temp_folder / config.statistics_filename ;
47+ OPENGEODE_EXCEPTION ( std::filesystem::exists ( stats_path ),
48+ " Statistics file not created" );
49+
50+ std::ifstream stats_file ( stats_path );
51+ std::string line;
52+ std::getline ( stats_file, line );
53+ OPENGEODE_EXCEPTION (
54+ line == " # Simulation Statistics" , " Header not correctly written" );
55+ std::getline ( stats_file, line );
56+ OPENGEODE_EXCEPTION ( line == " EnergyTerm1;EnergyTerm2;EnergyTerm3" ,
57+ " Header not correctly written" );
58+ std::getline ( stats_file, line );
59+ OPENGEODE_EXCEPTION (
60+ line == " 1 ; 2.5 ; 3.7" , " Statistics line not correctly written" );
61+
62+ geode::Logger::info ( " --> Success!" );
63+ }
64+
65+ void test_statistics_summary (
66+ const geode::SimulationPrinterConfigurator& config )
67+ {
68+ geode::Logger::info (
69+ " [TEST] SimulationPrinter print statistics summary" );
70+
71+ geode::SimulationPrinter printer ( config );
72+ geode::StatisticsMonitor monitor ( 2 );
73+ monitor.add_realization ( { 1 , 2 } );
74+ monitor.finalize ();
75+
76+ printer.print_statistics_summary ( monitor, " EnergyTerm1;EnergyTerm2" );
77+
78+ const std::filesystem::path temp_folder = config.output_folder ;
79+ const auto summary_path =
80+ temp_folder / config.statistics_summary_filename ;
81+
82+ OPENGEODE_EXCEPTION ( std::filesystem::exists ( summary_path ),
83+ " Summary file not created" );
84+
85+ std::ifstream summary_file ( summary_path );
86+ std::string content (
87+ ( std::istreambuf_iterator< char >( summary_file ) ),
88+ std::istreambuf_iterator< char >() );
89+ OPENGEODE_EXCEPTION (
90+ content.find ( " EnergyTerm1;EnergyTerm2" ) != std::string::npos,
91+ " Energy term names missing" );
92+ OPENGEODE_EXCEPTION (
93+ content.find ( " 2" ) != std::string::npos, " Count missing" );
94+ OPENGEODE_EXCEPTION (
95+ content.find ( " 1 ; 2" ) != std::string::npos, " Means missing" );
96+ OPENGEODE_EXCEPTION (
97+ content.find ( " 0 ; 0" ) != std::string::npos, " Variances missing" );
98+
99+ geode::Logger::info ( " --> Success!" );
100+ }
101+
102+ void test_print_objects (
103+ const geode::SimulationPrinterConfigurator& config )
104+ {
105+ geode::Logger::info (
106+ " [TEST] SimulationPrinter print statistics summary" );
107+
108+ geode::ObjectSets< geode::Point2D > object_sets;
109+ const auto set_id = object_sets.add_set ( " default_name" );
110+
111+ object_sets.add_object ( geode::Point2D{ { 0.0 , 0.0 } }, set_id );
112+ object_sets.add_object ( geode::Point2D{ { 1.0 , 0.0 } }, set_id );
113+ object_sets.add_object ( geode::Point2D{ { 3.0 , 0.0 } }, set_id );
114+
115+ geode::SimulationPrinter printer ( config );
116+ printer.print_object_sets ( object_sets, 1 );
117+
118+ const std::filesystem::path temp_folder = config.output_folder ;
119+
120+ const auto obj_path = temp_folder / " pattern_1.txt" ;
121+ OPENGEODE_EXCEPTION ( std::filesystem::exists ( obj_path ),
122+ " Object sets file not created" );
123+
124+ geode::Logger::info ( " --> Success!" );
125+ }
126+
127+ } // namespace
128+
129+ int main ()
130+ {
131+ try
132+ {
133+ geode::StochasticLibrary::initialize ();
134+ geode::Logger::set_level ( geode::Logger::LEVEL ::debug );
135+
136+ const std::filesystem::path temp_folder =
137+ std::filesystem::temp_directory_path () / " simprinter_test" ;
138+ SDEBUG ( temp_folder );
139+ std::filesystem::create_directories ( temp_folder );
140+
141+ geode::SimulationPrinterConfigurator config;
142+ config.output_folder = temp_folder.string ();
143+ config.statistics_filename = " stats.txt" ;
144+ config.statistics_summary_filename = " summary.txt" ;
145+ config.realisations_prefix = " pattern_" ;
146+
147+ test_print_statistics ( config );
148+ test_statistics_summary ( config );
149+ test_print_objects ( config );
150+ return 0 ;
151+ }
152+ catch ( ... )
153+ {
154+ return geode::geode_lippincott ();
155+ }
156+ }
0 commit comments