From e750611f272983f0fc7b47afae9566a9d6975172 Mon Sep 17 00:00:00 2001
From: Nikos Karastathis <n.karastathis@kit.edu>
Date: Mon, 22 Feb 2021 10:24:55 +0100
Subject: [PATCH] Re-architecture of detectors & antenna changes

make antennas faster
---
 corsika/modules/radio/antennas/Antenna.hpp    |  4 +-
 .../radio/antennas/TimeDomainAntenna.hpp      | 49 ++++++++++---------
 .../modules/radio/detectors/RadioDetector.hpp |  9 ++--
 .../radio/detectors/TimeDomainDetector.hpp    | 36 +++++++-------
 4 files changed, 50 insertions(+), 48 deletions(-)

diff --git a/corsika/modules/radio/antennas/Antenna.hpp b/corsika/modules/radio/antennas/Antenna.hpp
index 133e482e1..9aaabc365 100644
--- a/corsika/modules/radio/antennas/Antenna.hpp
+++ b/corsika/modules/radio/antennas/Antenna.hpp
@@ -30,8 +30,8 @@ namespace corsika {
     // this stores the polarization vector of an electric field
     using ElectricFieldVector =
         QuantityVector<ElectricFieldType::dimension_type>;
-    using MagneticFieldVector =
-        QuantityVector<MagneticFieldType::dimension_type>;
+//    using MagneticFieldVector =
+//        QuantityVector<MagneticFieldType::dimension_type>;
 
     // a dimensionless vector used for the incident direction
     using Vector = QuantityVector<dimensionless_d>;
diff --git a/corsika/modules/radio/antennas/TimeDomainAntenna.hpp b/corsika/modules/radio/antennas/TimeDomainAntenna.hpp
index 211d63d84..1462b23e1 100644
--- a/corsika/modules/radio/antennas/TimeDomainAntenna.hpp
+++ b/corsika/modules/radio/antennas/TimeDomainAntenna.hpp
@@ -27,9 +27,9 @@ namespace corsika {
     TimeType const duration_;        ///< The duration of this waveform.
     InverseTimeType const sample_rate_; ///< The sampling rate of this antenna.
     int num_bins_;                   ///< The number of bins used.
-    xt::xtensor<double,2> waveformE_; ///< The waveform stored by this antenna.
-    xt::xtensor<double,1> waveformT_;///< This xtensor stores timebins.
-    std::pair<xt::xtensor<double, 1>, xt::xtensor<double,2>> waveform_; ///< E & t pair
+    xt::xtensor<double,3> waveformE_; ///< The waveform stored by this antenna.
+    std::pair<xt::xtensor<double, 1>,
+        xt::xtensor<double,3>> waveform_; ///< useful for .getWaveform()
 
   protected:
     // expose the CRTP interfaces constructor
@@ -49,6 +49,8 @@ namespace corsika {
      * @param start_time         The starting time of this waveform.
      * @param duration           The duration of this waveform.
      * @param sample_rate        The sample rate of this waveform.
+     * @param num_bins_          The number of timebins to store E-field.
+     * @param waveformE_         The xtensor initialized to zero for E-field.
      *
      */
     TimeDomainAntenna(std::string const& name, Point const& location,
@@ -61,17 +63,17 @@ namespace corsika {
         , sample_rate_(sample_rate)
         , num_bins_ (static_cast<int>(duration * sample_rate))
         , waveformE_ (xt::zeros<double>({3, num_bins_}))
-        , waveformT_(xt::zeros<double>({num_bins_}))
     {};
 
     /**
      * Receive an electric field at this antenna.
      *
      * This assumes that the antenna will receive
-     *  an *instantaneous* electric field modeled as a delta function.
+     *  an *instantaneous* electric field modeled as a delta function (or timebin).
      *
-     * @param time        The (global) time at which this
-     * @param field       The incident electric field vector.
+     * @param time             The (global) time at which this signal is received.
+     * @param receive_vector   The incident unit vector. (not used at the moment)
+     * @param field            The incident electric field vector.
      *
      */
     void receive(TimeType const time, Vector const& receive_vector,
@@ -80,23 +82,14 @@ namespace corsika {
       if (time < start_time_ || time > start_time_ + duration_) {
         return;
       } else {
-
+        // figure out the correct timebin to store the E-field value.
         auto timebin_ {static_cast<std::size_t>((time - start_time_) * sample_rate_)};
 
-        for (std::size_t t = static_cast<std::size_t>(start_time_ / 1_ns);
-             t < static_cast<std::size_t>((start_time_ + duration_) / 1_ns);
-             t = t + timebin_) {
-          waveformT_.at(t) = t; // store the sample times
-        }
-
-        // store the x,y,z electric field components
-        waveformE_.at(0, timebin_) = waveformE_.at(0, timebin_) + (efield.getX().magnitude());
-        waveformE_.at(1, timebin_) = waveformE_.at(1, timebin_) + (efield.getY().magnitude());
-        waveformE_.at(2, timebin_) = waveformE_.at(2, timebin_) + (efield.getZ().magnitude());
+        // store the x,y,z electric field components.
+        waveformE_.at(0, timebin_) += (efield.getX().magnitude());
+        waveformE_.at(1, timebin_) += (efield.getY().magnitude());
+        waveformE_.at(2, timebin_) += (efield.getZ().magnitude());
 
-        // create a std::pair of sample times & electric field components
-        waveform_.first = waveformT_;
-        waveform_.second = waveformE_;
       }
     }
 
@@ -107,7 +100,16 @@ namespace corsika {
      *
      * @returns A pair of the sample times, and the field
      */
-    std::pair<xt::xtensor<double, 2>, xt::xtensor<double,1>> getWaveform() const {
+    std::pair<xt::xtensor<double, 1>, xt::xtensor<double,3>> getWaveform() const {
+
+      // create a 1-D xtensor to store time values so we can print them later.
+      xt::xtensor<double, 1> times_ {xt::zeros<double>({num_bins_, 1})};
+
+      for (auto i = 0; i <= num_bins_; i++) {
+        times_.at(i) = static_cast<double>(start_time_ / 1_ns) +
+                       static_cast<double>(i * sample_rate_ * 1_ns);
+      }
+
       return waveform_;
     };
 
@@ -115,8 +117,7 @@ namespace corsika {
      * Reset the antenna before starting a new simulation.
      */
     void reset() {
-      waveform_.first = xt::zeros_like(waveform_.first);
-      waveform_.second = xt::zeros_like(waveform_.second);
+      waveformE_ = xt::zeros_like(waveformE_);
     };
 
   }; // END: class Antenna final
diff --git a/corsika/modules/radio/detectors/RadioDetector.hpp b/corsika/modules/radio/detectors/RadioDetector.hpp
index c6701ac22..1d27a8ddd 100644
--- a/corsika/modules/radio/detectors/RadioDetector.hpp
+++ b/corsika/modules/radio/detectors/RadioDetector.hpp
@@ -13,10 +13,11 @@ namespace corsika {
 
   /**
    * The base interface for radio detectors.
+   * At the moment it is a collection of antennas with the same implementation.
    */
 
-  template <typename TAntennaImpl, typename TDetectorImpl>
-  class RadioDetector {
+  template <typename TAntennaImpl>
+  class AntennaCollection {
 
     /**
      * The collection of antennas used in this simulation.
@@ -29,7 +30,7 @@ namespace corsika {
      *
      * @param antenna    The antenna to add
      */
-    auto addAntenna(TAntennaImpl const& antenna) -> void { antennas_.push_back(antenna); }
+    void addAntenna(TAntennaImpl const& antenna) { antennas_.push_back(antenna); }
 
     /**
      * Get a *non*-const reference to the collection of antennas.
@@ -41,7 +42,7 @@ namespace corsika {
     /**
      * Reset all the antenna waveforms.
      */
-    auto reset() -> void {
+    void reset() {
       std::for_each(antennas_.begin(), antennas_.end(), std::mem_fn(&TAntennaImpl::reset));
     };
 
diff --git a/corsika/modules/radio/detectors/TimeDomainDetector.hpp b/corsika/modules/radio/detectors/TimeDomainDetector.hpp
index 23974262f..96fc6a9e7 100644
--- a/corsika/modules/radio/detectors/TimeDomainDetector.hpp
+++ b/corsika/modules/radio/detectors/TimeDomainDetector.hpp
@@ -7,21 +7,21 @@
  * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
  * the license.
  */
-#pragma once
-
-#include <vector>
-
-#include "corsika/modules/radio/detectors/RadioDetector.hpp"
-
-namespace corsika {
-
-  /**
-   * A common interface for radio detectors.
-   */
-  template <typename TAntennaImpl>
-  class TimeDomainDetector final : public RadioDetector<TAntennaImpl,TimeDomainDetector<TAntennaImpl>> {
-
-  public:
-  }; // END: class RadioDetector
-
-} // namespace corsika
+//#pragma once
+//
+//#include <vector>
+//
+//#include "corsika/modules/radio/detectors/RadioDetector.hpp"
+//
+//namespace corsika {
+//
+//  /**
+//   * A common interface for radio detectors.
+//   */
+//  template <typename TAntennaImpl>
+//  class TimeDomainDetector final : public RadioDetector<TAntennaImpl,TimeDomainDetector<TAntennaImpl>> {
+//
+//  public:
+//  }; // END: class RadioDetector
+//
+//} // namespace corsika
-- 
GitLab