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

Difference between = and ==

Register Blogs Community New Posts Updated Threads Search

Like Tree16Likes
  • 1 Post By manuc
  • 11 Post By marupio
  • 3 Post By manuc
  • 1 Post By elmo555

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 22, 2016, 05:35
Default Difference between = and ==
  #1
Senior Member
 
Manu Chakkingal
Join Date: Feb 2016
Location: Delft, Netherlands
Posts: 129
Rep Power: 10
manuc is on a distinguished road
Hello

I came across the usage of = and ==.
Does
= implies at calculating and assigning value of RHS to the LHS

== (initially assumed to be equality check operator), but found in cases where equations are solved and assigned similar to =

What is the exact difference /w the two and where it should be used in specific
Luttappy likes this.
manuc is offline   Reply With Quote

Old   February 22, 2016, 12:27
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
C++ allows operator overloading, which means that these operators could mean anything. A good design would be to keep it logical and consistent.

In OpenFOAM, we can use mathematical operators on GeometricFields, such as the velocity field. It makes sense that = means assignment, but OpenFOAM is very clever, and will apply the correct meaning to each of the boundary conditions on the field.

In the case of fixedValue, OpenFOAM will assume that you are doing routine physics calculations on your field... in these cases, we don't want to overwrite the fixedValue assigned at the start. So, U = A + B would do nothing to any of U's fixed value boundary conditions.

But there are situations where you do want to override the fixedValue. For instance, some boundary conditions use fixedValue as a base, but calculate a new value at every timestep. They need to override the value.

The design decision that was made is to use operator== for that. Kind of like a 'super assignment'. So for field variables, and the associated math, == is not a test... it is assignment.

In general, use operator= unless you are really certain you need to override any of the fixedValue.
hk318i, jherb, Saideep and 8 others like this.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   February 23, 2016, 06:57
Default
  #3
Senior Member
 
Manu Chakkingal
Join Date: Feb 2016
Location: Delft, Netherlands
Posts: 129
Rep Power: 10
manuc is on a distinguished road
Quote:
Originally Posted by marupio View Post
C++ allows operator overloading, which means that these operators could mean anything. A good design would be to keep it logical and consistent.

In OpenFOAM, we can use mathematical operators on GeometricFields, such as the velocity field. It makes sense that = means assignment, but OpenFOAM is very clever, and will apply the correct meaning to each of the boundary conditions on the field.

In the case of fixedValue, OpenFOAM will assume that you are doing routine physics calculations on your field... in these cases, we don't want to overwrite the fixedValue assigned at the start. So, U = A + B would do nothing to any of U's fixed value boundary conditions.

But there are situations where you do want to override the fixedValue. For instance, some boundary conditions use fixedValue as a base, but calculate a new value at every timestep. They need to override the value.

The design decision that was made is to use operator== for that. Kind of like a 'super assignment'. So for field variables, and the associated math, == is not a test... it is assignment.

In general, use operator= unless you are really certain you need to override any of the fixedValue.
Thank you for the reply ..
In short == is a forceful assignment at boundaries..while = leaves out the boundaries during assignment
Saideep, elmo555 and Kummi like this.
manuc is offline   Reply With Quote

Old   October 11, 2017, 11:07
Default
  #4
Member
 
sibo
Join Date: Oct 2016
Location: Chicago
Posts: 55
Rep Power: 10
sibo is on a distinguished road
Quote:
Originally Posted by marupio View Post
C++ allows operator overloading, which means that these operators could mean anything. A good design would be to keep it logical and consistent.

In OpenFOAM, we can use mathematical operators on GeometricFields, such as the velocity field. It makes sense that = means assignment, but OpenFOAM is very clever, and will apply the correct meaning to each of the boundary conditions on the field.

In the case of fixedValue, OpenFOAM will assume that you are doing routine physics calculations on your field... in these cases, we don't want to overwrite the fixedValue assigned at the start. So, U = A + B would do nothing to any of U's fixed value boundary conditions.

But there are situations where you do want to override the fixedValue. For instance, some boundary conditions use fixedValue as a base, but calculate a new value at every timestep. They need to override the value.

The design decision that was made is to use operator== for that. Kind of like a 'super assignment'. So for field variables, and the associated math, == is not a test... it is assignment.

In general, use operator= unless you are really certain you need to override any of the fixedValue.
Hi David,

Thanks for your detailed explanation.
I want to create a dynamic mesh solver and the situation in my case is exactly the same as you mentioned.
I want to use fixedValue as a base for the wall's boundary condition and then give it a calculated displacement value to make it move every time step.

Therefore, I use this "==" operator. want to assign the calculated "dispVals" to "PointDisplacement".
Code:
PointDisplacement.boundaryField()[patchID] == dispVals;
But when I compile, i got the following error message:
Code:
error: no match for ‘operator==’ (operand types are ‘const Foam::pointPatchField<Foam::Vector<double> >’ and ‘Foam::vectorField {aka Foam::Field<Foam::Vector<double> >}’)
    PointDisplacement.boundaryField()[patchID] == dispVals;
                                                                        ^
I have no idea why this error comes out, can you help me with this?
Any suggestion would be greatly appreciated!
Thanks!
sibo is offline   Reply With Quote

Old   January 18, 2018, 11:46
Default
  #5
Member
 
Lennart
Join Date: Feb 2016
Posts: 46
Rep Power: 10
elmo555 is on a distinguished road
Quote:
Originally Posted by sibo View Post
Therefore, I use this "==" operator. want to assign the calculated "dispVals" to "PointDisplacement".
Code:
PointDisplacement.boundaryField()[patchID] == dispVals;
But when I compile, i got the following error message:
Code:
error: no match for ‘operator==’ (operand types are ‘const Foam::pointPatchField<Foam::Vector<double> >’ and ‘Foam::vectorField {aka Foam::Field<Foam::Vector<double> >}’)
    PointDisplacement.boundaryField()[patchID] == dispVals;
                                                                        ^
I have no idea why this error comes out, can you help me with this?
Any suggestion would be greatly appreciated!
Thanks!
You'll probably need to use boundaryFieldRef() instead of boundaryField(), because boundaryField() returns a const, and you're trying to write into the field:

Code:
PointDisplacement.boundaryFieldRef()[patchID] == dispVals;
Michael@UW likes this.
elmo555 is offline   Reply With Quote

Reply


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 Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 16:18.