how to change a volScalarField in bc?
I need to add a source term in the time loop, which is calculated in one of my boundary conditions.
First I defined the following field in createFields.H: Code:
Info<< "Creating field sourceH\n" << endl; But how can I change the values of the field in the bc??? I tried the following: Code:
scalarField sourceH = patch().lookupPatchField<volScalarField, scalar>("sourceH").patchInternalField(); |
This type of issue has come up a few times before. The problem is access: the boundary condition only has const access to anything it looks up through the objectRegistry, and private data it creates cannot be accessed at the top level because the solver uses fvPatchField as an interface.
Does your solver need to change it? If not, the solution is fairly simple. Instead of creating the source term at the top level, give it to your custom bc. Then use objectRegistry::lookupObject to find it in your solver. |
Thanks David for your explanations!
The solver doesn't have change the scalarField, but just adding it as a source term. To make sure I got you right: Did you mean to calculate a scalarField sourceH in my boundary condition and the call it in the solver by something like: Code:
const GeometricField<scalar, fvPatchField, volMesh>& Hquelle = objectRegistry::lookupObject<GeometricField<scalar, fvPatchField, volMesh> >("sourceH"); I'm not really sure about this or in general how to directly call the scalarField from the bc in the solver.... |
Very close. Instead of objectRegistry::lookupObject... use the name of the objectRegistry sourceH is registered to... you'll probably put it in the mesh, just like nearly everything else:
Code:
const volScalarField& Hquelle |
creating Field
I have still problems with creating the volScalarField in my BC:
I tried it like this: in the header file: Code:
class abflussWehrDS in the .C file: Code:
Foam:: Code:
abflussWehrDS.C: In constructor ‘Foam::abflussWehrDS::abflussWehrDS(const Foam::fvPatch&, const Foam::DimensionedField<Foam::Vector<double>, Foam::volMesh>&)’: |
"does not have any field named sourceH_"
This is your first problem. It looks like your header file has problems... but you have only quoted a portion of it. Code:
class abflussWehrDS Code:
class myClass Code:
class myClass |
misquote
Sorry. You're right! I left something out in my quotation:
Code:
class abflussWehrDS |
There is probably an error where it complains in the header file that it doesn't know what volScalarField means. This could be an #include problem, or circular dependency.
|
I tried to to solve this problem by adding
Code:
fixedValueFvPatchScalarField any other solutions to my initial problem? or is there still a possibility to do it this way????? thanks in advance!!!! |
What are your errors?
|
HTML Code:
abflussWehrDS.H:85: error: field ‘sourceH_’ has incomplete type |
"abflussWehrDS.H:85: error: field ‘sourceH_’ has incomplete type"
This is an #include problem. Either you didn't include the correct files that gives you the full definition of volScalarField, or you have circular dependency among your include files. The first option is likely, as volScalarField needs at least two files - the file that gives you the typedef volScalarField, and the one with the full definition. I strongly dislike these kinds of problems. |
I tried it by including
Code:
#include "fvPatchFields.H" Code:
class abflussWehrDS Code:
abflussWehrDS.H:84: error: field ‘sourceH_’ has incomplete type |
Quote:
The problem of creating the source term / a variable in the boundary condition for use in the main solver, is that if you don't add it correctly to the database, it will be deleted out of it as soon as the fvPatchField is destroyed. This means, as soon as you exit the boundary condition code. Anyways, the solution is quite simple. Let's say you want to store a scalarList in the database, for use anywhere (in the bc, the solver, etc) you would do: Code:
scalarList your_scalar_list; Code:
const IOList<scalar>& dbScalarList You would do this using the following (perfectly legal) C++ trick: Code:
const_cast<IOList<scalar>& >( dbScalarList ) = your_updated_scalar_list ; Kind regards, Francois |
Using const_cast is cheating!
|
Quote:
|
Quote:
Cheating..? Perhaps.. But most importantly: It works! :) |
Thanks!!
Thank you all for your help. The const_cast works:)
But I have one more question: Is it also possible to do this for a volScalarField. I could only find IOField<scalar>. Or is it possible to convert a scalarField into a volScalarField, which would also be sufficient. |
Well, actually, it has been a while since I implemented this hack, but if I recall properly, the store() function can store any pointer to an IOobject.
So, since your volScalarField is already an IOobject, what you are trying to do should be very simple to do: Code:
// with your_volScalarField created as it normally would be Code:
// with dbVolScalarField retrieved earlier from your database using lookupObject<volScalarField>() Francois. |
From
OpenFOAM-1.6-ext/src/finiteVolume/fields/fvPatchFields/derived/parabolicVelocity comes: Code:
void parabolicVelocityFvPatchVectorField::updateCoeffs() Just so you don't miss it , the key line is Code:
vectorField::operator= blah blah T. |
All times are GMT -4. The time now is 17:29. |