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

Need ur help

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

Reply
 
LinkBack Thread Tools Display Modes
Old   October 28, 2012, 22:44
Default Need ur help
  #1
New Member
 
Mandeep
Join Date: Aug 2012
Location: Canada
Posts: 8
Rep Power: 5
Rocky4 is on a distinguished road
Dear Foamers,

i have a volScalarField (say X) whose value at the patches and cells depends on the cell values of another volScalarField (sayY). i am able to change the value of X at the cells using

forAll(X, celli)
{ if (Y[celli] ==0)
{X[celli] =a;}
else
{X[celli]=b;}}

however i am unable to do the same for the patches of X
i.e if Y[celli] =0 where celli is any boundary cell then X[patchi] should be set to 'a' else it should be set to 'b'.something like this

forAll(X.boundaryField(), patchi)
{ //if (Y[celli] ==0) ,this represents any boundary cell value of field Y
{X.boundaryField()[patchi] =a;}
else
{X.boundaryField()[patchi]=b;}}

can someone can give a detailed code to accomplish this task?
i m new to openfoam. any kind of help is greatly appreciated. thanks in advance.

Last edited by Rocky4; October 29, 2012 at 00:47.
Rocky4 is offline   Reply With Quote

Old   October 29, 2012, 00:30
Default
  #2
Senior Member
 
Adhiraj
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 101
Rep Power: 6
adhiraj is on a distinguished road
I would do this:-
Code:
forAll(X.boundaryField(), patchi)
{
 forAll(X.boundaryField()[patchi],celli)
 {
  if(Y.boundaryField()[patchi][celli]==0)
      X.boundaryField()[patchi][celli]=a;
  else
     X.boundaryField()[patchi][celli]=b;
 }
}
NOTE:
  1. By the way, you have a typo in the if loop: you probably mean
    Code:
    Y[celli]==0
  2. For doubles or floats, comparison directly like this may fail. You may be better off checking if the absolute value is within a small region around zero.
  3. The volScalarFields can be directly copied and assigned to each other.
    I don't know if something like this would work:
    Code:
    X=(Y==0)?a:b
adhiraj is offline   Reply With Quote

Old   October 29, 2012, 00:46
Default
  #3
New Member
 
Mandeep
Join Date: Aug 2012
Location: Canada
Posts: 8
Rep Power: 5
Rocky4 is on a distinguished road
Hi Adhiraj
thanks a lot for your kind help .i will just try it out .and yes i had typo in the if loop.
thanks alot.
regards
mandeep
Rocky4 is offline   Reply With Quote

Old   October 29, 2012, 13:32
Default
  #4
New Member
 
Mandeep
Join Date: Aug 2012
Location: Canada
Posts: 8
Rep Power: 5
Rocky4 is on a distinguished road
Hi Adhiraj,
I tried it and worked great.in my actual code the x field is assigned value 'a' if 0<y<1 and 'b' if y=1. so the issue of 0 didn't comeup.once again thanks alot for ur kind help
regards
mandeep
Rocky4 is offline   Reply With Quote

Old   October 31, 2012, 06:54
Default
  #5
Senior Member
 
Adhiraj
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 101
Rep Power: 6
adhiraj is on a distinguished road
Good that it worked.

The issue is not with zero in particular; two doubles should not be compared directly like this
Code:
x==y
Rather you want to check if
Code:
abs(x-y)<=epsilon
where epsilon is a small number.
adhiraj is offline   Reply With Quote

Reply

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



All times are GMT -4. The time now is 17:30.