/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "fvcSnGrad.H"
#include "fvmSup.H"
#include "fvcReconstruct.H"

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

void Foam::solvers::cipsaMultiphaseEuler::momentumPredictor()
{
    volScalarField& p_rgh = p_rgh_;

    // Update mixture properties
    U_ = fluid.U();
    rho_ = fluid.rho();

    // Store velocity and flux of previous iteration
    forAll(movingPhases, movingPhasei)
    {
        movingPhases[movingPhasei].U()().storePrevIter();
        movingPhases[movingPhasei].phi()().storePrevIter();
    }

    // Update drag and virtual mass coefficient table and interfacial
    // forces tables
    momentumTransferSystem_.fillMomentumTransferTables();

    alphafs.setSize(phases.size());
    forAll(phases, phasei)
    {
        const phaseModel& phase = phases[phasei];
        const volScalarField& alpha = phase;

        alphafs.set(phasei, fvc::interpolate(alpha));
    }

    HUEqns.setSize(phases.size());
    relaxU.setSize(phases.size());
    ddtUEqns.setSize(phases.size());
    ddtPhiCoeffs.setSize(phases.size());
    dmdtUEqns.setSize(phases.size());
    fvModelsUEqns.setSize(phases.size());
    updateHUEqn();
    updateDdtUEqn();
    updateDmdtUEqn();
    updateFvModelsUEqn();

    // rHUs, rHUfs, rADs, rADfs
    rHUs.setSize(phases.size());
    rHUfs.setSize(phases.size());
    rADs.setSize(phases.size());
    rADfs.setSize(phases.size());
    forAll(movingPhases, movingPhasei)
    {
        const phaseModel& phase = movingPhases[movingPhasei];

        rHUs.set(phase.index(), 1.0/HUEqns[phase.index()].A());
        rHUfs.set(phase.index(), fvc::interpolate(rHUs[phase.index()]));

        rADs.set
        (
            phase.index(),
            1.0
           /(
                HUEqns[phase.index()].A()
              + ddtUEqns[phase.index()].A()
              + fvModelsUEqns[phase.index()].A()
            )
        );

        rADfs.set
        (
            phase.index(),
            1.0
           /(
                fvc::interpolate(HUEqns[phase.index()].A())
              + fvc::interpolate(ddtUEqns[phase.index()].A())
              + fvc::interpolate(fvModelsUEqns[phase.index()].A())
            )
        );
    }
    fillFields("rHUs", dimless, rHUs);
    fillFields("rHUfs", dimless, rHUfs);
    fillFields("rADs", dimTime/dimDensity, rADs);
    fillFields("rADfs", dimTime/dimDensity, rADfs);

    dragDiag.setSize(phases.size());
    dragDiagf.setSize(phases.size());
    dragPCoefff.setSize(phases.size());
    vmDiag.setSize(phases.size());
    vmDiagf.setSize(phases.size());
    updateDragDiag();
    updateVmDiag();

    // Momentum predictor
    if (predictMomentum)
    {
        // Correct p_rgh for consistency with p
        p_rgh = p - rho*buoyancy.gh - buoyancy.pRef;

        PtrList<volVectorField> dragSource(calcDragSource());
        PtrList<volVectorField> vmSource(calcVmSource());
        PtrList<surfaceScalarField> Ffs(momentumTransferSystem_.Ffs());

        PtrList<volScalarField> rhoAlphaBuoyancy(calcRhoAlphaBuoyancy());
        PtrList<volVectorField> buoyancySource
        (
            calcBuoyancySource(rhoAlphaBuoyancy)
        );
        rhoAlphaBuoyancy.clear();

        // Mixture density gradient times gravitational acceleration
        const surfaceScalarField ghSnGradRho
        (
            "ghSnGradRho",
            buoyancy.ghf*fvc::snGrad(rho)*mesh.magSf()
        );

        // rAUfs, alpharAUfs, phigs WITHOUT field relaxation of UAlpha
        rAUfs.setSize(phases.size());
        PtrList<surfaceScalarField> alpharAUfs(phases.size());
        PtrList<surfaceScalarField> phigs(phases.size());
        forAll(movingPhases, movingPhasei)
        {
            phaseModel& phase = movingPhases_[movingPhasei];
            volVectorField& UAlpha = phase.URef();

            rAUfs.set
            (
                phase.index(),
                1.0
               /(
                    1.0
                  + rHUfs[phase.index()]
                   *(
                        fvc::interpolate(ddtUEqns[phase.index()].A())
                      + fvc::interpolate(dmdtUEqns[phase.index()].A())
                      + fvc::interpolate(fvModelsUEqns[phase.index()].A())
                      + dragDiagf[phase.index()]
                      + vmDiagf[phase.index()]
                    )
                )
            );

            alpharAUfs.set
            (
                phase.index(),
                rHUfs[phase.index()]*rAUfs[phase.index()]
               *(alphafs[phase.index()] + dragPCoefff[phase.index()])
            );

            // Combined buoyancy and force fluxes
            phigs.set
            (
                phase.index(),
                alpharAUfs[phase.index()]
               *(
                  - ghSnGradRho
                  + fluid.resolvedInterface().surfaceTension(phase)
                   *mesh.magSf()
                )
              - rHUfs[phase.index()]*rAUfs[phase.index()]*mesh.magSf()
               *phase.turbulentNormalStress()
            );

            // Momentum predictor equation
            fvVectorMatrix UEqns
            (
                HUEqns[phase.index()]
              + ddtUEqns[phase.index()]
              + dmdtUEqns[phase.index()]
              + fvModelsUEqns[phase.index()]
              + fvm::Sp(dragDiag[phase.index()], UAlpha)
              + fvm::Sp(vmDiag[phase.index()], UAlpha)
                ==
                dragSource[phase.index()]
              + vmSource[phase.index()]
              + buoyancySource[phase.index()]
              + (
                    HUEqns[phase.index()].A()
                  + ddtUEqns[phase.index()].A()
                  + dmdtUEqns[phase.index()].A()
                  + fvModelsUEqns[phase.index()].A()
                  + dragDiag[phase.index()]
                  + vmDiag[phase.index()]//?????????????????????????????????
                )
               *fvc::reconstruct
                (
                    phigs[phase.index()]
                  - alpharAUfs[phase.index()]*fvc::snGrad(p_rgh)
                   *mesh.magSf()
                  - rHUfs[phase.index()]*rAUfs[phase.index()]*Ffs[phase.index()]
                )
            );

            UEqns.relax();
            fvConstraints().constrain(UEqns);
            UEqns.solve();
            fvConstraints().constrain(UAlpha);
        }

        rAUfs.clear();
    }
}


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