I have a problem with understanding how the boundary conditions can be modified. I am trying to apply the potentialFlux solver on a unit rectangular domain of which I have created a mesh using Gmsh. First I applied a uniform pressure difference between two opposing sides (labeled as "inflow" and "outflow") as boundary condition (uniform fixedValue ), while a zeroGradient boundary condition was applied on the two other sides. This case runs ok, but when I tried to apply a nonuniform fixedValue boundary condition on the "inflow" side, I get a weird result. Using the face centre coordinates of the "inflow" patch, I tried to apply a linearly varying pressure over the "inflow" patch. But when I look at the result, the pressure does not vary linearly over the "inflow" patch and the values don't match with my input values. I manipulated the boundary condition as follow:
{
in_bnd = p.ext_boundaryField()[inflow_ind]
in_centres = foam_vector2numpy(in_bnd.patch().Cf())
in_inds = numpy.argsort(in_centres[:,1])
ref_coords = np.linspace(0.0,1.0,51)
ref_vals = np.linspace(0.0,5.0,51)
new_in_vals = numpy.interp(numpy.sort(in_centres[:,1]),ref_coords,ref_vals)[in_inds]
for ind in range(in_bnd.size()):
in_bnd[ind] = 10.0 + new_in_vals[ind]
}
where 'foam_vector2numpy' is a function that I wrote to transfer an OpenFOAM vector List to a numpy array. I must be modifying the boundary conditions wrongly, otherwise I don't know how to explain the results. Is this a legitimated way of accessing and modifying the boundary conditions?
thanks,
Hon Fai
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can you get the desired results if you would write in C++?
pythonFlu is able to simplify C++ coding, but it basically does not perform any magic and redirect all its calls to corresponding C++ functionality. So, if you could express your thoughts in terms of the OpenFOAM C++ API, then it will be easier for me to help your in translating your ideas into Python.
I am not so familiar with the OpenFOAM C++ API, especially with how the datastructures work together. Most of the documentation deals with setting up the input files to run cases,
while I could find hardly any on how the data is actually stored and passed on during a simulation. I am not trying to implement a new boundary condition,
I just simply want to change the values in a fixedValue boundary condition assigned to a certain boundary patch, depending on the location of the face in the patch.
In the case I am trying (potentialFoam to solve the Poisson equation), I initiate the data structures with input files. The p and U input files were set as follow:
the p file:
dimensions [0 2 -2 0 0 0 0];
internalField uniform 0;
boundaryField
{
inflow
{
type fixedValue;
value 1.0;
}
outflow
{
type fixedValue;
value uniform 0.0;
}
side
{
type zeroGradient;
}
defaultFaces
{
type empty;
}
}
the U file:
dimensions ;
internalField uniform (0 0 0);
boundaryField
{
inflow
{
type calculated;
}
outflow
{
type calculated;
}
side
{
type zeroGradient;
}
defaultFaces
{
type empty;
}
}
I can of course use the input files to define a nonuniform fixedValue boundary condition with a List<scalar> containing the values.
The problem is that I don't know a priori how the faces are ordered in the patch so I don't know how to order the values in the List<scalar>.
Moreover, I am trying to construct an algorithm in which the boundary values are varied in an optimization routine, so I would like to change the boundary values interactively, which I tried using the code as described in my previous post.
I hope it is clear what it is that I am trying to do. I checked several times whether I assigned the right value to the right face, based on the coordinates of the face centres.
I am not sure what I am doing wrong. I assumed it should be possible to change the values in a boundary patch after initialization from file. Do I need to change anything else or call an update function?
Or do I really need to implement a new boundary condition? How would this be different from what I am doing now?
many thanks,
Hon Fai
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, I figured out what went wrong. I got the indexing wrong after all plus I got mislead by the visualization in vtk. I had to use the 'vtkCellDataToPointData' class to convert the cell data to point data before I could use the stream tracer in vtk. This is apparently done by averaging between neighbouring cells, which results in different values at the boundaries than what I defined and hence expected.
What I am trying works: the boundary values can be easily updated in pythonFlu, which I was looking for.
Many thanks and sorry again for the confusion,
Hon Fai
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have a problem with understanding how the boundary conditions can be modified. I am trying to apply the potentialFlux solver on a unit rectangular domain of which I have created a mesh using Gmsh. First I applied a uniform pressure difference between two opposing sides (labeled as "inflow" and "outflow") as boundary condition (uniform fixedValue ), while a zeroGradient boundary condition was applied on the two other sides. This case runs ok, but when I tried to apply a nonuniform fixedValue boundary condition on the "inflow" side, I get a weird result. Using the face centre coordinates of the "inflow" patch, I tried to apply a linearly varying pressure over the "inflow" patch. But when I look at the result, the pressure does not vary linearly over the "inflow" patch and the values don't match with my input values. I manipulated the boundary condition as follow:
where 'foam_vector2numpy' is a function that I wrote to transfer an OpenFOAM vector List to a numpy array. I must be modifying the boundary conditions wrongly, otherwise I don't know how to explain the results. Is this a legitimated way of accessing and modifying the boundary conditions?
thanks,
Hon Fai
Dear Hon Fai,
Can you get the desired results if you would write in C++?
pythonFlu is able to simplify C++ coding, but it basically does not perform any magic and redirect all its calls to corresponding C++ functionality. So, if you could express your thoughts in terms of the OpenFOAM C++ API, then it will be easier for me to help your in translating your ideas into Python.
By the way, I think that the right solution for you need will be the definition of a new boundary condition.
pythonFlu contains an example of how to do it properly in Python, look -
http://github.com/asimurzin/chtMultiRegionFlux/blob/master/chtMultiRegionFlux/r1_5/derivedFvPatchFields/solidWallHeatFluxTemperatureCoupledFvPatchScalarField.py
Best regards,
Alexey
Hi Alexey,
I am not so familiar with the OpenFOAM C++ API, especially with how the datastructures work together. Most of the documentation deals with setting up the input files to run cases,
while I could find hardly any on how the data is actually stored and passed on during a simulation. I am not trying to implement a new boundary condition,
I just simply want to change the values in a fixedValue boundary condition assigned to a certain boundary patch, depending on the location of the face in the patch.
In the case I am trying (potentialFoam to solve the Poisson equation), I initiate the data structures with input files. The p and U input files were set as follow:
the p file:
the U file:
dimensions ;
internalField uniform (0 0 0);
boundaryField
{
inflow
{
type calculated;
}
outflow
{
type calculated;
}
side
{
type zeroGradient;
}
defaultFaces
{
type empty;
}
}
I can of course use the input files to define a nonuniform fixedValue boundary condition with a List<scalar> containing the values.
The problem is that I don't know a priori how the faces are ordered in the patch so I don't know how to order the values in the List<scalar>.
Moreover, I am trying to construct an algorithm in which the boundary values are varied in an optimization routine, so I would like to change the boundary values interactively, which I tried using the code as described in my previous post.
I hope it is clear what it is that I am trying to do. I checked several times whether I assigned the right value to the right face, based on the coordinates of the face centres.
I am not sure what I am doing wrong. I assumed it should be possible to change the values in a boundary patch after initialization from file. Do I need to change anything else or call an update function?
Or do I really need to implement a new boundary condition? How would this be different from what I am doing now?
many thanks,
Hon Fai
Sorry,
I just saw a typo in my previous post.
the inflow boundary condition in the p file should be:
sorry,
Hon Fai
Ok, I figured out what went wrong. I got the indexing wrong after all plus I got mislead by the visualization in vtk. I had to use the 'vtkCellDataToPointData' class to convert the cell data to point data before I could use the stream tracer in vtk. This is apparently done by averaging between neighbouring cells, which results in different values at the boundaries than what I defined and hence expected.
What I am trying works: the boundary values can be easily updated in pythonFlu, which I was looking for.
Many thanks and sorry again for the confusion,
Hon Fai