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
1 change: 1 addition & 0 deletions src/core/atm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(atm STATIC
atm_field.cpp
atm_spectral_field.cpp
xml_atm.cpp
)

Expand Down
1 change: 1 addition & 0 deletions src/core/atm/atm.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "atm_field.h"
#include "atm_spectral_field.h"
#include "xml_atm.h"
47 changes: 33 additions & 14 deletions src/core/atm/atm_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <configtypes.h>
#include <debug.h>
#include <enumsFieldComponent.h>
#include <field_concepts.h>
#include <functional_atm_field_interp.h>
#include <hitran_species.h>
#include <isotopologues.h>
Expand Down Expand Up @@ -644,29 +645,45 @@ VectorView Data::flat_view() {
}

namespace {
std::vector<std::pair<Index, Numeric>> flat_weight_(const GeodeticField3 &data, Numeric alt, Numeric lat, Numeric lon) {
return interp::flat_weight(data, alt, lat, lon);
void flat_weight_(std::vector<std::pair<Index, Numeric>> &weights,
const GeodeticField3 &data,
Numeric alt,
Numeric lat,
Numeric lon) {
interp::flat_weight(weights, data, alt, lat, lon);
}

std::vector<std::pair<Index, Numeric>> flat_weight_(Numeric, Numeric, Numeric, Numeric) {
constexpr std::pair<Index, Numeric> v1{0, 1.0};
return {v1};
void flat_weight_(std::vector<std::pair<Index, Numeric>> &weights, Numeric, Numeric, Numeric, Numeric) {
weights.emplace_back(0, 1.0);
}

std::vector<std::pair<Index, Numeric>> flat_weight_(const FunctionalData &f, Numeric alt, Numeric lat, Numeric lon) {
return ternary::get_w(f.f, alt, lat, lon);
void flat_weight_(std::vector<std::pair<Index, Numeric>> &weights,
const FunctionalData &f,
Numeric alt,
Numeric lat,
Numeric lon) {
weights.append_range(ternary::get_w(f.f, alt, lat, lon));
}
} // namespace

//! Flat weights for the positions in an atmosphere
std::vector<std::pair<Index, Numeric>> Data::flat_weight(const Numeric alt,
const Numeric lat,
const Numeric lon) const {
return std::visit([&](auto &v) { return flat_weight_(v, alt, lat, lon); }, data);
void Data::flat_weight(std::vector<std::pair<Index, Numeric>> &weights,
const Numeric alt,
const Numeric lat,
const Numeric lon) const {
weights.clear();
std::visit([&](auto &v) { flat_weight_(weights, v, alt, lat, lon); }, data);
// The derivative of the log-interpolated value v = exp(sum w ln x) by a stored x is w v / x
if (log_interpolation and std::holds_alternative<GeodeticField3>(data)) {
const ConstVectorView x = flat_view();
Numeric ln_v = 0.0;
for (const auto &[j, w] : weights) ln_v += w * std::log(x[j]);
const Numeric v = std::exp(ln_v);
for (auto &[j, w] : weights) w *= v / x[j];
}
}

std::vector<std::pair<Index, Numeric>> Data::flat_weight(const Vector3 pos) const {
return flat_weight(pos[0], pos[1], pos[2]);
void Data::flat_weight(std::vector<std::pair<Index, Numeric>> &weights, const Vector3 pos) const {
flat_weight(weights, pos[0], pos[1], pos[2]);
}

bool Data::ok() const {
Expand Down Expand Up @@ -1240,3 +1257,5 @@ std::string std::formatter<AtmField>::to_string(const AtmField &v) const {
static_assert(std::same_as<typename AtmField::KeyVal, AtmKeyVal>,
"The order of arguments in the template of which Field inherits from is "
"wrong. KeyVal must be defined in the same way for this to work.");

static_assert(field<AtmField, AtmKeyVal, Numeric, Numeric, Numeric>, "The atmospheric field must satisfy the field concepts");
11 changes: 6 additions & 5 deletions src/core/atm/atm_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,13 @@ struct Data {

[[nodiscard]] VectorView flat_view();

//! Flat weights for the positions in an atmosphere
[[nodiscard]] std::vector<std::pair<Index, Numeric>> flat_weight(const Numeric alt,
const Numeric lat,
const Numeric lon) const;
//! Flat weights for the positions in an atmosphere, into weights, which is cleared first to reuse its allocation
void flat_weight(std::vector<std::pair<Index, Numeric>> &weights,
const Numeric alt,
const Numeric lat,
const Numeric lon) const;

[[nodiscard]] std::vector<std::pair<Index, Numeric>> flat_weight(const Vector3 pos) const;
void flat_weight(std::vector<std::pair<Index, Numeric>> &weights, const Vector3 pos) const;

[[nodiscard]] bool ok() const;

Expand Down
Loading
Loading