/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "phaseModel.H"
#include "phaseSystem.H"
#include "diameterModel.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
#include "fvcSmooth.H"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//

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

namespace Foam
{
    defineTypeNameAndDebug(phaseModel, 0);
    defineRunTimeSelectionTable(phaseModel, phaseSystem);
}


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

Foam::phaseModel::phaseModel
(
    const phaseSystem& fluid,
    const word& phaseName,
    const bool referencePhase,
    const label index
)
:
    // volScalarField
    // (
    //     referencePhase
    //   ? volScalarField
    //     (
    //         IOobject
    //         (
    //             IOobject::groupName("alpha", phaseName),
    //             fluid.mesh().time().name(),
    //             fluid.mesh(),
    //             IOobject::NO_READ,
    //             IOobject::AUTO_WRITE
    //         ),
    //         fluid.mesh(),
    //         dimensionedScalar(dimless, 0)
    //     )
    //   : volScalarField
    //     (
    //         IOobject
    //         (
    //             IOobject::groupName("alpha", phaseName),
    //             fluid.mesh().time().name(),
    //             fluid.mesh(),
    //             IOobject::MUST_READ,
    //             IOobject::AUTO_WRITE
    //         ),
    //         fluid.mesh()
    //     )
    // ),
    // Replaced by the following because the Clang compiler does not use
    // std::move to transfer the result of the ternary operator
    volScalarField
    (
        IOobject
        (
            IOobject::groupName("alpha", phaseName),
            fluid.mesh().time().name(),
            fluid.mesh(),
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        referencePhase
      ? volScalarField::New
        (
            IOobject::groupName("alpha", phaseName),
            fluid.mesh(),
            dimensionedScalar(dimless, 0)
        )
      : tmp<volScalarField>
        (
            new volScalarField
            (
                IOobject
                (
                    IOobject::groupName("alpha", phaseName),
                    fluid.mesh().time().name(),
                    fluid.mesh(),
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE,
                    false
                ),
                fluid.mesh()
            )
        )
    ),

    fluid_(fluid),
    name_(phaseName),
    index_(index),
    residualAlpha_
    (
        "residualAlpha",
        dimless,
        fluid.subDict(phaseName).lookup("residualAlpha")
    ),
    alphaMax_(fluid.subDict(phaseName).lookupOrDefault("alphaMax", 1.0)),
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
    continuousThreshold_
    (
        "continuousThreshold",
        dimless,
        fluid.subDict(phaseName).lookupOrDefault
        (
            "continuousThreshold",
            0.74
        )
    ),
    nInterfaceLayers_
    (
        fluid.subDict(phaseName).lookupOrDefault("nInterfaceLayers", 3)
    )
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//
{
    diameterModel_ = diameterModel::New(fluid.subDict(phaseName), *this);
}


Foam::autoPtr<Foam::phaseModel> Foam::phaseModel::clone() const
{
    NotImplemented;
    return autoPtr<phaseModel>(nullptr);
}


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

Foam::phaseModel::~phaseModel()
{}


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

const Foam::word& Foam::phaseModel::name() const
{
    return name_;
}


const Foam::word& Foam::phaseModel::keyword() const
{
    return name_;
}


Foam::label Foam::phaseModel::index() const
{
    return index_;
}


const Foam::phaseSystem& Foam::phaseModel::fluid() const
{
    return fluid_;
}


const Foam::dimensionedScalar& Foam::phaseModel::residualAlpha() const
{
    return residualAlpha_;
}


Foam::scalar Foam::phaseModel::alphaMax() const
{
    return alphaMax_;
}


Foam::tmp<Foam::volScalarField> Foam::phaseModel::d() const
{
    return diameterModel_().d();
}


const Foam::diameterModel& Foam::phaseModel::diameter() const
{
    return diameterModel_();
}


Foam::diameterModel& Foam::phaseModel::diameter()
{
    return diameterModel_();
}


void Foam::phaseModel::correct()
{
    diameterModel_->correct();
}


void Foam::phaseModel::correctContinuityError(const volScalarField& source)
{}


void Foam::phaseModel::correctKinematics()
{}


void Foam::phaseModel::correctThermo()
{}


void Foam::phaseModel::correctReactions()
{}


void Foam::phaseModel::correctSpecies()
{}


void Foam::phaseModel::predictMomentumTransport()
{}


void Foam::phaseModel::predictThermophysicalTransport()
{}


void Foam::phaseModel::correctMomentumTransport()
{}


void Foam::phaseModel::correctThermophysicalTransport()
{}


void Foam::phaseModel::correctUf()
{}


bool Foam::phaseModel::read()
{
    return diameterModel_->read(fluid_.subDict(name_));
}


void Foam::phaseModel::correctInflowOutflow(surfaceScalarField& alphaPhi) const
{
    surfaceScalarField::Boundary& alphaPhiBf = alphaPhi.boundaryFieldRef();
    const volScalarField::Boundary& alphaBf = boundaryField();
    const surfaceScalarField::Boundary& phiBf = phiRef().boundaryField();

    forAll(alphaPhiBf, patchi)
    {
        fvsPatchScalarField& alphaPhip = alphaPhiBf[patchi];

        if (!alphaPhip.coupled())
        {
            alphaPhip = phiBf[patchi]*alphaBf[patchi];
        }
    }
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~ Begin Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~//
bool Foam::phaseModel::continuous() const
{
    return diameterModel_->type() == "none";
}


const Foam::dimensionedScalar& Foam::phaseModel::continuousThreshold() const
{
    return continuousThreshold_;
}


Foam::tmp<Foam::volScalarField>
Foam::phaseModel::continuousStructureIndicator() const
{
    if (!continuous())
    {
        FatalErrorInFunction<< "Requesting continuous structure indicator "
            << "from a non-continuous phase: " << name_
            << exit(FatalError);
    }

    const volScalarField& alpha(*this);

    tmp<volScalarField> indicator
    (
        volScalarField::New
        (
            IOobject::groupName("continuousStructureIndicator", name_),
            pos(alpha - continuousThreshold_)
        )
    );
    fvc::spread(indicator.ref(), alpha, nInterfaceLayers_, 0.05);

    return indicator;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End Code Repository ~~~~~~~~~~~~~~~~~~~~~~~~~~//


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