void Foam::wavyFilmHeightFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const label patchI = patch().index();

    const scalar t = db().time().timeOutputValue();

    // retrieve the film region from the database

    const regionModels::regionModel& region =
        db().time().lookupObject<regionModels::regionModel>
        (
            "surfaceFilmProperties"
        );

    const regionModels::singleLayerRegion& film =
        dynamic_cast<const regionModels::singleLayerRegion&>(region);

    // calculate the vector tangential to the patch

    const volVectorField& nHat = film.nHat();

    const vectorField nHatp(nHat.boundaryField()[patchI].patchInternalField());

    vectorField nTan(nHatp ^ patch().nf());
    nTan /= mag(nTan) + ROOTVSMALL;

    // calculate distance in patch tangential direction

    const vectorField& Cf = patch().Cf();
    scalarField d(nTan & Cf);
    d -= min(d);

    // calculate the wavy film height

    const scalar mean = mean_->value(t);
    const scalar a = a_->value(t);
    const scalar omega = omega_->value(t);

    operator==(mean + a*sin(omega*constant::mathematical::twoPi*d));

    fixedValueFvPatchScalarField::updateCoeffs();
}
void Foam::inclinedFilmNusseltHeightFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const label patchI = patch().index();

    const scalar t = db().time().timeOutputValue();

    // retrieve the film region from the database

    const regionModels::regionModel& region =
        db().time().lookupObject<regionModels::regionModel>
        (
            "surfaceFilmProperties"
        );

    const regionModels::surfaceFilmModels::kinematicSingleLayer& film =
        dynamic_cast
        <
            const regionModels::surfaceFilmModels::kinematicSingleLayer&
        >(region);

    // calculate the vector tangential to the patch

    const vectorField n(patch().nf());

    const volVectorField& nHat = film.nHat();

    const vectorField nHatp(nHat.boundaryField()[patchI].patchInternalField());

    vectorField nTan(nHatp ^ n);
    nTan /= mag(nTan) + ROOTVSMALL;

    // calculate distance in patch tangential direction

    const vectorField& Cf = patch().Cf();
    scalarField d(nTan & Cf);

    // TODO: currently re-evaluating the entire gTan field to return this patch
    const scalarField gTan(film.gTan()().boundaryField()[patchI] & n);

    if (patch().size() && (max(mag(gTan)) < SMALL))
    {
       WarningIn
        (
            "void Foam::inclinedFilmNusseltHeightFvPatchScalarField::"
            "updateCoeffs()"
        )
            << "Tangential gravity component is zero.  This boundary condition "
            << "is designed to operate on patches inclined with respect to "
            << "gravity"
            << nl;
    }

    const volScalarField& mu = film.mu();
    // The use of the first internal cell for properties doesn't seem correct, kvm
    const scalarField mup(mu.boundaryField()[patchI].patchInternalField());
    // const scalarField mup(mu.boundaryField()[patchI]);

    const volScalarField& rho = film.rho();
    const scalarField rhop(rho.boundaryField()[patchI].patchInternalField());
    // const scalarField rhop(rho.boundaryField()[patchI]);
    
    // calculate the wavy film height

    const scalar GMean = GammaMean_->value(t);
    const scalar a = a_->value(t);
    const scalar omega = omega_->value(t);


    // solve for deltaMean via Bi-Section method
    scalar fxC = 10.0;
    scalar tol = 0.00001;
    label iter=0;
    scalar deltaMeanA = 2e-2;
    scalar deltaMeanB = 2e-6;
    scalar deltaMeanC;

    //  Info << " acoeff " << a << nl;
    scalarField C(pow(3.0*sqr(mup/rhop)/mup/(-gTan + ROOTVSMALL), 0.33333333));
    scalarField delta(deltaMeanA + a*sin(omega*constant::mathematical::twoPi*d));

    while(fabs(fxC)>tol){

        label deltaSize = delta.size();
        reduce(deltaSize, sumOp<label>());
        delta = (deltaMeanA + a*sin(omega*constant::mathematical::twoPi*d));
        scalar fxA = GMean - gSum(pow(delta/C,3))/deltaSize; 
        // DEBUG(delta.size());
        // DEBUG(deltaSize);
        if(fxA > 0.0){
            WarningIn
                (
                 "void Foam::inclinedFilmNusseltHeightFvPatchScalarField::"
                 "updateCoeffs()"
                )
                << "Initial guess for deltaMeanA too low:"
                << deltaMeanA
                << nl;
        }


        delta = (deltaMeanB + a*sin(omega*constant::mathematical::twoPi*d));
        scalar fxB = GMean - gSum(pow(delta/C,3))/deltaSize;

        if(fxB < 0.0){
            WarningIn
                (
                 "void Foam::inclinedFilmNusseltHeightFvPatchScalarField::"
                 "updateCoeffs()"
                )
                << "Initial guess for deltaMeanB too high:"
                << deltaMeanB
                << nl;
        }
        //  Info << "fxA " << fxA << nl;
        //  Info << "fxB " << fxB << nl;

        deltaMeanC = 0.5*(deltaMeanA+deltaMeanB);

        delta = (deltaMeanC + a*sin(omega*constant::mathematical::twoPi*d));
        fxC = GMean - gSum(pow(delta/C,3))/deltaSize;

        //  Info << iter << " fxC " << fxC << nl;

        if( fxC < 0.0 ) {
            deltaMeanA = deltaMeanC;
        }
        else{
            deltaMeanB = deltaMeanC;
        }
        iter++;
        if(iter>1e5){
            WarningIn
                (
                 "void Foam::inclinedFilmNusseltHeightFvPatchScalarField::"
                 "updateCoeffs()"
                )
                << "Maximum number of bisection method iterations reached."
                << nl;
            break;
        }
    }

    scalar deltaMean = deltaMeanC;
    delta = (deltaMean + a*sin(omega*constant::mathematical::twoPi*d));
    //  Info << "delta " << delta << nl;

    // scalarField G(GMean + GMean*a*sin(omega*constant::mathematical::twoPi*d));
    
    // correction to mean 
    // scalar uncorrectedMean = gSum(G)/G.size();
    // DEBUG(uncorrectedMean);
    // G *= GMean/(uncorrectedMean+SMALL);

    // set internal values
    // volScalarField& rho2 = const_cast<volScalarField&>(film.rho());
    // const labelUList& faceCells = patch().faceCells();
    // forAll(faceCells,i)
    // {
    //     label cellI = faceCells[i];
    //     rho2[cellI] = 1.0;
    // }
    // Info << rho2 << nl;

    scalarField G(pow(delta/C,3));

    const scalarField Re(max(G, 0.0)/mup);

    // DEBUG(mup[0]);
    // DEBUG(rhop[0]);
    // DEBUG(gTan[0]);
    // DEBUG(Re[0]);
    // DEBUG(G[0]);
    // const scalarField h(pow(3.0*sqr(mup/rhop)/(-gTan + ROOTVSMALL), 0.33333333)*pow(Re, 0.33333333));
    // DEBUG(h[0]);

    operator==
    (
        pow(3.0*sqr(mup/rhop)/(-gTan + ROOTVSMALL), 0.33333333)*pow(Re, 0.33333333)
    );

    fixedValueFvPatchScalarField::updateCoeffs();
}
void Foam::inclinedFilmNusseltInletVelocityFvPatchVectorField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const label patchi = patch().index();

    // retrieve the film region from the database

    const regionModels::regionModel& region =
        db().time().lookupObject<regionModels::regionModel>
        (
            "surfaceFilmProperties"
        );

    const regionModels::surfaceFilmModels::kinematicSingleLayer& film =
        dynamic_cast
        <
            const regionModels::surfaceFilmModels::kinematicSingleLayer&
        >(region);

    // calculate the vector tangential to the patch
    // note: normal pointing into the domain
    const vectorField n(-patch().nf());

    // TODO: currently re-evaluating the entire gTan field to return this patch
    const scalarField gTan(film.gTan()().boundaryField()[patchi] & n);

    if (patch().size() && (max(mag(gTan)) < SMALL))
    {
        WarningInFunction
            << "is designed to operate on patches inclined with respect to "
            << "gravity"
            << endl;
    }

    const volVectorField& nHat = film.nHat();

    const vectorField nHatp(nHat.boundaryField()[patchi].patchInternalField());

    vectorField nTan(nHatp ^ n);
    nTan /= mag(nTan) + ROOTVSMALL;

    // calculate distance in patch tangential direction

    const vectorField& Cf = patch().Cf();
    scalarField d(nTan & Cf);

    // calculate the wavy film height

    const scalar t = db().time().timeOutputValue();

    const scalar GMean = GammaMean_->value(t);
    const scalar a = a_->value(t);
    const scalar omega = omega_->value(t);

    const scalarField G(GMean + a*sin(omega*constant::mathematical::twoPi*d));

    const volScalarField& mu = film.mu();
    const scalarField mup(mu.boundaryField()[patchi].patchInternalField());

    const volScalarField& rho = film.rho();
    const scalarField rhop(rho.boundaryField()[patchi].patchInternalField());

    const scalarField Re(max(G, scalar(0.0))/mup);

    operator==(n*pow(gTan*mup/(3.0*rhop), 0.333)*pow(Re, 0.666));

    fixedValueFvPatchVectorField::updateCoeffs();
}