/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "phaseForces.H"
#include "addToRunTimeSelectionTable.H"
#include "fvcGrad.H"
#include "dragModel.H"
#include "virtualMassModel.H"
#include "liftModel.H"
#include "wallLubricationModel.H"
#include "turbulentDispersionModel.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
#include "fvcReconstruct.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace functionObjects
{
    defineTypeNameAndDebug(phaseForces, 0);
    addToRunTimeSelectionTable(functionObject, phaseForces, dictionary);
}
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::functionObjects::phaseForces::phaseForces
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
    fvMeshFunctionObject(name, runTime, dict),
    phase_
    (
        mesh_.lookupObject<phaseModel>
        (
            IOobject::groupName("alpha", dict.lookup("phase"))
        )
    ),
    fluid_(mesh_.lookupObject<phaseSystem>(phaseSystem::propertiesName))
{
    read(dict);

    forAll(fluid_.phases(), phasei)
    {
        const phaseModel& otherPhase = fluid_.phases()[phasei];

        if (&otherPhase == &phase_) continue;

        const phaseInterface interface(phase_, otherPhase);

        if (fluid_.foundInterfacialModel<blendedDragModel>(interface))
        {
            forceFields_.insert
            (
                dragModel::typeName,
                new volVectorField
                (
                    IOobject
                    (
                        IOobject::groupName("dragForce", phase_.name()),
                        mesh_.time().name(),
                        mesh_
                    ),
                    mesh_,
                    dimensionedVector(dimForce/dimVolume, Zero)
                )
            );
        }

        if (fluid_.foundInterfacialModel<blendedVirtualMassModel>(interface))
        {
            forceFields_.insert
            (
                virtualMassModel::typeName,
                new volVectorField
                (
                    IOobject
                    (
                        IOobject::groupName
                        (
                            "virtualMassForce",
                            phase_.name()
                        ),
                        mesh_.time().name(),
                        mesh_
                    ),
                    mesh_,
                    dimensionedVector(dimForce/dimVolume, Zero)
                )
            );
        }

        if (fluid_.foundInterfacialModel<blendedLiftModel>(interface))
        {
            forceFields_.insert
            (
                liftModel::typeName,
                new volVectorField
                (
                    IOobject
                    (
                        IOobject::groupName("liftForce", phase_.name()),
                        mesh_.time().name(),
                        mesh_
                    ),
                    mesh_,
                    dimensionedVector(dimForce/dimVolume, Zero)
                )
            );
        }

        if
        (
            fluid_.foundInterfacialModel
            <blendedWallLubricationModel>(interface)
        )
        {
            forceFields_.insert
            (
                wallLubricationModel::typeName,
                new volVectorField
                (
                    IOobject
                    (
                        IOobject::groupName
                        (
                            "wallLubricationForce",
                            phase_.name()
                        ),
                        mesh_.time().name(),
                        mesh_
                    ),
                    mesh_,
                    dimensionedVector(dimForce/dimVolume, Zero)
                )
            );
        }

        if
        (
            fluid_.foundInterfacialModel
            <blendedTurbulentDispersionModel>(interface)
        )
        {
            forceFields_.insert
            (
                turbulentDispersionModel::typeName,
                new volVectorField
                (
                    IOobject
                    (
                        IOobject::groupName
                        (
                            "turbulentDispersionForce",
                            phase_.name()
                        ),
                        mesh_.time().name(),
                        mesh_
                    ),
                    mesh_,
                    dimensionedVector(dimForce/dimVolume, Zero)
                )
            );
        }
    }
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::functionObjects::phaseForces::~phaseForces()
{}


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

bool Foam::functionObjects::phaseForces::read(const dictionary& dict)
{
    fvMeshFunctionObject::read(dict);

    return true;
}


bool Foam::functionObjects::phaseForces::execute()
{
    // Zero the force fields
    forAllConstIter
    (
        HashPtrTable<volVectorField>,
        forceFields_,
        forceFieldIter
    )
    {
        *forceFieldIter() = Zero;
    }

    // Add the forces from all the interfaces which contain this phase
    forAll(fluid_.phases(), phasei)
    {
        const phaseModel& otherPhase = fluid_.phases()[phasei];

        if (&otherPhase == &phase_) continue;

        const phaseInterface interface(phase_, otherPhase);

        if (fluid_.foundInterfacialModel<blendedDragModel>(interface))
        {
            *forceFields_[dragModel::typeName] +=
                fluid_.lookupInterfacialModel<blendedDragModel>(interface).K()
               *(otherPhase.U() - phase_.U());
        }

        if (fluid_.foundInterfacialModel<blendedVirtualMassModel>(interface))
        {
            *forceFields_[virtualMassModel::typeName] +=
                fluid_.lookupInterfacialModel
                <blendedVirtualMassModel>(interface).K()
               *(
                    (otherPhase.DUDt() & otherPhase.U())
                  - (phase_.DUDt() & phase_.U())
                );
        }

        if (fluid_.foundInterfacialModel<blendedLiftModel>(interface))
        {
            *forceFields_[liftModel::typeName] +=
                (&interface.phase1() == &phase_ ? -1 : +1)
               *fluid_.lookupInterfacialModel<blendedLiftModel>(interface).F();
        }

        if
        (
            fluid_.foundInterfacialModel
            <blendedWallLubricationModel>(interface)
        )
        {
            *forceFields_[wallLubricationModel::typeName] +=
                (&interface.phase1() == &phase_ ? -1 : +1)
               *fluid_.lookupInterfacialModel
                <blendedWallLubricationModel>(interface).F();
        }

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Comment HZDR ~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
        if
        (
            fluid_.foundInterfacialModel
            <blendedTurbulentDispersionModel>(interface)
        )
        {
            *forceFields_[turbulentDispersionModel::typeName] +=
                fluid_.lookupInterfacialModel
                <blendedTurbulentDispersionModel>(interface).D()
               *fvc::grad
                (
                    otherPhase
                   /max(phase_ + otherPhase, otherPhase.residualAlpha())
                );
        }
\*~~~~~~~~~~~~~~~~~~~~~~~~ End Comment Code Repository ~~~~~~~~~~~~~~~~~~~~~~*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
        if
        (
            fluid_.foundInterfacialModel
            <blendedTurbulentDispersionModel>(interface)
        )
        {
            *forceFields_[turbulentDispersionModel::typeName] +=
                (&interface.phase1() == &phase_ ? -1 : +1)
               *fvc::reconstruct
                (
                    fluid_.lookupInterfacialModel
                    <blendedTurbulentDispersionModel>(interface).Ff()
                );
        }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
    }

    return true;
}


bool Foam::functionObjects::phaseForces::write()
{
    forAllConstIter
    (
        HashPtrTable<volVectorField>,
        forceFields_,
        forceFieldIter
    )
    {
        writeObject(forceFieldIter()->name());
    }

    return true;
}


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