Program Listing for File DataPlotter.cc

Program Listing for File DataPlotter.cc#

Return to documentation for file (doxygen_docs/GeneralKModels/DataPlotter.cc)

#include "Karana/GeneralKModels/DataPlotter.h"
#include "Karana/KCore/Allocator.h"

/**
 * @file
 * @brief DataPlotter implementation.
 */

namespace Karana::Models {

    namespace ku = Karana::KUtils;
    namespace km = Karana::Math;

    DataPlotter::DataPlotter(std::string_view name,
                             const kc::ks_ptr<kd::ModelManager> &mm,
                             const kc::ks_ptr<ku::PlotData> &plot_data)
        : KModel<DataPlotter, DataPlotterParams>(name, mm)
        , _plot_data(plot_data) {
        params = std::allocate_shared<DataPlotterParams>(kc::Allocator<DataPlotterParams>{},
                                                         std::format("{}_params", name));
    };

    kc::ks_ptr<DataPlotter> DataPlotter::create(std::string_view name,
                                                const kc::ks_ptr<kd::ModelManager> &mm,
                                                const kc::ks_ptr<ku::PlotData> &plot_data) {
        kc::ks_ptr<DataPlotter> dl =
            std::allocate_shared<DataPlotter>(kc::Allocator<DataPlotter>{}, name, mm, plot_data);
        mm->registerModel(dl);
        return dl;
    }

    void DataPlotter::postModelStep(const km::Ktime &, const km::Vec &) { _plot_data->update(); }

    DataPlotterParams::DataPlotterParams(std::string_view name)
        : KModelParams(name) {}

    bool DataPlotterParams::isReady() const { return true; }

    kc::ks_ptr<ku::PlotData> DataPlotter::getPlotData() { return _plot_data; }

    void DataPlotter::_registerModel() {
        // Call the parent. This will do the normal registration.
        KModel<DataPlotter, DataPlotterParams>::_registerModel();

        kc::ks_ptr<Base> dark = shared_from_this();
        auto t = kc::static_pointer_cast<DataPlotter>(dark);

        // Also register a timed event now if we should log the first step.
        kc::ks_ptr<kd::StatePropagator> sp =
            kc::dynamic_pointer_cast<kd::StatePropagator>(model_manager);
        if (sp and params->log_first_step) {
            auto te = kd::TimedEvent::create(
                std::format("{}_first_step", name()),
                model_manager->getTime(),
                [t](const km::Ktime &) { t->_plot_data->update(); },
                true);
            sp->registerTimedEvent(te);
        }
    }

    void DataPlotter::_unregisterModel() {
        // Call the parent. This will do the normal unregistration.
        KModel<DataPlotter, DataPlotterParams>::_unregisterModel();

        // Try and unregister this event regardless of the parameter setting.
        // It's possible the parameter has been changed since the last registration.
        kc::ks_ptr<kd::StatePropagator> sp =
            kc::dynamic_pointer_cast<kd::StatePropagator>(model_manager);
        if (sp) {
            sp->unregisterTimedEvent(std::format("{}_first_step", name()), true, true);
        }
    }

    // Destructor included for MacOS builds. Must have a key-function out-of-line to avoid dulpicate
    // symbols.
    DataPlotter::~DataPlotter(){};

} // namespace Karana::Models