/*
  Copyright 2017 SINTEF Digital, Mathematics and Cybernetics.
  Copyright 2017 Statoil ASA.
  Copyright 2016 - 2017 IRIS AS.

  This file is part of the Open Porous Media project (OPM).

  OPM 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.

  OPM 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 OPM.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <opm/simulators/wells/StandardWellAssemble.hpp>

#include <opm/material/densead/Evaluation.hpp>
#include <opm/material/fluidsystems/BlackOilFluidSystem.hpp>

#include <opm/models/blackoil/blackoilvariableandequationindices.hh>
#include <opm/models/blackoil/blackoilonephaseindices.hh>
#include <opm/models/blackoil/blackoiltwophaseindices.hh>

#include <opm/simulators/wells/StandardWellEquations.hpp>
#include <opm/simulators/wells/StandardWellPrimaryVariables.hpp>
#include <opm/simulators/wells/WellAssemble.hpp>
#include <opm/simulators/wells/WellBhpThpCalculator.hpp>
#include <opm/simulators/wells/GroupStateHelper.hpp>
#include <opm/simulators/wells/WellInterfaceFluidSystem.hpp>
#include <opm/simulators/wells/WellState.hpp>


namespace Opm {

//! \brief Class administering assembler access to equation system.
template<typename Scalar, typename IndexTraits, int numEq>
class StandardWellEquationAccess {
public:
    //! \brief Constructor initializes reference to the equation system.
    explicit StandardWellEquationAccess(StandardWellEquations<Scalar, IndexTraits, numEq>& eqns)
        : eqns_(eqns)
    {}

    using BVectorWell = typename StandardWellEquations<Scalar, IndexTraits, numEq>::BVectorWell;
    using DiagMatWell = typename StandardWellEquations<Scalar, IndexTraits, numEq>::DiagMatWell;
    using OffDiatMatWell = typename StandardWellEquations<Scalar, IndexTraits, numEq>::OffDiagMatWell;

    //! \brief Returns a reference to residual vector.
    BVectorWell& residual()
    {
        return eqns_.resWell_;
    }

    //! \brief Returns a reference to B matrix.
    OffDiatMatWell& B()
    {
        return eqns_.duneB_;
    }

    //! \brief Returns a reference to C matrix.
    OffDiatMatWell& C()
    {
        return eqns_.duneC_;
    }

    //! \brief Returns a reference to D matrix.
    DiagMatWell& D()
    {
        return eqns_.duneD_;
    }

private:
    StandardWellEquations<Scalar, IndexTraits, numEq>& eqns_; //!< Reference to equation system
};

template<class FluidSystem, class Indices>
void
StandardWellAssemble<FluidSystem,Indices>::
assembleControlEq(const GroupStateHelperType& groupStateHelper,
                  const Well::InjectionControls& inj_controls,
                  const Well::ProductionControls& prod_controls,
                  const PrimaryVariables& primary_variables,
                  const Scalar rho,
                  StandardWellEquationsType& eqns1,
                  const bool stopped_or_zero_target) const
{
    auto& deferred_logger = groupStateHelper.deferredLogger();
    const auto& well_state = groupStateHelper.wellState();
    const auto& summary_state = groupStateHelper.summaryState();

    static constexpr int Water = FluidSystem::waterPhaseIdx;
    static constexpr int Oil = FluidSystem::oilPhaseIdx;
    static constexpr int Gas = FluidSystem::gasPhaseIdx;
    EvalWell control_eq(primary_variables.numWellEq() + Indices::numEq, 0.0);

    const auto& well = well_.wellEcl();

    auto getRates = [&]() {
        std::vector<EvalWell> rates(3, EvalWell(primary_variables.numWellEq() + Indices::numEq, 0.0));
        if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
            rates[Water] = primary_variables.getQs(FluidSystem::canonicalToActiveCompIdx(FluidSystem::waterCompIdx));
        }
        if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
            rates[Oil] = primary_variables.getQs(FluidSystem::canonicalToActiveCompIdx(FluidSystem::oilCompIdx));
        }
        if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
            rates[Gas] = primary_variables.getQs(FluidSystem::canonicalToActiveCompIdx(FluidSystem::gasCompIdx));
        }
        if constexpr (Indices::enableSolvent) {
            rates[Gas] += primary_variables.getQs(Indices::contiSolventEqIdx);
        }
        return rates;
    };

    if (stopped_or_zero_target) {
        control_eq = primary_variables.eval(PrimaryVariables::WQTotal);
    } else if (well_.isInjector()) {
        // Find injection rate.
        const EvalWell injection_rate = primary_variables.eval(PrimaryVariables::WQTotal);
                                                 // Setup function for evaluation of BHP from THP (used only if needed).
        std::function<EvalWell()> bhp_from_thp = [&]() {
            const auto rates = getRates();
            return WellBhpThpCalculator(well_).calculateBhpFromThp(well_state,
                                                                   rates,
                                                                   well,
                                                                   summary_state,
                                                                   rho,
                                                                   deferred_logger);
        };

        WellAssemble(well_).
            assembleControlEqInj(groupStateHelper,
                                 inj_controls,
                                 primary_variables.eval(PrimaryVariables::Bhp),
                                 injection_rate,
                                 bhp_from_thp,
                                 control_eq);
    } else {
             // Find rates.
        const auto rates = getRates();
                                            // Setup function for evaluation of BHP from THP (used only if needed).
        std::function<EvalWell()> bhp_from_thp = [&]() {
            return WellBhpThpCalculator(well_).calculateBhpFromThp(well_state,
                                                                   rates,
                                                                   well,
                                                                   summary_state,
                                                                   rho,
                                                                   deferred_logger);
        };
        WellAssemble(well_).
            assembleControlEqProd(groupStateHelper,
                                  prod_controls,
                                  primary_variables.eval(PrimaryVariables::Bhp),
                                  rates,
                                  bhp_from_thp,
                                  control_eq);
    }

    // using control_eq to update the matrix and residuals
    // TODO: we should use a different index system for the well equations
    StandardWellEquationAccess eqns(eqns1);
    eqns.residual()[0][PrimaryVariables::Bhp] = control_eq.value();
    for (int pv_idx = 0; pv_idx < primary_variables.numWellEq(); ++pv_idx) {
        eqns.D()[0][0][PrimaryVariables::Bhp][pv_idx] = control_eq.derivative(pv_idx + Indices::numEq);
    }
}

template<class FluidSystem, class Indices>
void StandardWellAssemble<FluidSystem,Indices>::
assembleInjectivityEq(const EvalWell& eq_pskin,
                      const EvalWell& eq_wat_vel,
                      const int pskin_index,
                      const int wat_vel_index,
                      const int cell_idx,
                      const int numWellEq,
                      StandardWellEquationsType& eqns1) const
{
    StandardWellEquationAccess eqns(eqns1);
    eqns.residual()[0][pskin_index] = eq_pskin.value();
    eqns.residual()[0][wat_vel_index] = eq_wat_vel.value();
    for (int pvIdx = 0; pvIdx < numWellEq; ++pvIdx) {
        eqns.D()[0][0][wat_vel_index][pvIdx] = eq_wat_vel.derivative(pvIdx+Indices::numEq);
        eqns.D()[0][0][pskin_index][pvIdx] = eq_pskin.derivative(pvIdx+Indices::numEq);
    }

         // the water velocity is impacted by the reservoir primary varaibles. It needs to enter matrix B
    for (int pvIdx = 0; pvIdx < Indices::numEq; ++pvIdx) {
        eqns.B()[0][cell_idx][wat_vel_index][pvIdx] = eq_wat_vel.derivative(pvIdx);
    }
}

template<class FluidSystem, class Indices>
void StandardWellAssemble<FluidSystem,Indices>::
assemblePerforationEq(const EvalWell& cq_s_effective,
                      const int componentIdx,
                      const int cell_idx,
                      const int numWellEq,
                      StandardWellEquationsType& eqns1) const
{
    StandardWellEquationAccess eqns(eqns1);

    // subtract sum of phase fluxes in the well equations.
    eqns.residual()[0][componentIdx] += cq_s_effective.value();

       // assemble the jacobians
    for (int pvIdx = 0; pvIdx < numWellEq; ++pvIdx) {
        // also need to consider the efficiency factor when manipulating the jacobians.
        eqns.C()[0][cell_idx][pvIdx][componentIdx] -= cq_s_effective.derivative(pvIdx+Indices::numEq); // intput in transformed matrix
        eqns.D()[0][0][componentIdx][pvIdx] += cq_s_effective.derivative(pvIdx+Indices::numEq);
    }

    for (int pvIdx = 0; pvIdx < Indices::numEq; ++pvIdx) {
        eqns.B()[0][cell_idx][componentIdx][pvIdx] += cq_s_effective.derivative(pvIdx);
    }
}

template<class FluidSystem, class Indices>
void StandardWellAssemble<FluidSystem,Indices>::
assembleSourceEq(const EvalWell& resWell_loc,
                 const int componentIdx,
                 const int numWellEq,
                 StandardWellEquationsType& eqns1) const
{
    StandardWellEquationAccess eqns(eqns1);
    for (int pvIdx = 0; pvIdx < numWellEq; ++pvIdx) {
        eqns.D()[0][0][componentIdx][pvIdx] += resWell_loc.derivative(pvIdx+Indices::numEq);
    }
    eqns.residual()[0][componentIdx] += resWell_loc.value();
}

template<class FluidSystem, class Indices>
void StandardWellAssemble<FluidSystem,Indices>::
assembleZFracEq(const EvalWell& cq_s_zfrac_effective,
                const int cell_idx,
                const int numWellEq,
                StandardWellEquationsType& eqns1) const
{
    StandardWellEquationAccess eqns(eqns1);
    for (int pvIdx = 0; pvIdx < numWellEq; ++pvIdx) {
        eqns.C()[0][cell_idx][pvIdx][Indices::contiZfracEqIdx] -= cq_s_zfrac_effective.derivative(pvIdx+Indices::numEq);
    }
}

#include <opm/simulators/utils/InstantiationIndicesMacros.hpp>

INSTANTIATE_TYPE_INDICES(StandardWellAssemble, double)

#if FLOW_INSTANTIATE_FLOAT
INSTANTIATE_TYPE_INDICES(StandardWellAssemble, float)
#endif

}
