CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

== operator

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree8Likes
  • 7 Post By hjasak
  • 1 Post By tomislav_maric

Reply
 
LinkBack Thread Tools Display Modes
Old   February 15, 2011, 07:57
Default == operator
  #1
Senior Member
 
Illya Shevchuk
Join Date: Aug 2009
Location: Darmstadt, Germany
Posts: 174
Rep Power: 8
linch is on a distinguished road
Hi foamers,

I know the "==" operator as an equality operator in C, i.e. (a==b) suppose to return true or false.

But then I can't understand following lines of interFoam:
Code:
rho == alpha1*rho1 + (scalar(1) - alpha1)*rho2;
and
Code:
solve
        (
            UEqn
         ==
            fvc::reconstruct
            (
                (
                    fvc::interpolate(interface.sigmaK())*fvc::snGrad(alpha1)
                  - ghf*fvc::snGrad(rho)
                  - fvc::snGrad(p_rgh)
                ) * mesh.magSf()
            )
        );
Can you please explain me the meaning of the == operator is these cases? What would be the difference if I would use
Code:
rho = alpha1*rho1 + (scalar(1) - alpha1)*rho2;
instead of == for the first one?

Best regards,
Ilya
linch is offline   Reply With Quote

Old   February 15, 2011, 08:43
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 14
l_r_mcglashan will become famous soon enough
I think...

= only does stuff to the internalField()
== does stuff to the internalField() AND the boundaryField()
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   February 15, 2011, 08:56
Default
  #3
Senior Member
 
Illya Shevchuk
Join Date: Aug 2009
Location: Darmstadt, Germany
Posts: 174
Rep Power: 8
linch is on a distinguished road
And how OF knows, if it has to use the C/C++ standard boolean or the OF-redefined "==" operator?
linch is offline   Reply With Quote

Old   February 16, 2011, 00:32
Default
  #4
Senior Member
 
santiagomarquezd's Avatar
 
Santiago Marquez Damian
Join Date: Aug 2009
Location: Santa Fe, Santa Fe, Argentina
Posts: 418
Rep Power: 15
santiagomarquezd will become famous soon enough
Hi, '=' is the usual operator used to replace the content of LHS with RHS. '==' is overloaded in fvMatrix class in order to write equations. It's only dividing sides.

Regards.
__________________
Santiago MÁRQUEZ DAMIÁN, Ph.D.
Post-doctoral Fellow
Research Center for Computational Mechanics (CIMEC) - CONICET/FICH-UNL
T.E.: 54-342-4511594 Ext. 1005
Güemes 3450 - (3000) Santa Fe
Santa Fe - Argentina
http://www.cimec.org.ar
santiagomarquezd is offline   Reply With Quote

Old   February 16, 2011, 08:11
Default
  #5
Senior Member
 
Illya Shevchuk
Join Date: Aug 2009
Location: Darmstadt, Germany
Posts: 174
Rep Power: 8
linch is on a distinguished road
Even in this case: rho == alpha1*rho1 + (scalar(1) - alpha1)*rho2; ?

I thought in this line the volScalar rho is being computed, but if I understand you correctly an equation for rho is being defined?

regards,
Ilya
linch is offline   Reply With Quote

Old   February 16, 2011, 09:00
Default
  #6
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Delft, Netherlands
Posts: 919
Rep Power: 17
akidess will become famous soon enough
No Ilya, in your example it is an assignment just like Laurence already stated further up. Santiago was explaining the case when having an "==" within a solve().

Last edited by akidess; February 16, 2011 at 09:01. Reason: extended answer
akidess is offline   Reply With Quote

Old   February 16, 2011, 09:22
Default
  #7
Senior Member
 
santiagomarquezd's Avatar
 
Santiago Marquez Damian
Join Date: Aug 2009
Location: Santa Fe, Santa Fe, Argentina
Posts: 418
Rep Power: 15
santiagomarquezd will become famous soon enough
Illya, from what source file did you take this line?:

Code:
rho = alpha1*rho1 + (scalar(1) - alpha1)*rho2;
Regards.
__________________
Santiago MÁRQUEZ DAMIÁN, Ph.D.
Post-doctoral Fellow
Research Center for Computational Mechanics (CIMEC) - CONICET/FICH-UNL
T.E.: 54-342-4511594 Ext. 1005
Güemes 3450 - (3000) Santa Fe
Santa Fe - Argentina
http://www.cimec.org.ar
santiagomarquezd is offline   Reply With Quote

Old   February 16, 2011, 12:47
Default
  #8
Senior Member
 
Illya Shevchuk
Join Date: Aug 2009
Location: Darmstadt, Germany
Posts: 174
Rep Power: 8
linch is on a distinguished road
Quote:
Originally Posted by santiagomarquezd View Post
Illya, from what source file did you take this line?
interFoam : alphaEqnSubCycle.H

Quote:
Originally Posted by akidess View Post
No Ilya, in your example it is an assignment just like Laurence already stated further up. Santiago was explaining the case when having an "==" within a solve().
Thanks, so for volFields it makes more sense to use == instead of =, if no boundary condition is prescribed for the LHS-field?
linch is offline   Reply With Quote

Old   February 16, 2011, 14:33
Default
  #9
Senior Member
 
santiagomarquezd's Avatar
 
Santiago Marquez Damian
Join Date: Aug 2009
Location: Santa Fe, Santa Fe, Argentina
Posts: 418
Rep Power: 15
santiagomarquezd will become famous soon enough
Aha, it's true I never paid attention in this particular '==' operator. Checking GeometricField.C, operator== and operator= copies both internal and boundary fields. The difference seems to be in another characteristic, operator= uses transfer method and operator== does not. But I don't realize the necessity of this operator in this case.

Regards.
__________________
Santiago MÁRQUEZ DAMIÁN, Ph.D.
Post-doctoral Fellow
Research Center for Computational Mechanics (CIMEC) - CONICET/FICH-UNL
T.E.: 54-342-4511594 Ext. 1005
Güemes 3450 - (3000) Santa Fe
Santa Fe - Argentina
http://www.cimec.org.ar

Last edited by santiagomarquezd; February 18, 2011 at 08:31.
santiagomarquezd is offline   Reply With Quote

Old   February 18, 2011, 08:07
Default
  #10
Senior Member
 
Illya Shevchuk
Join Date: Aug 2009
Location: Darmstadt, Germany
Posts: 174
Rep Power: 8
linch is on a distinguished road
Quote:
Originally Posted by santiagomarquezd View Post
Aha, ... operator= uses transfer method and operator== does not
Sry, but what does it mean?

Regards.
linch is offline   Reply With Quote

Old   February 19, 2011, 07:55
Default
  #11
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,763
Rep Power: 21
hjasak will become famous soon enough
Nope: operator== will FORCE assignment, even if patch field type says it should not be so. For example for a fixedValue patch, operator== will change its value and operator= will do nothing.

Hrv
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   February 19, 2011, 12:27
Default
  #12
Senior Member
 
santiagomarquezd's Avatar
 
Santiago Marquez Damian
Join Date: Aug 2009
Location: Santa Fe, Santa Fe, Argentina
Posts: 418
Rep Power: 15
santiagomarquezd will become famous soon enough
Aha, that explains the part I couldn't understand at all (GeometricField.C:01103):

Code:
01090 template<class Type, template<class> class PatchField, class GeoMesh>
01091 void Foam::GeometricField<Type, PatchField, GeoMesh>::operator==
01092 (
01093     const tmp<GeometricField<Type, PatchField, GeoMesh> >& tgf
01094 )
01095 {
01096     const GeometricField<Type, PatchField, GeoMesh>& gf = tgf();
01097 
01098     checkField(*this, gf, "==");
01099 
01100     // only equate field contents not ID
01101 
01102     dimensionedInternalField() = gf.dimensionedInternalField();
01103     boundaryField() == gf.boundaryField();
01104 
01105     tgf.clear();
01106 }
i think it ends doing this (fvPatchField.C):

Code:
00437 // Force an assignment, overriding fixedValue status
00438 template<class Type>
00439 void Foam::fvPatchField<Type>::operator==
00440 (
00441     const fvPatchField<Type>& ptf
00442 )
00443 {
00444     Field<Type>::operator=(ptf);
00445 }
Thanks for the clarification!!

Regards.

P.S. fvPatchField class don't have a 'fixed value flag' to avoid overriding?
__________________
Santiago MÁRQUEZ DAMIÁN, Ph.D.
Post-doctoral Fellow
Research Center for Computational Mechanics (CIMEC) - CONICET/FICH-UNL
T.E.: 54-342-4511594 Ext. 1005
Güemes 3450 - (3000) Santa Fe
Santa Fe - Argentina
http://www.cimec.org.ar
santiagomarquezd is offline   Reply With Quote

Old   September 26, 2011, 15:35
Default
  #13
Senior Member
 
Hisham's Avatar
 
Hisham El Safti
Join Date: Apr 2011
Location: Braunschweig, Germany
Posts: 247
Blog Entries: 10
Rep Power: 8
Hisham is on a distinguished road
Greetings,

I am developing a timeVaryingMapped BC for tractionDisplacement based on the timeV..M..FixedValue and the tractionDisplacement BCs. In the first (timeV..M..FixedValues) I find that values are assigned by the "==" operator:
Quote:
00710 this->operator==((1-s)*startSampledValues_ + s*endSampledValues_);
According to the discussion here, this should mean that the value (for given patch and variable) is imposed for each time it is calculated.
However, in the tractionDisplacement BC, the gradient() is calculated via a "=" operator.
I would like to know:

1. What is the meaning of "this->operator==(someField);" for a fixedGradient BC?
2. To assign gradient() for the custom BC I seek, should I use "gradient() == someVectorField;" or "gradient() = someVectorField;"

I appreciate your help/answer! Thanks
Hisham
Hisham is offline   Reply With Quote

Old   September 27, 2011, 03:12
Default
  #14
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 260
Blog Entries: 5
Rep Power: 11
tomislav_maric is on a distinguished road
Quote:
Originally Posted by linch View Post
And how OF knows, if it has to use the C/C++ standard boolean or the OF-redefined "==" operator?
Hi Illya,

operators are like other functions in C++. Although you don't see it, they look like, for example:

Code:
left operand == (right operand)
where "==" is the name of the function. Since they are overloaded (the final name of the function is defined by the arguments, return value, as well as by the "==" special character), the compiler recognizes by the types involved, which operator to call, in which class (type).

From fvMatrix.C:

Code:
template<class Type>
Foam::tmp<Foam::fvMatrix<Type> > Foam::operator==
(
    const fvMatrix<Type>& A,
    const fvMatrix<Type>& B
)
{
    checkMethod(A, B, "==");
    return (A - B);
}
"checkMethod" compares the "psi_" private data members (geometric fields) to see if they are compatible (mapped on the same mesh) and the dimensioned set to ensure compatible field dimensions.

Since it returns A - B (both coeff matrices), it seems to me that it just makes the entire algebraic system implicit (put B on the left side, or in your case, the field you get by fvc::reconstruct).

operator = ... from fvMatrix.C:
Code:
template<class Type>                                                      
void Foam::fvMatrix<Type>::operator=(const fvMatrix<Type>& fvmv)          
{
    if (this == &fvmv)                                                    
    {
        FatalErrorIn("fvMatrix<Type>::operator=(const fvMatrix<Type>&)")  
            << "attempted assignment to self"                             
            << abort(FatalError);                                         
    }                                                                     
        
    if (&psi_ != &(fvmv.psi_))
    {                                                                     
        FatalErrorIn("fvMatrix<Type>::operator=(const fvMatrix<Type>&)")  
            << "different fields"                                         
            << abort(FatalError);                                         
    }   
        
    lduMatrix::operator=(fvmv);                                           
    source_ = fvmv.source_;
    internalCoeffs_ = fvmv.internalCoeffs_;                               
    boundaryCoeffs_ = fvmv.boundaryCoeffs_;
                                                                          
    if (faceFluxCorrectionPtr_ && fvmv.faceFluxCorrectionPtr_)            
    {   
        *faceFluxCorrectionPtr_ = *fvmv.faceFluxCorrectionPtr_;           
    }                                                                     
    else if (fvmv.faceFluxCorrectionPtr_)                                 
    {   
        faceFluxCorrectionPtr_ =
            new GeometricField<Type, fvsPatchField, surfaceMesh>          
        (*fvmv.faceFluxCorrectionPtr_);                                   
    }                                                                     
}
It has to call the lduMatrix:perator= because it inherits from it, this guy goes over lower, upper and diagonal part and executes the data copy. The other part copies the rest of the data that is stored in the fvMatrix.

There seems to be no difference between

rho = something

and

rho == something

If rho is a vol*Field, == will copy both the boundary and the internal field:

01154 template<class Type, template<class> class PatchField, class GeoMesh>
01155 void Foam::GeometricField<Type, PatchField, GeoMesh>:perator==
01156 (
01157 const tmp<GeometricField<Type, PatchField, GeoMesh> >& tgf
01158 )
01159 {
01160 const GeometricField<Type, PatchField, GeoMesh>& gf = tgf();
01161
01162 checkField(*this, gf, "==");
01163
01164 // only equate field contents not ID
01165
01166 dimensionedInternalField() = gf.dimensionedInternalField();
01167 boundaryField() == gf.boundaryField();
01168
01169 tgf.clear();
01170 }



and "=" seems to be doing the same thing:

01082 template<class Type, template<class> class PatchField, class GeoMesh>
01083 void Foam::GeometricField<Type, PatchField, GeoMesh>:perator=
01084 (
01085 const GeometricField<Type, PatchField, GeoMesh>& gf
01086 )
01087 {
01088 if (this == &gf)
01089 {
01090 FatalErrorIn
01091 (
01092 "GeometricField<Type, PatchField, GeoMesh>:perator="
01093 "(const GeometricField<Type, PatchField, GeoMesh>&)"
01094 ) << "attempted assignment to self"
01095 << abort(FatalError);
01096 }
01097
01098 checkField(*this, gf, "=");
01099
01100 // only equate field contents not ID
01101
01102 dimensionedInternalField() = gf.dimensionedInternalField();
01103 boundaryField() = gf.boundaryField();
01104 }

Both operators, work on GeometricField<Type, PatchField, GeoMesh> ( example: vol*Field) from both sides.

Hope this helps,
Tomislav
sharonyue likes this.
tomislav_maric is offline   Reply With Quote

Old   September 27, 2011, 04:31
Default
  #15
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 14
l_r_mcglashan will become famous soon enough
Ah, but == will force an overwrite of all the boundary conditions, whereas = will not.
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   September 27, 2011, 05:06
Default
  #16
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 260
Blog Entries: 5
Rep Power: 11
tomislav_maric is on a distinguished road
Quote:
Originally Posted by l_r_mcglashan View Post
Ah, but == will force an overwrite of all the boundary conditions, whereas = will not.
I just checked it with an application, and you are completely right.... still,

Code:
01167     boundaryField() == gf.boundaryField();
I didn't notice that double "==" in the operator == of the GeometricField<blah ... and when I look in the BoundaryField, this operator is defined to do:

Code:
void operator==            (           const GeometricBoundaryField &            
           )            
                   Forced assignment to. 
 BoundaryField<Type, PatchField, BoundaryMesh>
and the definition does exactly that:

Code:
// Forced assignments                                                      
template<class Type, template<class> class PatchField, class GeoMesh>      
void Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricBoundaryField::
operator==  
(           
    const typename GeometricField<Type, PatchField, GeoMesh>::             
    GeometricBoundaryField& bf
)           
{                                                                          
    forAll((*this), patchI)
    {       
        this->operator[](patchI) == bf[patchI];                            
    }                                                                      
}
It's all in the GeometricField.C and GeometricBoundaryField.C (GeometricBoundaryField is composited into GeometricField).

The only question is, what does this button do: "this->operator[](patchI) == bf[patchI]"
tomislav_maric is offline   Reply With Quote

Reply

Tags
equality operator, openfoam operators

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a biharmonic operator Pascal_doran OpenFOAM Programming & Development 10 October 31, 2011 11:31
Modifying the laplacian operator mlawson OpenFOAM Running, Solving & CFD 11 September 7, 2011 12:30
Installation of OpenFOAM15dev antonio_ing OpenFOAM Installation 34 December 18, 2009 11:06
Laplacian operator and nuSgs for heat equation Bedotto OpenFOAM Programming & Development 4 September 7, 2009 15:08
Operator declaration in Thermophysical library lena OpenFOAM Running, Solving & CFD 0 March 12, 2009 10:47


All times are GMT -4. The time now is 02:48.