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

NREL / SolTrace / 18761181207

23 Oct 2025 08:31PM UTC coverage: 89.633% (-0.3%) from 89.946%
18761181207

Pull #75

github

web-flow
Merge 50bcae2bd into f6f121007
Pull Request #75: New sun models

115 of 142 new or added lines in 8 files covered. (80.99%)

5 existing lines in 2 files now uncovered.

4418 of 4929 relevant lines covered (89.63%)

9120568.9 hits per line

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

92.39
/coretrace/simulation_runner/native_runner/native_runner.cpp
1

2
#include "native_runner.hpp"
3

4
#include <exception>
5
#include <map>
6
// #include <algorithm>
7

8
// SimulationData headers
9
#include "composite_element.hpp"
10
#include "element.hpp"
11
#include "simulation_parameters.hpp"
12
#include "simulation_data.hpp"
13
#include "simulation_data_export.hpp"
14

15
// NativeRunner headers
16
#include "native_runner_types.hpp"
17
#include "trace.hpp"
18

19
namespace SolTrace::NativeRunner
20
{
21

22
    NativeRunner::NativeRunner() : SimulationRunner(),
23✔
23
                                   as_power_tower(false),
23✔
24
                                   number_of_threads(1)
23✔
25
    {
26
    }
23✔
27

28
    NativeRunner::~NativeRunner()
23✔
29
    {
30
    }
23✔
31

32
    RunnerStatus NativeRunner::initialize()
22✔
33
    {
34
        return RunnerStatus::SUCCESS;
22✔
35
    }
36

37
    RunnerStatus NativeRunner::setup_simulation(const SimulationData *data)
22✔
38
    {
39

40
        RunnerStatus sts;
41

42
        sts = this->setup_parameters(data);
22✔
43

44
        if (sts == RunnerStatus::SUCCESS)
22✔
45
            sts = this->setup_sun(data);
22✔
46

47
        if (sts == RunnerStatus::SUCCESS)
22✔
48
            sts = this->setup_elements(data);
22✔
49

50
        return sts;
22✔
51
    }
52

53
    RunnerStatus NativeRunner::setup_parameters(const SimulationData *data)
22✔
54
    {
55
        // Get Parameter data
56
        // TODO: Check that these parameters are used as expected
57
        const SimulationParameters &sim_params = data->get_simulation_parameters();
22✔
58
        this->tsys.sim_errors_sunshape = sim_params.include_sun_shape_errors;
22✔
59
        this->tsys.sim_errors_optical = sim_params.include_optical_errors;
22✔
60
        this->tsys.sim_raycount = sim_params.number_of_rays;
22✔
61
        this->tsys.sim_raymax = sim_params.max_number_of_rays;
22✔
62
        this->tsys.seed = sim_params.seed;
22✔
63
        return RunnerStatus::SUCCESS;
22✔
64
    }
65

66
    RunnerStatus NativeRunner::setup_sun(const SimulationData *data)
23✔
67
    {
68
        // TODO: This should throw an error...
69
        // Get RaySource data (this runner assumes there is only the Sun)
70
        assert(data->get_number_of_ray_sources() == 1);
23✔
71

72
        ray_source_ptr sun = data->get_ray_source();
23✔
73
        vector_copy(this->tsys.Sun.Origin, sun->get_position());
23✔
74
        this->tsys.Sun.ShapeIndex = sun->get_shape();
23✔
75

76
        // Set sunshape data
77
        switch (sun->get_shape())
23✔
78
        {
79
        case SunShape::GAUSSIAN:
9✔
80
            this->tsys.Sun.Sigma = sun->get_sigma();
9✔
81
            break;
9✔
82
        case SunShape::PILLBOX:
10✔
83
            this->tsys.Sun.Sigma = sun->get_half_width();
10✔
84
            break;
10✔
85
        case SunShape::LIMBDARKENED:
1✔
86
            this->tsys.Sun.MaxAngle = 4.65; // [mrad]
1✔
87
            this->tsys.Sun.MaxIntensity = 1.0;
1✔
88
            break;
1✔
89
        case SunShape::BUIE_CSR: {
1✔
90
            this->tsys.Sun.MaxAngle = 43.6; // [mrad]
1✔
91
            this->tsys.Sun.MaxIntensity = 1.0;
1✔
92
            // Calculate kappa and gamma parameters
93
            // Creates the Buie (2003) sun shape based on CSR
94
            // [1] Buie, D., Dey, C., & Bosi, S. (2003). The effective size of the solar cone for solar concentrating systems. Solar energy, 74(2003), 417-427.
95
            // [2] Buie, D., Monger, A., & Dey, C. (2003). Sun shape distributions for terrestrial solar simulations. Solar Energy, 74(March 2003), 113-122.
96
            double csr = sun->get_circumsolar_ratio();
1✔
97
            double chi;
98
            if (csr > 0.145)
1✔
NEW
99
                chi = -0.04419909985804843 + csr * (1.401323894233574 + csr * (-0.3639746714505299 + csr * (-0.9579768560161194 + 1.1550475450828657 * csr)));
×
100
            else if (csr > 0.035)
1✔
101
                chi = 0.022652077593662934 + csr * (0.5252380349996234 + (2.5484334534423887 - 0.8763755326550412 * csr) * csr);
1✔
102
            else
NEW
103
                chi = 0.004733749294807862 + csr * (4.716738065192151 + csr * (-463.506669149804 + csr * (24745.88727411664 + csr * (-606122.7511711778 + 5521693.445014727 * csr))));
×
104
            this->tsys.Sun.buie_kappa = 0.9 * log(13.5 * chi) * pow(chi, -0.3);
1✔
105
            this->tsys.Sun.buie_gamma = 2.2 * log(0.52 * chi) * pow(chi, 0.43) - 0.1;
1✔
106
            break;
1✔
107
        }
108
        case SunShape::USER_DEFINED: {
2✔
109
            std::vector<double> angle, intensity;
2✔
110
            sun->get_user_data(angle, intensity);
2✔
111
            int npoints = angle.size();
2✔
112

113
            // Set user data
114
            this->tsys.Sun.MaxAngle = 0;
2✔
115
            this->tsys.Sun.MaxIntensity = 0;
2✔
116

117
            this->tsys.Sun.SunShapeAngle.resize(2 * npoints - 1);
2✔
118
            this->tsys.Sun.SunShapeIntensity.resize(2 * npoints - 1);
2✔
119

120
            for (int i = 0; i < npoints; i++)
38✔
121
            {
122
                this->tsys.Sun.SunShapeAngle[npoints + i - 1] = angle[i];
36✔
123
                this->tsys.Sun.SunShapeIntensity[npoints + i - 1] = intensity[i];
36✔
124

125
                if (angle[i] > this->tsys.Sun.MaxAngle)
36✔
126
                    this->tsys.Sun.MaxAngle = angle[i];
34✔
127
                if (intensity[i] > this->tsys.Sun.MaxIntensity)
36✔
128
                    this->tsys.Sun.MaxIntensity = intensity[i];
2✔
129
            }
130
            // fill negative angle side of array -> I don't think we need this.
131
            //for (int i = 0; i < npoints - 1; i++)
132
            //{
133
            //    this->tsys.Sun.SunShapeAngle[i] = -angle[npoints - i - 1];
134
            //    this->tsys.Sun.SunShapeIntensity[i] = intensity[npoints - i - 1];
135
            //}
136
            break;
2✔
137
        }
2✔
NEW
138
        default:
×
139
            // TODO: add error
NEW
140
            break;
×
141
        }
142

143
        return RunnerStatus::SUCCESS;
23✔
144
    }
23✔
145

146
    RunnerStatus NativeRunner::setup_elements(const SimulationData *data)
22✔
147
    {
148
        // TODO: Improve error messages from this function.
149

150
        RunnerStatus sts = RunnerStatus::SUCCESS;
22✔
151
        auto my_map = std::map<int_fast64_t, tstage_ptr>();
22✔
152
        // int_fast64_t current_stage_id = -1;
153
        tstage_ptr current_stage = nullptr;
22✔
154
        int_fast64_t element_number = 1;
22✔
155
        bool element_found_before_stage = false;
22✔
156

157
        for (auto iter = data->get_const_iterator();
22✔
158
             !data->is_at_end(iter);
8,661✔
159
             ++iter)
8,639✔
160
        {
161
            element_ptr el = iter->second;
8,639✔
162
            if (el->is_enabled() && el->is_stage())
8,639✔
163
            {
164
                tstage_ptr stage = make_tstage(el, this->eparams);
25✔
165
                auto retval = my_map.insert(
25✔
166
                    std::make_pair(el->get_stage(), stage));
50✔
167

168
                // current_stage_id = stage->stage_id;
169

170
                // std::cout << "Created stage " << el->get_stage()
171
                //           << " with " << stage->ElementList.size() << " elements"
172
                //           << std::endl;
173

174
                if (retval.second == false)
25✔
175
                {
176
                    // TODO: Duplicate stage numbers. Need to make an error
177
                    // message.
178
                    sts = RunnerStatus::ERROR;
×
179
                }
180

181
                current_stage = stage;
25✔
182
                element_number = 1;
25✔
183
            }
25✔
184
            else if (el->is_enabled() && el->is_single())
8,614✔
185
            {
186
                if (current_stage == nullptr)
8,366✔
187
                {
188
                    // throw std::runtime_error("No stage to add element to");
189
                    element_found_before_stage = true;
81✔
190
                    continue;
81✔
191
                }
192
                else if (el->get_stage() != current_stage->stage_id)
8,285✔
193
                {
194
                    throw std::runtime_error(
195
                        "Element does not match current stage");
×
196
                }
197

198
                telement_ptr elem = make_telement(iter->second,
16,570✔
199
                                                  element_number,
200
                                                  this->eparams);
8,285✔
201
                ++element_number;
8,285✔
202
                current_stage->ElementList.push_back(elem);
8,285✔
203
            }
8,285✔
204
        }
8,639✔
205

206
        if (my_map.size() != 0 && element_found_before_stage)
22✔
207
        {
208
            throw std::runtime_error("Element found without a stage");
×
209
        }
210

211
        if (my_map.size() == 0)
22✔
212
        {
213
            // No stage elements found in the passed in data. However,
214
            // the runner requires stages. So make a single stage
215
            // and put everything there. Note that the coordinates are
216
            // set to correspond to global coordinates. This is necessary
217
            // so that the element coordinate setup in make_element are
218
            // correct.
219
            int_fast64_t element_number = 1;
9✔
220
            auto stage = make_tstage(this->eparams);
9✔
221
            stage->ElementList.reserve(data->get_number_of_elements());
9✔
222
            for (auto iter = data->get_const_iterator();
9✔
223
                 !data->is_at_end(iter);
97✔
224
                 ++iter)
88✔
225
            {
226
                element_ptr el = iter->second;
88✔
227
                if (el->is_enabled() && el->is_single())
88✔
228
                {
229
                    telement_ptr tel = make_telement(el,
230
                                                     element_number,
231
                                                     this->eparams);
81✔
232
                    stage->ElementList.push_back(tel);
81✔
233
                    ++element_number;
81✔
234
                }
81✔
235
            }
88✔
236
            my_map.insert(std::make_pair(0, stage));
9✔
237
        }
9✔
238

239
        // std::map (according to the documentation) is automatically
240
        // ordered by the keys so inserting into a map will sort the stages
241
        // and we can just transfer the pointers, in order, to the StageList
242
        // simply by pulling them out of the map.
243
        int_fast64_t last_stage_id = -1;
22✔
244
        for (auto iter = my_map.cbegin();
22✔
245
             iter != my_map.cend();
56✔
246
             ++iter)
34✔
247
        {
248
            assert(last_stage_id < iter->first);
34✔
249
            last_stage_id = iter->first;
34✔
250
            this->tsys.StageList.push_back(iter->second);
34✔
251
        }
252

253
        if (sts == RunnerStatus::SUCCESS)
22✔
254
        {
255
            // std::cout << "Setting ZAperture..." << std::endl;
256
            // Compute and set ZAperture field in each element
257
            bool success = set_aperture_planes(&this->tsys);
22✔
258
            sts = success ? RunnerStatus::SUCCESS : RunnerStatus::ERROR;
22✔
259
        }
260

261
        return sts;
22✔
262
    }
22✔
263

264
    RunnerStatus NativeRunner::update_simulation(const SimulationData *data)
×
265
    {
266
        // TODO: Do a more efficient implementation of this?
267
        this->tsys.ClearAll();
×
268
        this->setup_simulation(data);
×
269
        return RunnerStatus::SUCCESS;
×
270
    }
271

272
    RunnerStatus NativeRunner::run_simulation()
22✔
273
    {
274
        bool trace_return = trace_native(
44✔
275
            &this->tsys,
276
            this->tsys.seed,
22✔
277
            this->tsys.sim_raycount,
22✔
278
            this->tsys.sim_raymax,
22✔
279
            this->tsys.sim_errors_sunshape,
22✔
280
            this->tsys.sim_errors_optical,
22✔
281
            this->as_power_tower);
22✔
282

283
        // this->tsys.CollectResults();
284

285
        return trace_return ? RunnerStatus::SUCCESS : RunnerStatus::ERROR;
22✔
286
    }
287

288
    RunnerStatus NativeRunner::report_simulation(SolTrace::Result::SimulationResult *result,
3✔
289
                                                 int level)
290
    {
291
        RunnerStatus retval = RunnerStatus::SUCCESS;
3✔
292

293
        const TSystem *sys = this->get_system();
3✔
294
        // const TRayData ray_data = sys->AllRayData;
295
        const TRayData ray_data = sys->RayData;
3✔
296
        std::map<unsigned int, SolTrace::Result::ray_record_ptr> ray_records;
3✔
297
        std::map<unsigned int, SolTrace::Result::ray_record_ptr>::iterator iter;
3✔
298
        size_t ndata = ray_data.Count();
3✔
299

300
        bool sts;
301
        Vector3d point, cosines;
3✔
302
        int element;
303
        int stage;
304
        unsigned int raynum;
305

306
        telement_ptr el = nullptr;
3✔
307
        element_id elid;
308
        SolTrace::Result::ray_record_ptr rec = nullptr;
3✔
309
        SolTrace::Result::interaction_ptr intr = nullptr;
3✔
310
        SolTrace::Result::RayEvent rev;
311

312
        for (size_t ii = 0; ii < ndata; ++ii)
62,247✔
313
        {
314
            sts = ray_data.Query(ii,
62,244✔
315
                                 point.data,
316
                                 cosines.data,
317
                                 &element,
318
                                 &stage,
319
                                 &raynum,
320
                                 &rev);
321

322
            if (!sts)
62,244✔
323
            {
324
                retval = RunnerStatus::ERROR;
×
325
                break;
×
326
            }
327

328
            // std::cout << "ii: " << ii
329
            //           << "\npoint: " << point
330
            //           << "\ndirection: " << cosines
331
            //           << "\nelement: " << element
332
            //           << "\nstage: " << stage
333
            //           << "\nraynum: " << raynum
334
            //           << "\nevent: " << ray_event_string(rev)
335
            //           << std::endl;
336

337
            iter = ray_records.find(raynum);
62,244✔
338
            if (iter == ray_records.end())
62,244✔
339
            {
340
                rec = SolTrace::Result::make_ray_record(raynum);
20,010✔
341
                result->add_ray_record(rec);
20,010✔
342
                ray_records[raynum] = rec;
20,010✔
343
                assert(rev == SolTrace::Result::RayEvent::CREATE);
20,010✔
344
            }
345
            else
346
            {
347
                rec = iter->second;
42,234✔
348
            }
349

350
            if (element > 0)
62,244✔
351
            {
352
                el = sys->StageList[stage - 1]->ElementList[element - 1];
35,633✔
353
                elid = el->sim_data_id;
35,633✔
354
            }
355
            else
356
            {
357
                elid = element;
26,611✔
358
            }
359

360
            intr = make_interaction_record(elid, rev, point, cosines);
62,244✔
361
            rec->add_interaction_record(intr);
62,244✔
362
        }
363

364
        return RunnerStatus::SUCCESS;
3✔
365
    }
3✔
366

367
    bool NativeRunner::set_aperture_planes(TSystem *tsys)
22✔
368
    {
369
        bool retval;
370

371
        for (auto iter = tsys->StageList.cbegin();
22✔
372
             iter != tsys->StageList.cend();
56✔
373
             ++iter)
34✔
374
        {
375
            retval = this->set_aperture_planes(*iter);
34✔
376
            if (!retval)
34✔
377
                break;
×
378
        }
379

380
        return retval;
22✔
381
    }
382

383
    bool NativeRunner::set_aperture_planes(tstage_ptr stage)
34✔
384
    {
385
        bool retval;
386

387
        for (auto eiter = stage->ElementList.begin();
34✔
388
             eiter != stage->ElementList.end();
8,400✔
389
             ++eiter)
8,366✔
390
        {
391
            retval = aperture_plane(*eiter);
8,366✔
392
            if (!retval)
8,366✔
393
                break;
×
394
        }
395

396
        return retval;
34✔
397
    }
398

399
    bool NativeRunner::aperture_plane(telement_ptr Element)
8,366✔
400
    {
401
        /*{Calculates the aperture plane of the element in element coord system.
402
        Applicable to rotationally symmetric apertures surfaces with small
403
        curvature: g, s, p, o, c, v, m, e, r, i.
404
          input - Element = Element record containing geometry of element
405
          output -
406
                 - Element.ZAperture  where ZAperture is the distance from
407
                   the origin to the plane.
408
        }*/
409

410
        Element->ZAperture =
16,732✔
411
            Element->icalc->compute_z_aperture(Element->aperture);
8,366✔
412

413
        return true;
8,366✔
414
    }
415

416
} // namespace SolTrace::NativeRunner
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