CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Type mismatch: Assigning a scalar to a Field<Type> inside templated BC code (https://www.cfd-online.com/Forums/openfoam-programming-development/177899-type-mismatch-assigning-scalar-field-type-inside-templated-bc-code.html)

karlli September 23, 2016 10:09

Type mismatch: Assigning a scalar to a Field<Type> inside templated BC code
 
Dear all,
I am trying to implement a custom boundary condition based on the uniformJumpFvPatchField class. This is a cyclic boundary condition that applies a uniform (but possibly time-varying) jump across the cyclic patch pair. I want to assign my own jump value from inside the code, rather than from a BC dict, but have run into type conversion (?) problems.

Background:
Typical uniformJump usage:

Code:

    cyc_inlet
    {
        type            uniformJump;
        patchType      cyclic;
        jumpTable      constant -10;
        value          uniform 310;
    }

The uniformJump BC is derived from fixedJump, where the (time varying) jump is stored in a table (in uniformJumpFvPatchField.H):


Code:

template<class Type>
class uniformJumpFvPatchField
:
    public fixedJumpFvPatchField<Type>
{

protected:

    // Protected data

        //- "jump" table
        autoPtr<Function1<Type>> jumpTable_;

The jump itself is a field associated with the patch, which is inherited from the fixedJumpFvPatchField class:


Code:

template<class Type>
class fixedJumpFvPatchField
:
    public jumpCyclicFvPatchField<Type>
{

protected:

    // Protected data

        //- "jump" field
        Field<Type> jump_;

My attempt:
As a starting point for my custom BC I want to assign a scalar value from inside the code as the jump value, instead of the value given by the jumpTable. Later, this scalar will depend on time-varying computations on the patch. My attempt so far (original code commented) is:

Code:

template<class Type>
void Foam::bulkTempFvPatchField<Type>::updateCoeffs()
{
    if (this->updated())
    {
        return;
    }

    if (this->cyclicPatch().owner())
    {
//        this->jump_ = jumpTable_->value(this->db().time().value());
       
        scalar targetJump(400);
        Field<scalar> myJump(this->patch().size(),targetJump);
       
        this->jump_ = myJump;
    }

The idea was to create a scalar, assign it uniformly to a field, and then apply that field to jump_ (which also is of the Field type). In compiling, I run into the following error:

Code:

error: no match for ‘operator=’ (operand types are ‘Foam::Field<Foam::Tensor<double> >’ and ‘const Foam::Field<double>’)
        this->jump_ = myJump;




Question:
I assume the error is related to the fact that the class is templated, and therefore need to work with all possible Type:s of Field. Is it possible to implement workaround where jump_ is set to an empty (or zero) Field for all Type:s except for Type being scalar? I have not been able to find the right C++ way to do this.
As an alternative, is there any way to redefine the class to only be a scalar boundary condition, despite the base class (fixedJumpFvPatchField) being templated? I have tried commenting out all template<class Type> lines and replacing Type with scalar, to no avail. The compiler then complains that the new class is not a template.

I realize this is more of a general C++ question rather than a specific OpenFOAM one, but I would still be useful to resolve it on this forum. I have not found any similar threads here so far.

Best regards,
Karl

EDIT: It IS possible to compile the BC as a scalar patch field by commenting out the following functions in the forward declaration files. This does however not solve my problem, since I then cannot use it (why?) on a cyclic patch.
makePatchFields(myBC);
template<class Type> class myBCFvPatchField;
makePatchTypeFieldTypedefs(myBC);

pete20r2 August 25, 2017 14:06

Hi karlii,
Did you ever get anywhere with this?
I am trying to modify uniformJump to maintain an average value, if you got these first steps working that would be cool.

karlli August 28, 2017 01:25

Peter,
Thank you for reminding me of this thread! I never managed to modify the cyclic boundary condition, but I have had good experiences with using/modifying the mapped and mappedField boundary conditions. They can be used to emulate a cyclic condition and have an option to set the area average value after mapping.

Regards,
Karl


All times are GMT -4. The time now is 01:25.