/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | 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 "linear.H"
#include "one.H"
#include "addToRunTimeSelectionTable.H"

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

namespace Foam
{
namespace blendingMethods
{
    defineTypeNameAndDebug(linear, 0);
    addToRunTimeSelectionTable(blendingMethod, linear, dictionary);
}
}


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

Foam::tmp<Foam::volScalarField> Foam::blendingMethods::linear::fContinuous
(
    const UPtrList<const volScalarField>& alphas,
    const label phaseSet,
    const label systemSet
) const
{
    tmp<volScalarField> x = this->x(alphas, phaseSet, systemSet);
    tmp<volScalarField> f =
        parameter(alphas, phaseSet, minFullyContinuousAlpha_);
    tmp<volScalarField> p =
        parameter(alphas, phaseSet, minPartlyContinuousAlpha_);
    return min(max((x - p())/max(f - p(), rootVSmall), zero()), one());
}


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

Foam::blendingMethods::linear::linear
(
    const dictionary& dict,
    const phaseInterface& interface
)
:
    blendingMethod(dict, interface),
    minFullyContinuousAlpha_
    (
        readParameters
        (
            "minFullyContinuousAlpha",
            dict,
            interface,
            {0, 1},
            true
        )
    ),
    minPartlyContinuousAlpha_
    (
        readParameters
        (
            "minPartlyContinuousAlpha",
            dict,
            interface,
            {0, 1},
            true
        )
    )
{
    forAllConstIter(phaseInterface, interface, iter)
    {
        const label i = iter.index();

        if
        (
            minFullyContinuousAlpha_[i].valid
         != minPartlyContinuousAlpha_[i].valid
        )
        {
            FatalErrorInFunction
                << "Both minimum fully and partly continuous alpha must be "
                << "supplied for phases that can become continuous. Only one "
                << "is supplied for " << iter().name() << exit(FatalError);
        }

        if
        (
            (
                canBeContinuous(i)
             && minFullyContinuousAlpha_[i].value
             <= minPartlyContinuousAlpha_[i].value
            )
        )
        {
            FatalErrorInFunction
                << "The fully continuous alpha specified for " << iter().name()
                << " is not greater than the partly continuous alpha"
                << exit(FatalError);
        }
    }

    if
    (
        canBeContinuous(0)
     && canBeContinuous(1)
     && (
            (
                minFullyContinuousAlpha_[0].value
              + minPartlyContinuousAlpha_[1].value
              < 1 - rootSmall
            )
         || (
                minFullyContinuousAlpha_[1].value
              + minPartlyContinuousAlpha_[0].value
              < 1 - rootSmall
            )
        )
    )
    {
        FatalErrorInFunction
            << typeName.capitalise() << " blending function for interface "
            << interface.name() << " is invalid in that it creates negative "
            << "coefficients for sub-modelled values. A valid function will "
            << "have fully continuous alphas that are greater than one minus "
            << "the partly continuous alphas in the opposite phase."
            << exit(FatalError);
    }
}


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

Foam::blendingMethods::linear::~linear()
{}


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

bool Foam::blendingMethods::linear::canBeContinuous(const label index) const
{
    return minFullyContinuousAlpha_[index].valid;
}


bool Foam::blendingMethods::linear::canSegregate() const
{
    return
        canBeContinuous(0)
     && canBeContinuous(1)
     && (
            (
                minFullyContinuousAlpha_[0].value
              + minPartlyContinuousAlpha_[1].value
              > 1 + rootSmall
            )
         || (
                minFullyContinuousAlpha_[1].value
              + minPartlyContinuousAlpha_[0].value
              > 1 + rootSmall
            )
        );
}


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