//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Flux/Flux.h
//! @brief     Defines and implements class Flux.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifdef SWIG
#error no need to expose this header to Swig
#endif // SWIG
#ifndef BORNAGAIN_SAMPLE_FLUX_IFLUX_H
#define BORNAGAIN_SAMPLE_FLUX_IFLUX_H

#include <complex>
#include <memory>
#include <vector>

//! Interface to access reflection/transmission coefficients.
//! Realized by ScalarFlux and MatrixFlux.

class Flux {
public:
    virtual ~Flux() = default;

    bool only_outgoing{false};
};

class Fluxes : public std::vector<std::unique_ptr<Flux>> {};

//! Mark consecutive layers from exit as only_outgoing if they have same kz and zero B-field.
//! Works for both scalar and polarized cases (#1333).
template <typename SliceContainer>
inline void markOnlyOutgoing(Fluxes& fluxes, const std::vector<std::complex<double>>& kz,
                             const SliceContainer& slices, bool top_exit)
{
    const size_t N = fluxes.size();
    const std::complex<double> exit_kz = kz[top_exit ? N - 1 : 0];

    if (top_exit) {
        for (size_t i = N; i-- > 0;) {
            if (kz[i] != exit_kz || slices[i]->bField().mag() != 0)
                break;
            fluxes[i]->only_outgoing = true;
        }
    } else {
        for (size_t i = 0; i < N; ++i) {
            if (kz[i] != exit_kz || slices[i]->bField().mag() != 0)
                break;
            fluxes[i]->only_outgoing = true;
        }
    }
}

#endif // BORNAGAIN_SAMPLE_FLUX_IFLUX_H
