LCOV - code coverage report
Current view: top level - src/system - GreenTensor.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 74 36.5 %
Date: 2025-05-02 21:49:25 Functions: 6 22 27.3 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2025 Pairinteraction Developers
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #include "pairinteraction/system/GreenTensor.hpp"
       5             : 
       6             : #include "pairinteraction/utils/eigen_assertion.hpp"
       7             : #include "pairinteraction/utils/eigen_compat.hpp"
       8             : #include "pairinteraction/utils/spherical.hpp"
       9             : #include "pairinteraction/utils/traits.hpp"
      10             : 
      11             : #include <Eigen/Dense>
      12             : #include <Eigen/Sparse>
      13             : #include <complex>
      14             : #include <map>
      15             : #include <spdlog/spdlog.h>
      16             : #include <unsupported/Eigen/Splines>
      17             : 
      18             : namespace pairinteraction {
      19             : template <typename Scalar>
      20          27 : GreenTensor<Scalar>::ConstantEntry::ConstantEntry(int row, int col, Scalar val)
      21          27 :     : row_(row), col_(col), val_(val) {}
      22             : 
      23             : template <typename Scalar>
      24          27 : Scalar GreenTensor<Scalar>::ConstantEntry::val() const {
      25          27 :     return val_;
      26             : }
      27             : 
      28             : template <typename Scalar>
      29          54 : int GreenTensor<Scalar>::ConstantEntry::row() const noexcept {
      30          54 :     return row_;
      31             : }
      32             : 
      33             : template <typename Scalar>
      34          54 : int GreenTensor<Scalar>::ConstantEntry::col() const noexcept {
      35          54 :     return col_;
      36             : }
      37             : 
      38             : template <typename Scalar>
      39           0 : GreenTensor<Scalar>::OmegaDependentEntry::OmegaDependentEntry(int row, int col,
      40             :                                                               Eigen::Spline<real_t, 1> real_spline,
      41             :                                                               Eigen::Spline<real_t, 1> imag_spline)
      42           0 :     : row_(row), col_(col), real_spline(std::move(real_spline)),
      43           0 :       imag_spline(std::move(imag_spline)) {}
      44             : 
      45             : template <typename Scalar>
      46           0 : Scalar GreenTensor<Scalar>::OmegaDependentEntry::val(double omega) const {
      47             :     if constexpr (traits::NumTraits<Scalar>::is_complex_v) {
      48           0 :         return {real_spline(omega)(0), imag_spline(omega)(0)};
      49             :     } else {
      50           0 :         return real_spline(omega)(0);
      51             :     }
      52             : }
      53             : 
      54             : template <typename Scalar>
      55           0 : int GreenTensor<Scalar>::OmegaDependentEntry::row() const noexcept {
      56           0 :     return row_;
      57             : }
      58             : 
      59             : template <typename Scalar>
      60           0 : int GreenTensor<Scalar>::OmegaDependentEntry::col() const noexcept {
      61           0 :     return col_;
      62             : }
      63             : 
      64             : template <typename Scalar>
      65           9 : void GreenTensor<Scalar>::set_entries(
      66             :     int kappa1, int kappa2, const Eigen::MatrixX<Scalar> &tensor_in_cartesian_coordinates) {
      67           9 :     const real_t numerical_precision =
      68           9 :         5 * std::numeric_limits<real_t>::epsilon() * tensor_in_cartesian_coordinates.norm();
      69             : 
      70          18 :     Eigen::SparseMatrix<complex_t> tensor =
      71           9 :         (spherical::get_transformator<complex_t>(kappa1) * tensor_in_cartesian_coordinates *
      72           9 :          spherical::get_transformator<complex_t>(kappa2).adjoint())
      73           9 :             .sparseView(1, numerical_precision);
      74             : 
      75           9 :     std::vector<Entry> entries;
      76          36 :     for (int k = 0; k < tensor.outerSize(); ++k) {
      77          54 :         for (typename Eigen::SparseMatrix<complex_t>::InnerIterator it(tensor, k); it; ++it) {
      78             :             if constexpr (traits::NumTraits<Scalar>::is_complex_v) {
      79           0 :                 entries.emplace_back(ConstantEntry(it.row(), it.col(), it.value()));
      80             :             } else {
      81          26 :                 entries.emplace_back(ConstantEntry(it.row(), it.col(), it.value().real()));
      82          27 :                 assert(abs(it.value().imag()) < numerical_precision);
      83             :             }
      84             :         }
      85             :     }
      86           9 :     entries_map[{kappa1, kappa2}] = std::move(entries);
      87           8 : }
      88             : 
      89             : template <typename Scalar>
      90           0 : void GreenTensor<Scalar>::set_entries(
      91             :     int kappa1, int kappa2,
      92             :     const std::vector<Eigen::MatrixX<Scalar>> &tensors_in_cartesian_coordinates,
      93             :     const std::vector<double> &omegas) {
      94             : 
      95           0 :     if (tensors_in_cartesian_coordinates.size() != omegas.size()) {
      96           0 :         throw std::invalid_argument("The number of tensors and omegas must match.");
      97             :     }
      98             : 
      99           0 :     auto num_knots = static_cast<int>(omegas.size());
     100           0 :     Eigen::Map<const Eigen::RowVectorXd> knots(omegas.data(), num_knots);
     101             : 
     102           0 :     constexpr int spline_degree = 3; // cubic spline interpolation
     103             : 
     104             :     // Temporary storage wih key = (row, col) and value = vector of one double per omega
     105           0 :     std::map<std::pair<int, int>, std::pair<Eigen::RowVectorXd, Eigen::RowVectorXd>> temp_map;
     106           0 :     for (int idx = 0; idx < num_knots; ++idx) {
     107           0 :         const real_t numerical_precision = 5 * std::numeric_limits<real_t>::epsilon() *
     108           0 :             tensors_in_cartesian_coordinates[idx].norm();
     109             : 
     110           0 :         Eigen::SparseMatrix<complex_t> tensor =
     111           0 :             (spherical::get_transformator<complex_t>(kappa1) *
     112           0 :              tensors_in_cartesian_coordinates[idx] *
     113           0 :              spherical::get_transformator<complex_t>(kappa2).adjoint())
     114           0 :                 .sparseView(1, numerical_precision);
     115             : 
     116           0 :         for (int k = 0; k < tensor.outerSize(); ++k) {
     117           0 :             for (typename Eigen::SparseMatrix<complex_t>::InnerIterator it(tensor, k); it; ++it) {
     118           0 :                 std::pair<int, int> key{it.row(), it.col()};
     119           0 :                 auto &[vec_real, vec_imag] =
     120             :                     temp_map
     121           0 :                         .try_emplace(key, Eigen::RowVectorXd::Zero(num_knots),
     122             :                                      Eigen::RowVectorXd::Zero(num_knots))
     123           0 :                         .first->second;
     124           0 :                 vec_real(idx) = it.value().real();
     125             :                 if constexpr (traits::NumTraits<Scalar>::is_complex_v) {
     126           0 :                     vec_imag(idx) = it.value().imag();
     127             :                 } else {
     128           0 :                     assert(abs(it.value().imag()) < numerical_precision);
     129             :                 }
     130             :             }
     131             :         }
     132             :     }
     133             : 
     134             :     // Set the green tensor entries with spline interpolation
     135           0 :     std::vector<Entry> entries;
     136           0 :     entries.reserve(temp_map.size());
     137           0 :     for (const auto &[key, value] : temp_map) {
     138           0 :         const auto &[vec_real, vec_imag] = value;
     139           0 :         const auto &[row, col] = key;
     140             : 
     141           0 :         Eigen::Spline<real_t, 1> real_spline =
     142             :             Eigen::SplineFitting<Eigen::Spline<real_t, 1>>::Interpolate(vec_real, spline_degree,
     143             :                                                                         knots);
     144             : 
     145           0 :         Eigen::Spline<real_t, 1> imag_spline;
     146             :         if constexpr (traits::NumTraits<Scalar>::is_complex_v) {
     147           0 :             imag_spline = Eigen::SplineFitting<Eigen::Spline<real_t, 1>>::Interpolate(
     148             :                 vec_imag, spline_degree, knots);
     149             :         }
     150             : 
     151           0 :         entries.emplace_back(
     152           0 :             OmegaDependentEntry(row, col, std::move(real_spline), std::move(imag_spline)));
     153             :     }
     154           0 :     entries_map[{kappa1, kappa2}] = std::move(entries);
     155           0 : }
     156             : 
     157             : template <typename Scalar>
     158             : const std::vector<typename GreenTensor<Scalar>::Entry> &
     159          90 : GreenTensor<Scalar>::get_entries(int kappa1, int kappa2) const {
     160          90 :     if (auto it = entries_map.find({kappa1, kappa2}); it != entries_map.end()) {
     161          26 :         return it->second;
     162             :     }
     163          63 :     static const std::vector<Entry> empty_entries;
     164          63 :     return empty_entries;
     165             : }
     166             : 
     167             : // Explicit instantiations
     168             : template class GreenTensor<double>;
     169             : template class GreenTensor<std::complex<double>>;
     170             : } // namespace pairinteraction

Generated by: LCOV version 1.16