/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | Multiphase Code Repository by HZDR
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
  ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
License
    This file is part of the Multiphase Code Repository by HZDR.

    Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
    Website: https://hzdr.de

    Multiphase Code Repository by HZDR is based on the free software for
    computational fluid dynamics (CFD) from the OpenFOAM Foundation.
    Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org

    If you are interested in which files are original OpenFOAM Foundation
    files, which OpenFOAM Foundation files were modified, and which files were
    newly created, see FILES.md.

    Multiphase Code Repository by HZDR 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.

    Multiphase Code Repository by HZDR 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 the Multiphase Code Repository by HZDR. If not, see
    <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "cipsaMultiphaseEuler.H"
#include "fvcFlux.H"
#include "fvcReconstruct.H"

// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

void Foam::solvers::cipsaMultiphaseEuler::updateDragDiag()
{
    fillFields("dragDiag", dimensionSet(1, -3, -1, 0, 0), dragDiag);
    fillFields("dragDiagf", dimensionSet(1, -3, -1, 0, 0), dragDiagf);
    fillFields("dragPCoefff", dimensionSet(0, 0, 0, 0, 0), dragPCoefff);

    forAllConstIter
    (
        momentumTransferSystem::KdTable,
        momentumTransferSystem_.KdsTable(),
        KdIter
    )
    {
        const volScalarField& K(*KdIter());
        const phaseInterface interface(fluid, KdIter.key());

        forAllConstIter(phaseInterface, interface, interfaceIter)
        {
            const phaseModel& phase = interfaceIter();
            const phaseModel& otherPhase = interfaceIter.otherPhase();

            if (phase.stationary()) continue;

            if (partialElimination)
            {
                dragDiag[phase.index()] +=
                    K
                   *(
                        1
                      + rADs[otherPhase.index()]
                       *(
                            HUEqns[phase.index()].A()
                          + ddtUEqns[phase.index()].A()
                          + fvModelsUEqns[phase.index()].A()
                        )
                    );
            }
            else
            {
                dragDiag[phase.index()] += K;
            }
        }
    }

    forAllConstIter
    (
        momentumTransferSystem::KdfTable,
        momentumTransferSystem_.KdfsTable(),
        KdfIter
    )
    {
        const surfaceScalarField& Kf(*KdfIter());
        const phaseInterface interface(fluid, KdfIter.key());

        forAllConstIter(phaseInterface, interface, interfaceIter)
        {
            const phaseModel& phase = interfaceIter();
            const phaseModel& otherPhase = interfaceIter.otherPhase();

            if (phase.stationary()) continue;

            if (partialElimination)
            {
                dragDiagf[phase.index()] +=
                    Kf
                   *(
                        1
                      + rADfs[otherPhase.index()]
                       *(
                            fvc::interpolate(HUEqns[phase.index()].A())
                          + fvc::interpolate(ddtUEqns[phase.index()].A())
                          + fvc::interpolate(fvModelsUEqns[phase.index()].A())
                        )
                    );

                dragPCoefff[phase.index()] += Kf*rADfs[otherPhase.index()];
            }
            else
            {
                dragDiagf[phase.index()] += Kf;
            }
        }
    }
}


Foam::PtrList<Foam::volVectorField>
Foam::solvers::cipsaMultiphaseEuler::calcDragSource()
{
    PtrList<volVectorField> dragSource(phases.size());
    fillFields("dragSource", dimensionSet(1, -2, -2, 0, 0), dragSource);

    {
        // Summed RHS of cell-based momentum balance equation
        volVectorField HSum
        (
            IOobject
            (
                "HSum",
                runTime.name(),
                mesh
            ),
            mesh,
            dimensionedVector(dimensionSet(1, -2, -2, 0, 0), Zero)
        );

        if (partialElimination)
        {
            forAll(movingPhases, movingPhasei)
            {
                const phaseModel& phase = movingPhases[movingPhasei];

                HSum +=
                    HUEqns[phase.index()].H()
                  + ddtUEqns[phase.index()].H()
                  + fvModelsUEqns[phase.index()].H()
                  - fvc::reconstruct
                    (
                        phase.turbulentNormalStress()*mesh.magSf()
                    );
            }
        }

        forAllConstIter
        (
            momentumTransferSystem::KdTable,
            momentumTransferSystem_.KdsTable(),
            KdIter
        )
        {
            const volScalarField& K(*KdIter());
            const phaseInterface interface(fluid, KdIter.key());

            forAllConstIter(phaseInterface, interface, interfaceIter)
            {
                const phaseModel& phase = interfaceIter();
                const phaseModel& otherPhase = interfaceIter.otherPhase();

                if (phase.stationary() || otherPhase.stationary()) continue;

                if (partialElimination)
                {
                    dragSource[phase.index()] +=
                        K*rADs[otherPhase.index()]*HSum;

                    forAll(movingPhases, movingPhasei)
                    {
                        const phaseModel& remainingPhase =
                            movingPhases[movingPhasei];

                        if
                        (
                            (remainingPhase.index() == phase.index())
                         || (remainingPhase.index() == otherPhase.index())
                        ) continue;

                        dragSource[phase.index()] -=
                        (
                            K*rADs[otherPhase.index()]
                           *(
                                HUEqns[remainingPhase.index()].A()
                              + ddtUEqns[remainingPhase.index()].A()
                              + fvModelsUEqns[remainingPhase.index()].A()
                            )
                           *phases[remainingPhase.index()].U()
                        );
                    }
                }
                else
                {
                    dragSource[phase.index()] +=
                        K*phases[otherPhase.index()].U();
                }
            }
        }
    }

    return dragSource;
}


Foam::PtrList<Foam::surfaceScalarField>
Foam::solvers::cipsaMultiphaseEuler::calcDragSourcef()
{
    PtrList<surfaceScalarField> dragSourcef(phases.size());
    fillFields("dragSourcef", dimensionSet(0, 3, -1, 0, 0), dragSourcef);

    {
        // Summed RHS of face-based momentum balance equation
        surfaceScalarField HSumf
        (
            IOobject
            (
                "HSumf",
                runTime.name(),
                mesh
            ),
            mesh,
            dimensionedScalar(dimensionSet(1, 0, -2, 0, 0), Zero)
        );

        if (partialElimination)
        {
            forAll(movingPhases, movingPhasei)
            {
                const phaseModel& phase = movingPhases[movingPhasei];

                HSumf +=
                    fvc::flux(HUEqns[phase.index()].H())
                  + ddtPhiCoeffs[phase.index()]
                  + fvc::flux(fvModelsUEqns[phase.index()].H())
                  - phase.turbulentNormalStress()*mesh.magSf();
            }
        }

        forAllConstIter
        (
            momentumTransferSystem::KdfTable,
            momentumTransferSystem_.KdfsTable(),
            KdfIter
        )
        {
            const surfaceScalarField& Kf(*KdfIter());
            const phaseInterface interface(fluid, KdfIter.key());

            forAllConstIter(phaseInterface, interface, interfaceIter)
            {
                const phaseModel& phase = interfaceIter();
                const phaseModel& otherPhase = interfaceIter.otherPhase();

                if (phase.stationary() || otherPhase.stationary()) continue;

                if (partialElimination)
                {
                    dragSourcef[phase.index()] +=
                        Kf*rADfs[otherPhase.index()]*rHUfs[phase.index()]*HSumf;

                    forAll(movingPhases, movingPhasei)
                    {
                        const phaseModel& remainingPhase =
                            movingPhases[movingPhasei];

                        if
                        (
                            (remainingPhase.index() == phase.index())
                         || (remainingPhase.index() == otherPhase.index())
                        ) continue;

                        dragSourcef[phase.index()] -=
                        (
                            Kf*rADfs[otherPhase.index()]*rHUfs[phase.index()]
                           *(
                                fvc::interpolate
                                (
                                    HUEqns[remainingPhase.index()].A()
                                )
                              + fvc::interpolate
                                (
                                    ddtUEqns[remainingPhase.index()].A()
                                )
                              + fvc::interpolate
                                (
                                    fvModelsUEqns[remainingPhase.index()].A()
                                )
                            )
                           *MRF.absolute(phases[remainingPhase.index()].phi())
                        );
                    }
                }
                else
                {
                    dragSourcef[phase.index()] +=
                        Kf*rHUfs[phase.index()]
                       *MRF.absolute(phases[otherPhase.index()].phi());
                }
            }
        }
    }

    return dragSourcef;
}


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