LCOV - code coverage report
Current view: top level - models - portexcitations.cpp (source / functions) Coverage Total Hit
Test: Palace Coverage Report Lines: 20.4 % 49 10
Test Date: 2025-10-23 22:45:05 Functions: 12.5 % 8 1
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       2              : // SPDX-License-Identifier: Apache-2.0
       3              : 
       4              : #include "portexcitations.hpp"
       5              : 
       6              : #include "lumpedportoperator.hpp"
       7              : #include "surfacecurrentoperator.hpp"
       8              : #include "waveportoperator.hpp"
       9              : 
      10              : #include <fmt/format.h>
      11              : #include <fmt/ranges.h>
      12              : #include <nlohmann/json.hpp>
      13              : 
      14              : namespace palace
      15              : {
      16              : 
      17            0 : [[nodiscard]] std::string PortExcitations::FmtLog() const
      18              : {
      19              :   fmt::memory_buffer buf{};
      20            0 :   auto to = [&buf](auto f, auto &&...a)  // mini-lambda for cleaner code
      21            0 :   { fmt::format_to(std::back_inserter(buf), f, std::forward<decltype(a)>(a)...); };
      22              : 
      23              :   int i = 1;
      24            0 :   for (const auto &[idx, ex] : excitations)
      25              :   {
      26            0 :     to("Excitation{} with index {:d} has contributions from:\n",
      27            0 :        (Size() > 1) ? fmt::format(" {:d}/{:d}", i, Size()) : "", idx);
      28            0 :     if (!ex.lumped_port.empty())
      29              :     {
      30            0 :       to(" Lumped port{} {:2d}\n", (ex.lumped_port.size() > 1) ? "s" : "",
      31            0 :          fmt::join(ex.lumped_port, " "));
      32              :     }
      33            0 :     if (!ex.wave_port.empty())
      34              :     {
      35            0 :       to(" Wave port{} {:2d}\n", (ex.wave_port.size() > 1) ? "s" : "",
      36            0 :          fmt::join(ex.wave_port, " "));
      37              :     }
      38            0 :     if (!ex.current_port.empty())
      39              :     {
      40            0 :       to(" Surface current port{} {:2d}\n", (ex.current_port.size() > 1) ? "s" : "",
      41            0 :          fmt::join(ex.current_port, " "));
      42              :     }
      43            0 :     i++;
      44              :   }
      45            0 :   return fmt::to_string(buf);
      46              : }
      47              : 
      48            0 : void to_json(nlohmann::json &j, const PortExcitations::SingleExcitationSpec &p)
      49              : {
      50            0 :   j = nlohmann::json{{"LumpedPort", p.lumped_port},
      51            0 :                      {"WavePort", p.wave_port},
      52            0 :                      {"SurfaceCurrent", p.current_port}};
      53            0 : }
      54              : 
      55            0 : void from_json(const nlohmann::json &j, PortExcitations::SingleExcitationSpec &p)
      56              : {
      57            0 :   j.at("LumpedPort").get_to(p.lumped_port);
      58            0 :   j.at("WavePort").get_to(p.wave_port);
      59            0 :   j.at("SurfaceCurrent").get_to(p.current_port);
      60            0 : }
      61              : 
      62            0 : void to_json(nlohmann::json &j, const PortExcitations &p)
      63              : {
      64            0 :   j = nlohmann::json{p.excitations};
      65            0 : }
      66              : 
      67            0 : void from_json(const nlohmann::json &j, PortExcitations &p)
      68              : {
      69            0 :   j.get_to(p.excitations);
      70            0 : }
      71              : 
      72           17 : PortExcitations::PortExcitations(const LumpedPortOperator &lumped_port_op,
      73              :                                  const WavePortOperator &wave_port_op,
      74              :                                  const SurfaceCurrentOperator &surf_j_op)
      75              : {
      76           47 :   for (const auto &[idx, port] : lumped_port_op)
      77              :   {
      78           30 :     if (!port.HasExcitation())
      79              :     {
      80           14 :       continue;
      81              :     }
      82           16 :     excitations.try_emplace(port.excitation, SingleExcitationSpec{});  // If not present
      83           16 :     excitations.at(port.excitation).lumped_port.push_back(idx);
      84              :   }
      85           17 :   for (const auto &[idx, port] : wave_port_op)
      86              :   {
      87            0 :     if (!port.HasExcitation())
      88              :     {
      89            0 :       continue;
      90              :     }
      91            0 :     excitations.try_emplace(port.excitation, SingleExcitationSpec{});
      92            0 :     excitations.at(port.excitation).wave_port.push_back(idx);
      93              :   }
      94              : 
      95              :   // Surface currents are always excited. Add them to all single existing excitations.
      96              :   // TODO: Add excitation 1 if not present already?
      97              :   std::vector<int> current_port_idx;
      98           17 :   for (const auto &[idx, port] : surf_j_op)
      99              :   {
     100              :     current_port_idx.push_back(idx);
     101              :   }
     102           17 :   if (!current_port_idx.empty())
     103              :   {
     104            0 :     for (auto &[ex_idx, ex_spec] : excitations)
     105              :     {
     106            0 :       ex_spec.current_port = current_port_idx;
     107              :     }
     108              :   }
     109           17 : };
     110              : 
     111              : }  // namespace palace
        

Generated by: LCOV version 2.0-1