/*****************************************************************************

    TRAVIS - Trajectory Analyzer and Visualizer

    http://www.travis-analyzer.de/

    Copyright (c) 2009-2026 Martin Brehm
                  2012-2024 Martin Thomas
                  2016-2024 Sascha Gehrke
                  2023-2024 Paul Zaby

    Please cite:  J. Chem. Phys. 2020, 152 (16), 164105.         (DOI 10.1063/5.0005078 )
                  J. Chem. Inf. Model. 2011, 51 (8), 2007-2023.  (DOI 10.1021/ci200217w )

    This file was written by Paul Zaby.

    ---------------------------------------------------------------------------

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*****************************************************************************/


#ifndef CONDUCT_H
#define CONDUCT_H


// This must always be the first include directive
#include "config.h"
#include "moltools.h"



// Required modern C++ features
#ifdef TRAVIS_MODERN_CPP



bool gatherConduct();
bool initializeConduct();
bool processConduct(CTimeStep* ts);
bool finalizeConduct();

class CConductObservation: public CObservation
{
public:
    explicit CConductObservation();
    ~CConductObservation();

    void initialize();
    void process(CTimeStep* ts);
    void finalize();

    bool Parse();
    std::pair<double, double> MinMaxEntry(const std::vector<std::vector<double> >& conduct_data, const std::vector<double>& total_conduct);
    std::pair<double, double> MinMaxEntry(const std::vector<double>& total_conduct);
    void DumpPlots(const std::vector<std::vector<double> >& conduct_data, const std::vector<double>& total_conduct, const std::vector<std::vector<double> >& var_mat = {}, const std::vector<double>& var = {});
    void DumpPlots(const std::vector<double>& total_conduct, const std::vector<double>& var = {});

    std::pair<int, int> FindLinearRegion(const std::vector<double>& y, double tol = 0.1, int nslice = 25, double incr = 0.01);
    std::tuple<double, double, double, double> ErrWeightLinReg(const std::vector<double>& y, const std::vector<double>& var, bool find_linear_region = true);
    std::tuple<double, double, double, double, double> LinReg(const std::vector<double>& y, int start, int end, bool weighted, const std::vector<double>& var = {});
    std::tuple<double, double, double, double, double> LinReg(const std::vector<double>& x, const std::vector<double>& y, bool weighted, const std::vector<double>& var = {});
    std::pair<std::vector<double>, std::vector<double>> CalcTransportNumbers(const std::vector<double>& term_values, const std::vector<double>& term_errors = {}, bool real_transference = false);
    std::pair<double, double> LinRegOld(const std::vector<double>& y);
    void WriteCSV(const char *name, const std::vector<std::vector<double> >& conduct_data, const std::vector<double>& total_conduct, const std::vector<std::vector<double> >& var_mat, const std::vector<double>& var);
    void WriteCSV(const char *name, const std::vector<double>& total_conduct, const std::vector<double>& var);

private:
    std::string m_sName;
    std::vector<double> m_faCharges;
    std::vector<int> m_iaActiveMolecules;
    std::vector<std::vector<int> > m_iaaAtomSet;
    std::vector<std::vector<CxDVec3Array*> > m_oaaCoords;
    std::vector<std::vector<CxDVec3Array*> > m_oaaVelocities;
    // integers represent the molecule components (/types) used for centering velocities and coordinates (default: 0)
    std::vector<int> m_iaCenterComponents = {0};

    double m_fTemperature;
    int m_iDepth;
    double m_fVol;
    int m_iStride;
    int m_iStride2;
    bool m_bGK;
    bool m_bDecompConduct;
    bool m_bMsdiff;
    bool m_bMsdiffLogFit;
    // bool m_bDecompSelfCross;
    bool m_bUncertainty;
    bool m_bIPS; // rule of initial positive sequences for variance estimation
    int m_iCentering;
    bool m_bCenterByMass = true;
    bool m_bCenterByGeometry = false;
    bool m_bCenterByComponent = false;
};



#endif


#endif



