• 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

89.32
/coretrace/simulation_data/simulation_data.cpp
1
#include "simulation_data.hpp"
2

3
#include <cassert>
4

5
#include "composite_element.hpp"
6
#include "simdata_io.hpp"
7

8
namespace SolTrace::Data {
9

10
SimulationData::SimulationData() : number_of_elements(0),
52✔
11
                                   my_elements(1),
52✔
12
                                   my_sources(0)
52✔
13
{
14
    return;
52✔
15
}
16

17
SimulationData::~SimulationData()
52✔
18
{
19
    return;
52✔
20
}
52✔
21

22
element_id SimulationData::add_element(element_ptr el)
19,821✔
23
{
24
    element_id id = ELEMENT_ERROR;
19,821✔
25

26
    if (el->get_id() != ELEMENT_ID_UNASSIGNED)
19,821✔
27
    {
28
        id = ELEMENT_ALREADY_REGISTERED;
1✔
29
    }
30
    else
31
    {
32
        id = this->my_elements.add_item(el);
19,820✔
33
        if (Element::is_success(id))
19,820✔
34
        {
35
            // Check that all fields required from the user have been specified
36
            el->enforce_user_fields_set();
19,820✔
37
            // Make sure coordinate stuff has been computed
38
            el->compute_coordinate_rotations();
19,816✔
39
            el->set_id(id);
19,816✔
40
            if (el->is_composite())
19,816✔
41
            {
42
                // The CompositeElement itself does not count toward the
43
                // number of elements since it does not impact the ray
44
                // tracing computation. So we do not increment the
45
                // number of elements here.
46
                uint_fast64_t n = this->add_subelements(el);
296✔
47
                assert(n == el->get_number_of_elements());
296✔
48
            }
49
            else
50
            {
51
                this->number_of_elements++;
19,520✔
52
            }
53
        }
54
        else
55
        {
56
            // TODO: Throw an error here
57
        }
58
    }
59

60
    return id;
19,817✔
61
}
62

63
// void SimulationData::add_single_element(element_id key,
64
//                                         element_ptr el)
65
// {
66
//     SingleElementMap::value_type to_insert(key, el);
67
//     auto result = this->my_elements.insert(to_insert);
68
//     assert(result.second);
69
//     return;
70
// }
71

72
uint_fast64_t SimulationData::add_subelements(element_ptr el)
298✔
73
{
74
    // CompositeElementMap::value_type to_insert(key, el);
75
    // auto result = this->composite_elements.insert(to_insert);
76
    // assert(result.second);
77

78
    composite_element_ptr cptr =
79
        std::dynamic_pointer_cast<CompositeElement>(el);
298✔
80
    assert(cptr != nullptr);
298✔
81

82
    uint_fast64_t nadded = cptr->get_number_of_elements();
298✔
83

84
    auto iter = cptr->get_const_iterator();
298✔
85
    while (!cptr->is_at_end(iter))
20,046✔
86
    {
87
        auto id = this->add_element(iter->second);
19,748✔
88
        assert(Element::is_success(id));
19,748✔
89
        // // Make sure coordinate stuff has been computed
90
        // iter->second->compute_coordinate_rotations();
91
        ++iter;
19,748✔
92
    }
93

94
    return nadded;
298✔
95
}
298✔
96

97
bool SimulationData::replace_element(element_id id, element_ptr el)
5✔
98
{
99
    bool success = false;
5✔
100
    assert(el->get_id() == ELEMENT_ID_UNASSIGNED);
5✔
101
    element_ptr old_el = this->get_element(id);
5✔
102
    if (old_el != nullptr)
5✔
103
    {
104
        success = this->my_elements.replace_item(id, el);
4✔
105
        if (success)
4✔
106
        {
107
            old_el->set_id(ELEMENT_ID_UNASSIGNED);
4✔
108
            if (old_el->is_composite())
4✔
109
            {
110
                this->remove_subelements(old_el);
2✔
111
            }
112
            else
113
            {
114
                this->number_of_elements--;
2✔
115
            }
116
            // Make sure coordinate stuff has been computed
117
            el->compute_coordinate_rotations();
4✔
118
            el->set_id(id);
4✔
119
            if (el->is_composite())
4✔
120
            {
121
                this->add_subelements(el);
2✔
122
            }
123
            else
124
            {
125
                this->number_of_elements++;
2✔
126
            }
127
            // this->number_of_elements -= old_el->get_number_of_elements();
128
            // this->number_of_elements += el->get_number_of_elements();
129
        }
130
    }
131

132
    return success;
5✔
133
}
5✔
134

135
element_ptr SimulationData::get_element(element_id id) const
106,826✔
136
{
137
    return this->my_elements.get_item(id);
106,826✔
138
    // element_ptr retval = nullptr;
139
    // auto iter1 = this->my_elements.find(id);
140
    // auto iter2 = this->composite_elements.find(id);
141
    // if (iter1 != this->my_elements.end())
142
    // {
143
    //     retval = iter1->second;
144
    // }
145
    // else if (iter2 != this->composite_elements.end())
146
    // {
147
    //     retval = iter2->second;
148
    // }
149
    // else
150
    // {
151
    //     // Intentional no-op
152
    // }
153
    // return retval;
154
}
155

156
uint_fast64_t SimulationData::remove_element(element_id id)
10✔
157
{
158

159
    uint_fast64_t nremoved = 0;
10✔
160
    element_ptr el = this->my_elements.get_item(id);
10✔
161

162
    if (el != nullptr)
10✔
163
    {
164
        this->my_elements.remove_item(id);
9✔
165
        el->set_id(ELEMENT_ID_UNASSIGNED);
9✔
166
        if (el->is_composite())
9✔
167
        {
168
            // The CompositeElement itself does not count toward the number
169
            // of elements since it does not impact the ray tracing computation.
170
            // So we do not decrement the number of elements here.
171
            nremoved = this->remove_subelements(el);
1✔
172
            assert(nremoved == el->get_number_of_elements());
1✔
173
        }
174
        else
175
        {
176
            this->number_of_elements--;
8✔
177
            nremoved = 1;
8✔
178
        }
179
    }
180

181
    return nremoved;
10✔
182

183
    // auto iter2 = this->composite_elements.find(id);
184
    // if (iter1 != this->my_elements.end())
185
    // {
186
    //     retval = remove_single_element(iter1);
187
    // }
188
    // else if (iter2 != this->composite_elements.end())
189
    // {
190
    //     retval = remove_composite_element(iter2);
191
    // }
192
    // else
193
    // {
194
    //     // Intentional no-op
195
    // }
196
    // return retval;
197
}
10✔
198

199
// uint_fast64_t SimulationData::remove_single_element(
200
//     SingleElementMap::iterator iter)
201
// {
202
//     iter->second->set_id(ELEMENT_ID_UNASSIGNED);
203
//     this->my_elements.erase(iter);
204
//     return 1;
205
// }
206

207
// uint_fast64_t SimulationData::remove_composite_element(element_ptr el)
208
// {
209
//     composite_element_ptr cptr = iter->second;
210
//     iter->second->set_id(ELEMENT_ID_UNASSIGNED);
211
//     this->composite_elements.erase(iter);
212
//     return this->remove_subelements(cptr);
213
// }
214

215
uint_fast64_t SimulationData::remove_subelements(element_ptr el)
3✔
216
{
217
    uint_fast64_t retval = 0;
3✔
218

219
    composite_element_ptr cptr =
220
        std::dynamic_pointer_cast<CompositeElement>(el);
3✔
221
    assert(cptr != nullptr);
3✔
222

223
    element_ptr ptr = nullptr;
3✔
224
    for (auto iter = cptr->get_iterator(); !cptr->is_at_end(iter); ++iter)
10✔
225
    {
226
        ptr = iter->second;
7✔
227
        retval += this->remove_element(ptr->get_id());
7✔
228
    }
229

230
    assert(retval == cptr->get_number_of_elements());
3✔
231

232
    return retval;
3✔
233
}
3✔
234

235
int SimulationData::update_simulation_positions()
×
236
{
237
    int sts = 0;
×
238
    // TODO: Implement this
239
    return sts;
×
240
}
241

242
int SimulationData::update_simulation_positions(const Time &t)
×
243
{
244
    this->my_parameters.sim_dt.my_time = t;
245
    return this->update_simulation_positions();
×
246
}
247

248
int SimulationData::update_simulation_positions(const Date &d)
×
249
{
250
    this->my_parameters.sim_dt.my_date = d;
251
    return this->update_simulation_positions();
×
252
}
253

254
int SimulationData::update_simulation_positions(const DateTime &dt)
×
255
{
256
    this->my_parameters.sim_dt.set(dt);
×
257
    return this->update_simulation_positions();
×
258
}
259

260
bool SimulationData::import_from_file(const char *file_name)
7✔
261
{
262
    return load_stinput_file(*this, file_name);
14✔
263
}
264

265
bool SimulationData::import_from_file(const std::string file_name)
7✔
266
{
267
    return this->import_from_file(file_name.c_str());
7✔
268
}
269

270
void SimulationData::import_json_file(const std::string file_name)
9✔
271
{
272
    load_json_file(*this, file_name);
9✔
273
}
6✔
274

275
void SimulationData::export_json_file(const std::string file_name)
10✔
276
{
277
    write_json_file(*this, file_name);
10✔
278
}
10✔
279

280
void SimulationData::clear(bool reset_parameters)
9✔
281
{
282
    this->my_elements.clear();
9✔
283
    this->my_sources.clear();
9✔
284
    this->number_of_elements = 0;
9✔
285

286
    if (reset_parameters)
9✔
NEW
287
        this->my_parameters = SimulationParameters();   // Reset
×
288
}
9✔
289

290
} // namespace SolTrace::Data
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