Set values of a volVectorField to zero
Hey there,
maybe you can help me with the following problem I'm currently dealing with: In my solver, I defined a volScalarField border which only contains the values 0 and 1. Now I would like to set the values of a volVectorField U to zero, but only in the cells where the value of border is 1. So far, I'm doing this with a forAll-loop, as follows: Code:
forAll(U,i) Code:
if(border == 1) |
You can use either
Code:
U = vector::zero Code:
U = vector(0.0,0.0,0.0) |
Thanks for your fast answer!
But using these two options will result in the dimension error, because U has the dimension m/s. Code:
--> FOAM FATAL ERROR: |
Then try:
Code:
U = dimensionedVector("zero", dimensionSet(0,1,-1,0,0), vector::zero) |
Thanks again, this works for setting the U-values to zero. But what about my if-condition? How do I have to write my if-condition, that it only sets the U-values to zero in these cells, where border = 1?
|
I can't test right now, but I would say this should work:
Code:
U[i] = vector::zero |
I found another (quite simple) way to solve the problem. If anyone is interested: Just multiply the volVectorField U with the function (-1)*(border-1). This function equals 0 for all cells where border=1 and equals 1 for all cells where border=0.
Code:
U = (-1)*(border-1)*U; |
I'm glad you have found a nice solution!
Re-reading your question I realized that you stated that a forAll is not efficient. That's not true. This is C++ not matlab. In general, if you can encompass all your calculations in a single forAll, that's much better that several "hiden" forAll, because of cache misses and redundant memory accesses. Do not presume that every operation in OpenFOAM is evaluated lazily (i.e. with a single forAll). Said that, I prefer code readability over performance. |
HI!
I'm doing something somewhat related. I want to set the velocity of a cell to 0 if the value or alpha1 (water phase) is less than a certain value (like <0.01) I implemented this in Ueqn right after the convective term: Code:
forAll(U, celli) Maybe I'm positioning the code wrong? or it the way it is written? Any help would be very much appreciated. Thank you! |
Hey Cuikahlil,
in which solver are you implementing this code? For me it seems right and I copied your code into my own pimpleFoam-based solver, here it works. |
Hi!
Thanks for your reply. Actually I realized late that it did work. What I intially expected was that all U values will actually be zero, but I didn't realize that I was only zero-ing the convective contribution and not the contribution from the other side. Anyways, sorry for the bother and thank you for your time. |
Hey,
I've used a similar approach like GustavGans in my solver: Code:
U *= (1-field); field is containing 0 and 1 values, depending on the temperature. However, I was wondering at which place it is the best to implement this code. I tested it at the end of the UEqn.H call. But the area, where field equals 1 is still seeing some velocity calculation and values that are greater than 0. Where exactly did you guys implement this approach, in order to get it to work as intended? Thanks in advance! |
I came up with a solution:
We, of course, have to implement this line after the momentum corrector in pEqn was called, so e.g. at the end of pEqn.C. However, to me, this doesn't seam to be an elegant solution. So I will try to add a sink term to the PDE. I will post the solution here, once it is satisfactory. |
All times are GMT -4. The time now is 02:38. |