Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions src/core/lbl/lbl_lineshape_voigt_ecs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <algorithm>
#include <cmath>
#include <exception>
#include <functional>
#include <ranges>
#include <stdexcept>

Expand Down Expand Up @@ -176,7 +177,7 @@ void ComputeData::adapt_multi(const QuantumIdentifier& bnd_qid,
for (Size i = 0; i < n; i++) {
const auto& line = bnd.lines[i];
pop[i] = line.gu * exp(-line.e0 / (Constant::k * T)) / QT;
dip[i] = 0.5 * Constant::c *
dip[i] = 0.5 * Constant::c *
std::sqrt(line.a / (Math::pow3(line.f0) * Constant::two_pi));

if (bnd.lineshape == LineByLineLineshape::VP_ECS_MAKAROV) {
Expand All @@ -190,10 +191,10 @@ void ComputeData::adapt_multi(const QuantumIdentifier& bnd_qid,
dipr[i] = hartmann::reduced_dipole(J.upper, J.lower, l2.upper, l2.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
} else if (bnd.lineshape == LineByLineLineshape::VP_ECS_STOTOP) {
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
auto& Kq = bnd.lines[i].qn.at(QuantumNumberType::K);
dipr[i] = stotop::reduced_dipole(J.upper, J.lower, Kq.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
auto& Kq = bnd.lines[i].qn.at(QuantumNumberType::K);
dipr[i] = stotop::reduced_dipole(J.upper, J.lower, Kq.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
} else if (bnd.lineshape == LineByLineLineshape::VP_ECS_SPHTOP) {
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
dipr[i] = sphtop::reduced_dipole(J.upper, J.lower);
Expand All @@ -203,17 +204,13 @@ void ComputeData::adapt_multi(const QuantumIdentifier& bnd_qid,

//! Must remember the sorting for the quantum numbers
if (not presorted) {
std::iota(sort.begin(), sort.end(), 0);
bubble_sort_by(
[&](Size i, Size j) {
const auto a = bnd.lines[sort[i]].f0 * pop[i] * dip[i] * dip[i];
const auto b = bnd.lines[sort[j]].f0 * pop[j] * dip[j] * dip[j];
return b > a;
},
sort,
pop,
dip,
dipr);
stdr::iota(sort, 0);
stdr::sort(
stdv::zip(sort, pop, dip, dipr), stdr::greater(), [&](const auto& v) {
const auto& [i, pop_i, dip_i, dipr_i] = v;
const auto a = bnd.lines[i].f0 * pop_i * dip_i * dip_i;
return a;
});
} else {
const auto presorter = [this](const auto& vec) {
auto out = vec;
Expand Down Expand Up @@ -318,7 +315,7 @@ void ComputeData::adapt_single(const QuantumIdentifier& bnd_qid,
for (Size i = 0; i < n; i++) {
const auto& line = bnd.lines[i];
pop[i] = line.gu * exp(-line.e0 / (Constant::k * T)) / QT;
dip[i] = 0.5 * Constant::c *
dip[i] = 0.5 * Constant::c *
std::sqrt(line.a / (Math::pow3(line.f0) * Constant::two_pi));

if (bnd.lineshape == LineByLineLineshape::VP_ECS_MAKAROV) {
Expand All @@ -332,10 +329,10 @@ void ComputeData::adapt_single(const QuantumIdentifier& bnd_qid,
dipr[i] = hartmann::reduced_dipole(J.upper, J.lower, l2.upper, l2.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
} else if (bnd.lineshape == LineByLineLineshape::VP_ECS_STOTOP) {
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
auto& Kq = bnd.lines[i].qn.at(QuantumNumberType::K);
dipr[i] = stotop::reduced_dipole(J.upper, J.lower, Kq.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
auto& Kq = bnd.lines[i].qn.at(QuantumNumberType::K);
dipr[i] = stotop::reduced_dipole(J.upper, J.lower, Kq.lower);
dip[i] *= std::signbit(dipr[i]) ? -1 : 1;
} else if (bnd.lineshape == LineByLineLineshape::VP_ECS_SPHTOP) {
auto& J = bnd.lines[i].qn.at(QuantumNumberType::J);
dipr[i] = sphtop::reduced_dipole(J.upper, J.lower);
Expand All @@ -345,17 +342,13 @@ void ComputeData::adapt_single(const QuantumIdentifier& bnd_qid,

//! Must remember the sorting for the quantum numbers
if (not presorted) {
std::iota(sort.begin(), sort.end(), 0);
bubble_sort_by(
[&](Size i, Size j) {
const auto a = bnd.lines[sort[i]].f0 * pop[i] * dip[i] * dip[i];
const auto b = bnd.lines[sort[j]].f0 * pop[j] * dip[j] * dip[j];
return b > a;
},
sort,
pop,
dip,
dipr);
stdr::iota(sort, 0);
stdr::sort(
stdv::zip(sort, pop, dip, dipr), stdr::greater(), [&](const auto& v) {
const auto& [i, pop_i, dip_i, dipr_i] = v;
const auto a = bnd.lines[i].f0 * pop_i * dip_i * dip_i;
return a;
});
} else {
const auto presorter = [this](const auto& vec) {
auto out = vec;
Expand Down
7 changes: 3 additions & 4 deletions src/core/lbl/lbl_lineshape_voigt_lte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,9 @@ void band_shape_helper(std::vector<single_shape>& lines,
} break;
}

bubble_sort_by(
[&](const Size l1, const Size l2) { return lines[l1].f0 > lines[l2].f0; },
lines,
pos);
stdr::sort(stdv::zip(lines, pos), {}, [](const auto& x) {
return std::get<0>(x).f0;
});
}

band_shape::band_shape(std::vector<single_shape>&& ls, const Numeric cut)
Expand Down
8 changes: 4 additions & 4 deletions src/core/lbl/lbl_lineshape_voigt_lte_mirrored.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sorting.h>

#include <Faddeeva.hh>
#include <algorithm>
#include <cmath>
#include <numeric>

Expand Down Expand Up @@ -421,10 +422,9 @@ void band_shape_helper(std::vector<single_shape>& lines,
} break;
}

bubble_sort_by(
[&](const Size l1, const Size l2) { return lines[l1].f0 > lines[l2].f0; },
lines,
pos);
stdr::sort(stdv::zip(lines, pos), {}, [](const auto& x) {
return std::get<0>(x).f0;
});
}

band_shape::band_shape(std::vector<single_shape>&& ls, const Numeric cut)
Expand Down
7 changes: 3 additions & 4 deletions src/core/lbl/lbl_lineshape_voigt_nlte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,9 @@ void band_shape_helper(std::vector<single_shape>& lines,
} break;
}

bubble_sort_by(
[&](const Size l1, const Size l2) { return lines[l1].f0 > lines[l2].f0; },
lines,
pos);
stdr::sort(stdv::zip(lines, pos), {}, [](const auto& x) {
return std::get<0>(x).f0;
});
}

band_shape::band_shape(std::vector<single_shape>&& ls, const Numeric cut)
Expand Down
29 changes: 0 additions & 29 deletions src/core/util/sorting.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,4 @@ void get_sorted_indexes(ArrayOfIndex& sorted, const T& data) {
});
}

template <typename T>
concept bubble_sorting_function = requires(T a) {
{ a(Size{}, Size{}) } -> std::same_as<bool>;
};

template <typename T>
concept bubble_sortable = requires(T& a) {
std::swap(a[Size{}], a[Size{}]);
{ a.size() } -> std::convertible_to<Size>;
};

template <bubble_sorting_function SortFn,
bubble_sortable T,
bubble_sortable... Ts>
void bubble_sort_by(const SortFn& sort_fn,
T& data_0,
Ts&... data_n) {
const Size n = static_cast<Size>(data_0.size());
assert((... and (n == static_cast<Size>(data_n.size()))));
for (Size i = 0; i < n; i++) {
for (Size j = i + 1; j < n; j++) {
if (sort_fn(i, j)) {
std::swap(data_0[i], data_0[j]);
(std::swap(data_n[i], data_n[j]), ...);
}
}
}
}

#endif /* sorting_h */
7 changes: 3 additions & 4 deletions src/m_lbl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,9 @@ void abs_bandsLineMixingAdaptation(AbsorptionBands& abs_bands,

for (Size i = 0; i < M; i++) {
for (Size j = 0; j < K; j++) {
auto s = eqv_str[i, j, joker];
auto v = eqv_val[i, j, joker];
bubble_sort_by(
[&](auto I1, auto I2) { return v[I1].real() > v[I2].real(); }, s, v);
stdr::sort(stdv::zip(eqv_val[i, j], eqv_str[i, j]),
{},
[](const auto& x) { return std::get<0>(x).real(); });
}
}

Expand Down
32 changes: 15 additions & 17 deletions src/python_interface/py_disort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <concepts>
#include <optional>
#include <ranges>

#include "configtypes.h"
#include "debug.h"
Expand Down Expand Up @@ -75,8 +76,8 @@ void py_disort(py::module_& m) try {
py::class_<MatrixOfDisortBDRF> mat_disbdrf(m, "MatrixOfDisortBDRF");
generic_interface(mat_disbdrf);

auto vecs = py::bind_vector<std::vector<DisortBDRF>,
py::rv_policy::reference_internal>(disort_nm,
auto vecs = py::bind_vector<std::vector<DisortBDRF>,
py::rv_policy::reference_internal>(disort_nm,
"ArrayOfBDRF");
vecs.doc() = "An array of BDRF functions";
generic_interface(vecs);
Expand Down Expand Up @@ -197,11 +198,10 @@ The relevant references are:
"pydisort_u",
[](disort::main_data& dis, Vector tau_, const Vector& phi) {
std::vector<Index> sorting(tau_.size());
std::iota(sorting.begin(), sorting.end(), 0);
bubble_sort_by(
[&tau_](auto il, auto jl) { return tau_[il] > tau_[jl]; },
sorting,
tau_);
stdr::iota(sorting, 0);
stdr::sort(stdv::zip(sorting, tau_), {}, [](const auto& x) {
return std::get<1>(x);
});

AscendingGrid tau{std::move(tau_)};
Tensor3 res(tau.size(), phi.size(), dis.mu().size());
Expand All @@ -220,11 +220,10 @@ The relevant references are:
"pydisort_flux_up",
[](disort::main_data& dis, Vector tau_) {
std::vector<Index> sorting(tau_.size());
std::iota(sorting.begin(), sorting.end(), 0);
bubble_sort_by(
[&tau_](auto il, auto jl) { return tau_[il] > tau_[jl]; },
sorting,
tau_);
stdr::iota(sorting, 0);
stdr::sort(stdv::zip(sorting, tau_), {}, [](const auto& x) {
return std::get<1>(x);
});

AscendingGrid tau{std::move(tau_)};
Matrix res(3, tau.size());
Expand All @@ -242,11 +241,10 @@ The relevant references are:
"pydisort_flux_down",
[](disort::main_data& dis, Vector tau_) {
std::vector<Index> sorting(tau_.size());
std::iota(sorting.begin(), sorting.end(), 0);
bubble_sort_by(
[&tau_](auto il, auto jl) { return tau_[il] > tau_[jl]; },
sorting,
tau_);
stdr::iota(sorting, 0);
stdr::sort(stdv::zip(sorting, tau_), {}, [](const auto& x) {
return std::get<1>(x);
});

AscendingGrid tau{std::move(tau_)};
Matrix res(3, tau.size());
Expand Down
8 changes: 3 additions & 5 deletions src/workspace_meta_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,9 @@ Equivalent (mostly) Python code:
"Output generic variables not supported in meta-functions");
}

bubble_sort_by([&wsm](Size i, Size j) { return wsm.gin[i] < wsm.gin[j]; },
wsm.gin,
wsm.gin_type,
wsm.gin_value,
wsm.gin_desc);
stdr::sort(stdv::zip(wsm.gin, wsm.gin_type, wsm.gin_value, wsm.gin_desc),
stdr::greater{},
[](const auto& x) { return std::get<0>(x); });

for (auto ptr = stdr::adjacent_find(wsm.gin); ptr != wsm.gin.end();
ptr = stdr::adjacent_find(wsm.gin)) {
Expand Down
Loading