• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NREL / SolTrace / 19580451842

21 Nov 2025 06:52PM UTC coverage: 89.184% (+0.4%) from 88.811%
19580451842

push

github

web-flow
Merge pull request #85 from NREL/simdata_io_json

Add json import/export to SimulationData

423 of 451 new or added lines in 17 files covered. (93.79%)

9 existing lines in 3 files now uncovered.

5970 of 6694 relevant lines covered (89.18%)

7997777.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/coretrace/simulation_data/simulation_data.hpp
1
/**
2
 * @file simulation_data.hpp
3
 * @brief Main simulation data container and management
4
 *
5
 * Defines the SimulationData class which serves as the main container
6
 * for all optical elements, stages, and simulation parameters.
7
 * Manages the complete optical system definition and provides
8
 * interfaces for ray tracing execution.
9
 *
10
 * @defgroup simulation Simulation Management
11
 * @{
12
 */
13

14
#ifndef SOLTRACE_SIMULATION_DATA_H
15
#define SOLTRACE_SIMULATION_DATA_H
16

17
#include <cstdint>
18
#include <map>
19
#include <memory>
20

21
#include "container.hpp"
22
#include "element.hpp"
23
#include "ray_source.hpp"
24
#include "simulation_parameters.hpp"
25

26
namespace SolTrace::Data {
27

28
class SimulationData
29
{
30
public:
31
    SimulationData();
32
    virtual ~SimulationData();
33

34
    /// @brief Add the given RaySource to the simulation data
35
    /// @param src RaySource to add
36
    /// @return unique identifier for the RaySource
37
    ray_source_id add_ray_source(ray_source_ptr src)
×
38
    {
39
        return this->my_sources.add_item(src);
×
40
    }
41

42
    /// @brief Remove the RaySource corresponding to the unique identifier `id`
43
    /// @param id unique identifier of the RaySource to remove
44
    /// @return true if the element was removed, false otherwise
45
    auto remove_ray_source(ray_source_id id)
46
    {
47
        return this->my_sources.remove_item(id);
48
    }
49

50
    /// @brief Get the RaySource corresponding to the unique identifier `id`
51
    /// @param id unique identifier of the RaySource to get
52
    /// @return requested RaySource if found, nullptr otherwise
53
    ray_source_ptr get_ray_source(ray_source_id id) const
54
    {
55
        return this->my_sources.get_item(id);
56
    }
57

58
    ray_source_ptr get_ray_source() const
11✔
59
    {
60
        return this->my_sources.get_const_iterator()->second;
11✔
61
    }
62

63
    /// @brief Replace the RaySource with id `id` with the RaySource `src`
64
    /// @param id unique identifier of the RaySource to replace
65
    /// @param src RaySource to use in replacement
66
    /// @return true if the RaySource was replaced, false otherwise
67
    bool replace_ray_source(ray_source_id id, ray_source_ptr src)
68
    {
69
        return this->my_sources.replace_item(id, src);
70
    }
71

72
    uint_fast64_t get_number_of_ray_sources() const
8✔
73
    {
74
        return this->my_sources.get_number_of_items();
8✔
75
    }
76

77
    /// @brief Get an iterator that can be used to access all ray sources owned
78
    ///        by this SimulationData object.
79
    /// @return iterator
80
    RaySourceContainer::iterator get_ray_source_iterator()
10✔
81
    {
82
        return this->my_sources.get_iterator();
10✔
83
    }
84

85
    /// @brief Tests whether the given iterator is at the end
86
    /// @param iter iterator to test
87
    /// @return true if at end, false otherwise
88
    bool is_ray_source_at_end(RaySourceContainer::iterator it)
19✔
89
    {
90
        return this->my_sources.is_at_end(it);
19✔
91
    }
92

93
    /// @brief Add the given Element to the SimulationData. When adding a
94
    ///        CompositeElement, all subelements must already be present.
95
    ///        Any subelements added to the CompositeElement after calling
96
    ///        `add_element` will not be used in any ray tracing.
97
    /// @param el Element to add
98
    /// @return unique identifier for the given object
99
    element_id add_element(element_ptr el);
100
    inline element_id add_stage(element_ptr el)
16✔
101
    {
102
        return add_element(el);
16✔
103
    }
104

105
    /// @brief Remove the Element corresponding to the unique identifier `id`
106
    /// @param id unique identifier of element to remove
107
    /// @return number of elements removed
108
    uint_fast64_t remove_element(element_id id);
109

110
    /// @brief Get the Element corresponding to the unique identifier `id`
111
    /// @param id unique identifier of element to get
112
    /// @return requested Element (pointer) if found, nullptr otherwise
113
    element_ptr get_element(element_id id) const;
114

115
    /// @brief Replace the Element with id `id` with the Element `el`
116
    /// @param id unique identifier of element to replace
117
    /// @param el Element to use in replacement
118
    /// @return true if Element was replaced, false otherwise
119
    bool replace_element(element_id id, element_ptr el);
120

121
    /// @brief Gives the number of elements owned by the SimulationData.
122
    ///        CompositeElements do not count toward this number.
123
    /// @return Number of elements owned by the SimulationData object
124
    uint_fast64_t get_number_of_elements() const
7✔
125
    {
126
        // return this->my_elements.size();
127
        return this->number_of_elements;
7✔
128
    }
129

130
    /// @brief Get an iterator that can be used to access all elements owned
131
    ///        by this SimulationData object.
132
    /// @return iterator
UNCOV
133
    ElementContainer::iterator get_iterator()
×
134
    {
UNCOV
135
        return this->my_elements.get_iterator();
×
136
        // return this->my_elements.begin();
137
    }
138

139
    /// @brief Get an const_iterator that can be used to access all elements
140
    ///        owned by this SimulationData object.
141
    /// @return iterator
142
    ElementContainer::const_iterator get_const_iterator() const
32✔
143
    {
144
        return this->my_elements.get_const_iterator();
32✔
145
        // return this->my_elements.cbegin();
146
    }
147

148
    /// @brief Tests whether the given iterator is at the end
149
    /// @param iter iterator to test
150
    /// @return true if at end, false otherwise
UNCOV
151
    bool is_at_end(ElementContainer::iterator iter)
×
152
    {
UNCOV
153
        return this->my_elements.is_at_end(iter);
×
154
        // return iter == this->my_elements.end();
155
    }
156
    bool is_at_end(ElementContainer::const_iterator citer) const
15,044✔
157
    {
158
        return this->my_elements.is_at_end(citer);
15,044✔
159
        // return citer == this->my_elements.end();
160
    }
161

162
    /// @brief Set the number of rays to trace
163
    /// @param nrays number of rays to trace
164
    void set_number_of_rays(uint_fast64_t nrays)
165
    {
166
        this->my_parameters.number_of_rays = nrays;
167
        return;
168
    }
169

170
    /// @brief Get the number of rays to trace
171
    /// @return number of rays to trace
172
    uint_fast64_t get_number_of_rays() const
173
    {
174
        return this->my_parameters.number_of_rays;
175
    }
176

177
    void set_max_rays_traced(uint_fast64_t nrays)
178
    {
179
        this->my_parameters.max_number_of_rays = nrays;
180
        return;
181
    }
182

183
    uint_fast64_t get_max_number_rays_traced() const
184
    {
185
        return this->my_parameters.max_number_of_rays;
186
    }
187

188
    void set_tolerance(double tolerance)
189
    {
190
        this->my_parameters.tolerance = tolerance;
191
        return;
192
    }
193
    double get_tolerance() const
194
    {
195
        return this->my_parameters.tolerance;
196
    }
197

198
    /// @brief Set the seed used for the random number generation
199
    /// @param seed seed to set
200
    void set_seed(uint_fast64_t seed)
201
    {
202
        this->my_parameters.seed = seed;
203
        return;
204
    }
205

206
    /// @brief Get the seed used for random number generation
207
    /// @return current seed
208
    uint_fast64_t get_seed() const
209
    {
210
        return this->my_parameters.seed;
211
    }
212

213
    void set_latitude(double latitude)
214
    {
215
        this->my_parameters.latitude = latitude;
216
        return;
217
    }
218
    double get_latitude() const
219
    {
220
        return this->my_parameters.latitude;
221
    }
222

223
    void set_longitude(double longitude)
224
    {
225
        this->my_parameters.longitude = longitude;
226
        return;
227
    }
228
    double get_longitude() const
229
    {
230
        return this->my_parameters.longitude;
231
    }
232

233
    void set_simulation_date(const Date &d)
234
    {
235
        this->my_parameters.sim_dt.my_date = d;
236
        return;
237
    }
238
    const Date &get_simulation_date() const
239
    {
240
        return this->my_parameters.sim_dt.my_date;
241
    }
242

243
    void set_simulation_datetime(const DateTime &dt)
244
    {
245
        this->my_parameters.sim_dt = dt;
246
        return;
247
    }
248
    const DateTime &get_simulation_datetime() const
249
    {
250
        return this->my_parameters.sim_dt;
251
    }
252

253
    void set_simulation_time(const Time &t)
254
    {
255
        this->my_parameters.sim_dt.my_time = t;
256
        return;
257
    }
258
    const Time &get_simulation_time() const
259
    {
260
        return this->my_parameters.sim_dt.my_time;
261
    }
262

263
    SimulationParameters &get_simulation_parameters()
×
264
    {
265
        return this->my_parameters;
×
266
    }
267
    const SimulationParameters &get_simulation_parameters() const
25✔
268
    {
269
        return this->my_parameters;
25✔
270
    }
271

272
    int update_simulation_positions();
273
    int update_simulation_positions(const Time &);
274
    int update_simulation_positions(const Date &);
275
    int update_simulation_positions(const DateTime &);
276

277
    bool import_from_file(const char *file_name);
278
    bool import_from_file(const std::string file_name);
279

280
    /// @brief Import simulation data from a JSON file.
281
    /// @param file_name Path to the JSON file to import.
282
    /// @throws std::runtime_error if the file cannot be read or parsed.
283
    /// Loads simulation data from the specified JSON file and updates the current simulation state.
284
    void import_json_file(const std::string file_name);
285

286
    /// @brief Export simulation data to a JSON file.
287
    /// @param file_name Path to the JSON file to write.
288
    /// @throws std::runtime_error if the file cannot be written.
289
    /// Serializes the current simulation data and writes it to the specified JSON file.
290
    void export_json_file(const std::string file_name);
291

292
    /// @brief Clear elements and ray sources
293
    /// @param reset_parameters Optional bool to reset simulation parameters
294
    void clear(bool reset_parameters = false);
295

296
private:
297
    // mutable element_id next_element_id;
298

299
    uint_fast64_t number_of_elements;
300

301
    ElementContainer my_elements;
302
    RaySourceContainer my_sources;
303
    SimulationParameters my_parameters;
304

305
    // void add_single_element(element_id key, element_ptr el);
306
    // void add_composite_element(element_id key, element_ptr el);
307
    // uint_fast64_t remove_single_element(ElementContainer::iterator iter);
308
    // uint_fast64_t remove_composite_element(element_ptr el);
309
    uint_fast64_t add_subelements(element_ptr el);
310
    uint_fast64_t remove_subelements(element_ptr el);
311
};
312

313
} // namespace SolTrace::Data
314

315
/**
316
 * @}
317
 */
318

319
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc