forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KernelContextException.cc
156 lines (146 loc) · 4.34 KB
/
KernelContextException.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/global/KernelContextException.cc
//---------------------------------------------------------------------------//
#include "KernelContextException.hh"
#include "corecel/OpaqueIdIO.hh"
#include "corecel/cont/ArrayIO.hh"
#include "corecel/io/JsonPimpl.hh"
#if CELERITAS_USE_JSON
# include "corecel/cont/ArrayIO.json.hh"
# include "corecel/math/QuantityIO.json.hh"
#endif
#include "CoreTrackView.hh"
namespace celeritas
{
namespace
{
//---------------------------------------------------------------------------//
#if CELERITAS_USE_JSON
template<class V, class S>
void insert_if_valid(char const* key,
OpaqueId<V, S> const& val,
nlohmann::json* obj)
{
if (val)
{
(*obj)[key] = val.unchecked_get();
}
}
#endif
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Construct with track data and kernel label.
*/
KernelContextException::KernelContextException(
HostCRef<CoreParamsData> const& params,
HostRef<CoreStateData> const& states,
ThreadId thread,
std::string_view label)
: thread_(thread), label_{label}
{
try
{
if (!thread)
{
// Make sure the thread is valid before trying to construct
// detailed debug information
throw std::exception();
}
CoreTrackView core(params, states, thread);
this->initialize(core);
}
catch (...)
{
// Ignore all exceptions while trying to process diagnostic information
what_ = label_ + " (error processing track state)";
}
}
//---------------------------------------------------------------------------//
/*!
* This class type's description.
*/
char const* KernelContextException::type() const
{
return "KernelContextException";
}
//---------------------------------------------------------------------------//
/*!
* Save context to a JSON object.
*/
void KernelContextException::output(JsonPimpl* json) const
{
#if CELERITAS_USE_JSON
nlohmann::json j;
# define KCE_INSERT_IF_VALID(ATTR) insert_if_valid(#ATTR, ATTR##_, &j)
KCE_INSERT_IF_VALID(thread);
KCE_INSERT_IF_VALID(track_slot);
KCE_INSERT_IF_VALID(event);
KCE_INSERT_IF_VALID(track);
if (track_)
{
KCE_INSERT_IF_VALID(parent);
j["num_steps"] = num_steps_;
KCE_INSERT_IF_VALID(particle);
j["energy"] = energy_;
j["pos"] = pos_;
j["dir"] = dir_;
KCE_INSERT_IF_VALID(volume);
KCE_INSERT_IF_VALID(surface);
}
if (!label_.empty())
{
j["label"] = label_;
}
# undef KCE_INSERT_IF_VALID
json->obj = std::move(j);
#else
CELER_DISCARD(json);
#endif
}
//---------------------------------------------------------------------------//
/*!
* Populate properties during construction.
*/
void KernelContextException::initialize(CoreTrackView const& core)
{
track_slot_ = core.track_slot_id();
auto const&& sim = core.make_sim_view();
if (sim.status() == TrackStatus::alive)
{
event_ = sim.event_id();
track_ = sim.track_id();
parent_ = sim.parent_id();
num_steps_ = sim.num_steps();
{
auto const&& par = core.make_particle_view();
particle_ = par.particle_id();
energy_ = par.energy();
}
{
auto const&& geo = core.make_geo_view();
pos_ = geo.pos();
dir_ = geo.dir();
volume_ = geo.volume_id();
surface_ = geo.surface_id();
}
}
{
// Construct std::exception message
std::ostringstream os;
os << "kernel context: track slot " << track_slot_ << " in '" << label_
<< "'";
if (track_)
{
os << ", track " << track_ << " of event " << event_;
}
what_ = os.str();
}
}
//---------------------------------------------------------------------------//
} // namespace celeritas