|
[Sponsors] |
February 14, 2018, 07:26 |
Set values of a volVectorField to zero
|
#1 |
New Member
Join Date: Jan 2018
Posts: 5
Rep Power: 8 |
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) { if(border[i] == 1) { U[i].component(0) = 0; U[i].component(1) = 0; U[i].component(2) = 0; } } Code:
if(border == 1) { U = 0; } |
|
February 14, 2018, 07:38 |
|
#2 |
New Member
Join Date: Dec 2015
Posts: 24
Rep Power: 10 |
You can use either
Code:
U = vector::zero Code:
U = vector(0.0,0.0,0.0) |
|
February 14, 2018, 08:00 |
|
#3 |
New Member
Join Date: Jan 2018
Posts: 5
Rep Power: 8 |
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: Different dimensions for = dimensions : [0 1 -1 0 0 0 0] = [0 0 0 0 0 0 0] |
|
February 14, 2018, 08:11 |
|
#4 |
New Member
Join Date: Dec 2015
Posts: 24
Rep Power: 10 |
Then try:
Code:
U = dimensionedVector("zero", dimensionSet(0,1,-1,0,0), vector::zero) |
|
February 14, 2018, 08:18 |
|
#5 |
New Member
Join Date: Jan 2018
Posts: 5
Rep Power: 8 |
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?
|
|
February 14, 2018, 08:25 |
|
#6 |
New Member
Join Date: Dec 2015
Posts: 24
Rep Power: 10 |
I can't test right now, but I would say this should work:
Code:
U[i] = vector::zero |
|
February 14, 2018, 09:11 |
|
#7 |
New Member
Join Date: Jan 2018
Posts: 5
Rep Power: 8 |
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; |
|
February 15, 2018, 10:58 |
|
#8 |
New Member
Join Date: Dec 2015
Posts: 24
Rep Power: 10 |
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. |
|
August 30, 2018, 07:43 |
|
#9 |
New Member
Kahlil Fredrick Cui
Join Date: Apr 2018
Posts: 29
Rep Power: 8 |
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) { if (alpha1[celli] < 0.01) { U[celli] == vector(0.0,0.0,0.0); } } Maybe I'm positioning the code wrong? or it the way it is written? Any help would be very much appreciated. Thank you! |
|
September 3, 2018, 06:27 |
|
#10 |
New Member
Join Date: Jan 2018
Posts: 5
Rep Power: 8 |
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. |
|
September 4, 2018, 10:02 |
|
#11 |
New Member
Kahlil Fredrick Cui
Join Date: Apr 2018
Posts: 29
Rep Power: 8 |
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. |
|
March 28, 2019, 03:04 |
|
#12 |
New Member
Sebastian
Join Date: Sep 2013
Posts: 14
Rep Power: 13 |
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! |
|
April 9, 2019, 01:12 |
|
#13 |
New Member
Sebastian
Join Date: Sep 2013
Posts: 14
Rep Power: 13 |
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. |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
correction of Grub after installing Windows XP and 8 | immortality | Lounge | 20 | January 5, 2014 18:41 |
Instalation on ubuntu 710 | basilwatson | OpenFOAM Installation | 17 | March 16, 2012 21:16 |
OF 1.6 | Ubuntu 9.10 (64bit) | GLIBCXX_3.4.11 not found | piprus | OpenFOAM Installation | 22 | February 25, 2010 14:43 |
Set nonuniform boundary values | nikwin | OpenFOAM Running, Solving & CFD | 1 | November 30, 2008 08:12 |
Help with GNUPlot | Renato. | Main CFD Forum | 6 | June 6, 2007 20:51 |