Skip to content

Commit 1205ddf

Browse files
code cleanup
1 parent a2008f0 commit 1205ddf

File tree

15 files changed

+100
-113
lines changed

15 files changed

+100
-113
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ data.add_flux(nu_min=1e14, nu_max=1e15, num_points=5, t=times, flux=flux, err=er
158158
- **ConfigParams**: `fwd_SSC``fwd_ssc`, `rvs_SSC``rvs_ssc`, `IC_cooling``ssc_cooling`, `KN``kn`
159159
- **PyRadiation**: `IC_cooling``ssc_cooling`, `SSC``ssc`, `KN``kn`
160160
- **Model Constructor**: `forward_rad``fwd_rad`, `reverse_rad``rvs_rad`
161-
- All parameter names now follow consistent snake_case convention
161+
- All parameter names now follow the consistent snake_case convention
162162

163163
#### **Enhanced Code Documentation**
164164
- **Comprehensive Comments**: Added detailed documentation to all major classes and structures

include/forward-shock.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* based on if the Ejecta class supports mass and energy injection methods.
2020
* <!-- ************************************************************************************** -->
2121
*/
22-
template <typename Ejecta, typename Medium>
22+
template <typename Ejecta>
2323
struct ForwardState {
2424
static constexpr bool mass_inject = HasDmdt<Ejecta>; ///< whether Ejecta class has dmdt method
2525
static constexpr bool energy_inject = HasDedt<Ejecta>; ///< whether Ejecta class has dedt method
@@ -62,7 +62,7 @@ struct ForwardState {
6262
template <typename Ejecta, typename Medium>
6363
class ForwardShockEqn {
6464
public:
65-
using State = ForwardState<Ejecta, Medium>;
65+
using State = ForwardState<Ejecta>;
6666

6767
/**
6868
* <!-- ************************************************************************************** -->

include/inverse-compton.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct ICPhoton {
148148

149149
/**
150150
* <!-- ************************************************************************************** -->
151-
* @brief Computes the base-2 logarithm of the photon specific intensity at a given frequency.
151+
* @brief Computes the base-2 logarithm of the photon-specific intensity at a given frequency.
152152
* @param log2_nu The base-2 logarithm of the frequency
153153
* @return The base-2 logarithm of the photon specific intensity at the given frequency
154154
* <!-- ************************************************************************************** -->
@@ -162,20 +162,16 @@ struct ICPhoton {
162162
private:
163163
void generate_grid();
164164

165-
void fill_integral_table();
166-
167165
Array nu0; // input frequency nu0 grid value
168166

169167
Array I_nu_dnu; // input I_nu
170168

171169
Array gamma; // gamma grid boundary values
172170

173-
Array N_e_dgamma; // electron column density
171+
Array dN_e; // electron column density
174172

175173
MeshGrid IC_tab;
176174

177-
Real min_nu_quested{con::inf};
178-
179175
static constexpr size_t gamma_grid_per_order{7}; // Number of frequency bins
180176

181177
static constexpr size_t nu_grid_per_order{5}; // Number of gamma bins
@@ -203,13 +199,13 @@ inline constexpr Real IC_x0 = 0.47140452079103166;
203199
* @tparam Photons Type of the photon distribution
204200
* @param electrons The electron grid
205201
* @param photons The photon grid
206-
* @param kn flag for Klein-Nishina
202+
* @param KN flag for Klein-Nishina
207203
* @return A 3D grid of IC photons
208204
* <!-- ************************************************************************************** -->
209205
*/
210206
template <typename Electrons, typename Photons>
211207
ICPhotonGrid<Electrons, Photons> generate_IC_photons(ElectronGrid<Electrons> const& electrons,
212-
PhotonGrid<Photons> const& photons, bool kn = true) noexcept;
208+
PhotonGrid<Photons> const& photons, bool KN = true) noexcept;
213209

214210
/**
215211
* <!-- ************************************************************************************** -->
@@ -245,9 +241,6 @@ template <typename Electrons, typename Photons>
245241
ICPhoton<Electrons, Photons>::ICPhoton(Electrons const& electrons, Photons const& photons, bool KN) noexcept
246242
: photons(photons), electrons(electrons), KN(KN) {}
247243

248-
template <typename Electrons, typename Photons>
249-
void ICPhoton<Electrons, Photons>::fill_integral_table() {}
250-
251244
template <typename Electrons, typename Photons>
252245
void ICPhoton<Electrons, Photons>::generate_grid() {
253246
const Real gamma_min = std::min(electrons.gamma_m, electrons.gamma_c);
@@ -258,24 +251,20 @@ void ICPhoton<Electrons, Photons>::generate_grid() {
258251
const Real nu_max = photons.nu_M * 10;
259252
const size_t nu_size = static_cast<size_t>(std::log10(nu_max / nu_min) * nu_grid_per_order);
260253

261-
Array dnu0;
262-
263-
Array dgamma;
254+
Array dnu0, dgamma;
264255

265256
logspace_boundary_center(std::log2(nu_min), std::log2(nu_max), nu_size, nu0, dnu0);
266257

267258
logspace_boundary_center(std::log2(gamma_min), std::log2(gamma_max), gamma_size, gamma, dgamma);
268259

269-
// I_nu = Array({nu_size}, -1);
270-
// column_den = Array({gamma_size}, -1);
271260
IC_tab = MeshGrid({gamma_size, nu_size}, -1);
272261
generated = true;
273262

274263
I_nu_dnu = Array::from_shape({nu_size});
275-
N_e_dgamma = Array::from_shape({gamma_size});
264+
dN_e = Array::from_shape({gamma_size});
276265

277266
for (size_t i = 0; i < gamma_size; ++i) {
278-
N_e_dgamma(i) = electrons.compute_column_den(gamma(i)) * dgamma(i);
267+
dN_e(i) = electrons.compute_column_den(gamma(i)) * dgamma(i);
279268
}
280269

281270
for (size_t j = 0; j < nu_size; ++j) {
@@ -299,7 +288,7 @@ Real ICPhoton<Electrons, Photons>::compute_I_nu(Real nu) {
299288
for (size_t i = gamma_size; i-- > 0;) {
300289
const Real gamma_i = gamma(i);
301290
const Real upscatter = 4 * IC_x0 * gamma_i * gamma_i;
302-
const Real Ndgamma = N_e_dgamma(i);
291+
const Real Ndgamma = dN_e(i);
303292

304293
if (nu > upscatter * nu0.back())
305294
break;
@@ -324,6 +313,7 @@ Real ICPhoton<Electrons, Photons>::compute_I_nu(Real nu) {
324313
break;
325314
}
326315
}
316+
327317
if (extrapolate) {
328318
IC_I_nu += Ndgamma *
329319
(IC_tab(i, 0) + (IC_tab(i, 0) - IC_tab(i, 1)) / (nu0(1) - nu0(0)) * (nu0(0) - nu / upscatter));

include/mesh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ Coord auto_grid(Ejecta const& jet, Array const& t_obs, Real theta_cut, Real thet
164164
* @tparam Ejecta Type of the jet/ejecta class
165165
* @param jet The jet/ejecta object
166166
* @param gamma_cut Lorentz factor cutoff value
167+
* @param phi_resol
168+
* @param theta_resol
167169
* @param is_axisymmetric Flag for axisymmetric jets
168170
* @return Angle (in radians) at which the jet's Lorentz factor drops to gamma_cut
169171
* <!-- ************************************************************************************** -->

include/observer.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ MeshGrid Observer::specific_flux(Array const& t_obs, Array const& nu_obs, Photon
333333

334334
InterpState state;
335335
for (size_t k = 0; k < t_grid - 1 && t_idx < t_obs_len; k++) {
336-
const Real t_hi = time(i, j, k + 1);
337-
if (t_hi < t_obs(t_idx)) {
338-
continue;
339-
} else {
336+
if (const Real t_hi = time(i, j, k + 1); t_hi >= t_obs(t_idx)) {
340337
const size_t idx_start = t_idx;
341338
iterate_to(t_hi, t_obs, t_idx);
342339
const size_t idx_end = t_idx;
@@ -395,7 +392,6 @@ Array Observer::specific_flux_series(Array const& t_obs, Array const& nu_obs, Ph
395392
while (t_idx < t_obs_len && k < t_grid - 1) {
396393
if (time(i, j, k + 1) < t_obs(t_idx)) {
397394
k++;
398-
continue;
399395
} else {
400396
if (set_boundaries(state, eff_i, i, j, k, lg2_nu_src(t_idx), photons)) [[likely]] {
401397
//F_nu(t_idx) += loglog_interpolate(state, lg2_t_obs(t_idx), lg2_t(i, j, k));

include/reverse-shock.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* injection capabilities.
1919
* <!-- ************************************************************************************** -->
2020
*/
21-
template <typename Ejecta, typename Medium>
21+
template <typename Ejecta>
2222
struct ReverseState {
2323
static constexpr bool mass_inject = HasDmdt<Ejecta>; ///< Whether ejecta has mass injection
2424
static constexpr bool energy_inject = HasDedt<Ejecta>; ///< Whether ejecta has energy injection
@@ -56,21 +56,21 @@ struct ReverseState {
5656
template <typename Ejecta, typename Medium>
5757
class FRShockEqn {
5858
public:
59-
using State = ReverseState<Ejecta, Medium>;
59+
using State = ReverseState<Ejecta>;
6060

6161
/**
6262
* <!-- ************************************************************************************** -->
6363
* @brief Constructor for the FRShockEqn class.
6464
* @details Initializes the forward-reverse shock equation with the given medium, ejecta, and parameters.
6565
* @param medium The medium through which the shock propagates
66-
* @param jet The ejecta driving the shock
66+
* @param ejecta The ejecta driving the shock
6767
* @param phi Azimuthal angle
6868
* @param theta Polar angle
6969
* @param rad_fwd Radiation params for forward shock
7070
* @param rad_rvs Radiation params for reverse shock
7171
* <!-- ************************************************************************************** -->
7272
*/
73-
FRShockEqn(Medium const& medium, Ejecta const& jet, Real phi, Real theta, RadParams const& rad_fwd,
73+
FRShockEqn(Medium const& medium, Ejecta const& ejecta, Real phi, Real theta, RadParams const& rad_fwd,
7474
RadParams const& rad_rvs);
7575

7676
Medium const& medium; ///< Reference to the medium properties

include/utilities.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ struct Tables {
346346
std::array<double, BUCKETS> log2_c{};
347347
std::array<uint64_t, BUCKETS> c_bits{}; // optional (not used in hot path)
348348
Tables() {
349-
const int shift = 52 - BUCKET_BITS;
350-
const uint64_t expbits = uint64_t(EXP_BIAS) << 52;
349+
constexpr int shift = 52 - BUCKET_BITS;
350+
constexpr uint64_t expbits = static_cast<uint64_t>(EXP_BIAS) << 52;
351351
for (size_t i = 0; i < BUCKETS; ++i) {
352-
uint64_t frac_mid = (uint64_t(i) << shift) | (uint64_t(1) << (shift - 1));
352+
const uint64_t frac_mid = (static_cast<uint64_t>(i) << shift) | (static_cast<uint64_t>(1) << (shift - 1));
353353
uint64_t bits = expbits | frac_mid; // center c_i bits
354-
double c = std::bit_cast<double>(bits);
354+
const double c = std::bit_cast<double>(bits);
355355
c_bits[i] = bits;
356356
inv_c[i] = 1.0 / c;
357357
log2_c[i] = std::log2(c);
@@ -366,17 +366,17 @@ inline const Tables& tables() {
366366

367367
inline double decompose_normals(double x, int& e) {
368368
uint64_t bits = std::bit_cast<uint64_t>(x);
369-
uint64_t expo = (bits >> 52) & EXP_MASK;
369+
const uint64_t expo = (bits >> 52) & EXP_MASK;
370370
if (expo == 0 || expo == EXP_MASK) {
371371
int ee;
372372
double m = std::frexp(x, &ee); // [0.5,1)
373373
m *= 2.0;
374374
e = ee - 1;
375375
return m; // -> [1,2)
376376
}
377-
e = int(expo) - EXP_BIAS;
378-
bits = (bits & FRAC_MASK) | (uint64_t(EXP_BIAS) << 52); // force mantissa exponent
379-
return std::bit_cast<double>(bits); // m in [1,2)
377+
e = static_cast<int>(expo) - EXP_BIAS;
378+
bits = (bits & FRAC_MASK) | (static_cast<uint64_t>(EXP_BIAS) << 52); // force mantissa exponent
379+
return std::bit_cast<double>(bits); // m in [1,2)
380380
}
381381

382382
/**

pybind/error_handling.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
namespace afterglow {
1414

15-
class ValidationError : public std::invalid_argument {
15+
class ValidationError final : public std::invalid_argument {
1616
public:
1717
explicit ValidationError(const std::string& message) : std::invalid_argument(message) {}
1818
explicit ValidationError(const char* message) : std::invalid_argument(message) {}
1919
};
2020

21-
class LogicError : public std::logic_error {
21+
class LogicError final : public std::logic_error {
2222
public:
2323
explicit LogicError(const std::string& message) : std::logic_error(message) {}
2424
explicit LogicError(const char* message) : std::logic_error(message) {}

pybind/mcmc.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ struct MultiBandModel {
268268
* @param data Complete multi-wavelength observational dataset for fitting
269269
* <!-- ************************************************************************************** -->
270270
*/
271-
MultiBandModel(MultiBandData data);
271+
explicit MultiBandModel(MultiBandData data);
272272

273273
/**
274274
* <!-- ************************************************************************************** -->
@@ -334,15 +334,15 @@ struct MultiBandModel {
334334
* @param t_min Minimum time for photon grid generation [seconds]
335335
* @param t_max Maximum time for photon grid generation [seconds]
336336
* @param obs Observer object for flux calculations
337-
* @param f_photons Forward shock synchrotron photon grid
338-
* @param r_photons Reverse shock synchrotron photon grid
339-
* @param f_IC_photons Forward shock inverse Compton photon grid
340-
* @param r_IC_photons Reverse shock inverse Compton photon grid
337+
* @param fwd_photons Forward shock synchrotron photon grid
338+
* @param rvs_photons Reverse shock synchrotron photon grid
339+
* @param fwd_IC_photons Forward shock inverse Compton photon grid
340+
* @param rvs_IC_photons Reverse shock inverse Compton photon grid
341341
* <!-- ************************************************************************************** -->
342342
*/
343343
template <typename ICPhotonGrid>
344-
void generate_photons(Params const& param, double t_min, double t_max, Observer& obs, SynPhotonGrid& f_photons,
345-
SynPhotonGrid& r_photons, ICPhotonGrid& f_IC_photons, ICPhotonGrid& r_IC_photons);
344+
void generate_photons(Params const& param, double t_min, double t_max, Observer& obs, SynPhotonGrid& fwd_photons,
345+
SynPhotonGrid& rvs_photons, ICPhotonGrid& fwd_IC_photons, ICPhotonGrid& rvs_IC_photons);
346346

347347
/**
348348
* <!-- ************************************************************************************** -->

pybind/pymodel.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ void save_photon_details(PhotonGrid const& photons, PyShock& details) {
199199
}
200200
}
201201

202-
void PyModel::single_evo_details(Shock const& shock, Coord const& coord, Array const& t_obs, Observer& obs,
203-
PyRadiation const& rad, PyShock& details) const {
202+
void PyModel::single_evo_details(Shock const& shock, Coord const& coord, Observer& obs, PyRadiation const& rad,
203+
PyShock& details) const {
204204
obs.observe(coord, shock, obs_setup.lumi_dist, obs_setup.z);
205205

206206
details.t_obs = obs.time / unit::sec;
@@ -239,7 +239,7 @@ auto PyModel::details(Real t_min, Real t_max) const -> PyDetails {
239239

240240
save_shock_details(fwd_shock, details.fwd);
241241

242-
single_evo_details(fwd_shock, coord, t_obs, observer, fwd_rad, details.fwd);
242+
single_evo_details(fwd_shock, coord, observer, fwd_rad, details.fwd);
243243

244244
return details;
245245
} else {
@@ -250,9 +250,9 @@ auto PyModel::details(Real t_min, Real t_max) const -> PyDetails {
250250

251251
save_shock_details(rvs_shock, details.rvs);
252252

253-
single_evo_details(fwd_shock, coord, t_obs, observer, fwd_rad, details.fwd);
253+
single_evo_details(fwd_shock, coord, observer, fwd_rad, details.fwd);
254254

255-
single_evo_details(rvs_shock, coord, t_obs, observer, rvs_rad, details.rvs);
255+
single_evo_details(rvs_shock, coord, observer, rvs_rad, details.rvs);
256256

257257
return details;
258258
}
@@ -284,8 +284,8 @@ auto PyModel::flux_density(PyArray const& t, PyArray const& nu) -> PyFlux {
284284
const Array t_obs = t * unit::sec;
285285
const Array nu_obs = nu * unit::Hz;
286286

287-
auto flux_func = [](Observer& obs, Array const& t, Array const& nu, auto& photons) -> XTArray {
288-
return obs.specific_flux_series(t, nu, photons) / unit::flux_den_cgs;
287+
auto flux_func = [](Observer& obs, Array const& time, Array const& freq, auto& photons) -> XTArray {
288+
return obs.specific_flux_series(time, freq, photons) / unit::flux_den_cgs;
289289
};
290290

291291
auto result = compute_emission(t_obs, nu_obs, flux_func);
@@ -300,8 +300,8 @@ auto PyModel::flux(PyArray const& t, double nu_min, double nu_max, size_t num_nu
300300
const Array nu_obs = xt::logspace(std::log10(nu_min * unit::Hz), std::log10(nu_max * unit::Hz), num_nu);
301301
const Array t_obs = t * unit::sec;
302302

303-
auto flux_func = [](Observer& obs, Array const& t, Array const& nu, auto& photons) -> XTArray {
304-
return obs.flux(t, nu, photons) / unit::flux_cgs;
303+
auto flux_func = [](Observer& obs, Array const& time, Array const& freq, auto& photons) -> XTArray {
304+
return obs.flux(time, freq, photons) / unit::flux_cgs;
305305
};
306306

307307
auto result = compute_emission(t_obs, nu_obs, flux_func);
@@ -377,8 +377,8 @@ auto PyModel::flux_density_exposures(PyArray const& t, PyArray const& nu, PyArra
377377

378378
const auto sampling = generate_exposure_sampling(t, nu, expo_time, num_points);
379379

380-
auto flux_func = [](Observer& obs, Array const& t, Array const& nu, auto& photons) -> XTArray {
381-
return obs.specific_flux_series(t, nu, photons) / unit::flux_den_cgs;
380+
auto flux_func = [](Observer& obs, Array const& time, Array const& freq, auto& photons) -> XTArray {
381+
return obs.specific_flux_series(time, freq, photons) / unit::flux_den_cgs;
382382
};
383383

384384
auto result = compute_emission(sampling.t_obs_sorted, sampling.nu_obs_sorted, flux_func);
@@ -395,8 +395,8 @@ auto PyModel::flux_density_grid(PyArray const& t, PyArray const& nu) -> PyFlux {
395395
const Array t_obs = t * unit::sec;
396396
const Array nu_obs = nu * unit::Hz;
397397

398-
auto flux_func = [](Observer& obs, Array const& t, Array const& nu, auto& photons) -> XTArray {
399-
return obs.specific_flux(t, nu, photons) / unit::flux_den_cgs;
398+
auto flux_func = [](Observer& obs, Array const& time, Array const& freq, auto& photons) -> XTArray {
399+
return obs.specific_flux(time, freq, photons) / unit::flux_den_cgs;
400400
};
401401

402402
auto result = compute_emission(t_obs, nu_obs, flux_func);

0 commit comments

Comments
 (0)