CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Problems creating volscalarfield (https://www.cfd-online.com/Forums/openfoam-programming-development/131468-problems-creating-volscalarfield.html)

sur4j March 15, 2014 07:59

Problems creating volscalarfield
 
I am trying to modify the solidDisplacementFoam solver so that it has an additional temperature dependant variable within it in the form of a scalar field, this variable has been programmed as shown in the main .C file:

Code:

forAll(T.internalField(), cellI)
{   
    float SCholder[400];

    SC[cellI] = SCholder[cellI] + 2/((2^((T.internalField()[cellI]))));
    SCholder[cellI] = SC[cellI];
}

and the scalar field is shown in the header file as:

Code:

volScalarField SC
(
    IOobject
    (
        "SC",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh
);

Problems I am facing:
  • When I define exp and base as double or float I get an error, how do I prevent this?
  • The array SCholder's size is defined as the number of cells, when I leave this blank I get an error saying undefined array size therefore, I have had to specify it as 400 for a specific simulation, can this be defined with a variable so that I do not have to change it every time I change the No of cells in a mesh?
  • The solver compiles fine with the code shown above however, when I run it on my test case I get the following error:
Code:

--> FOAM FATAL ERROR:
NO_READ specified for read-constructor of object SC of class IOobject

    From function regIOobject::readStream()
    in file db/regIOobject/regIOobjectRead.C at line 46.

FOAM aborting

I specified as NO_READ so that I would not have to create the initial conditions in the 0 folder because when I done this previously, the solver didn't work as expected and update SC accordingly, instead it just kept the initial conditions set for it in 0 in all time folders

alexeym March 15, 2014 09:16

Hi,

1. If you'd like to construct volScalarField with NO_READ, you have to use different constructor, for example:

Code:

    volScalarField p
    (
        IOobject
        (
            "p",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        p_rgh + rho*gh
    );

here volScalarField is created using another fields, or

Code:

volScalarField K
(
    IOobject
    (
        "K",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh,
    dimless/dimTime
);

here just dimensions are specified.

2. You create SCholder array inside the loop, so it won't be available after you exit the loop. Do you plan to use it outside forAll macro? And if you need dynamically allocated array, you can use Field type. Something like:

Code:

Field SCholder(SC.size())

sur4j March 15, 2014 09:51

Thank you for your reply however, I am now confused on how I need to implement this in the solver. The code is in the time loop so every time step it will run through all nodes and will calculate:
base = (((T.internalField()[cellI])))
and then calculate SC by looking at the previous SC and adding 2/base onto this. It will then update SCholder so that on the next time loop the code can see the previous SC, SCholder is not used anywhere else in the code.

Could you please guide me on how I should add this to the solver?

alexeym March 15, 2014 12:18

From your post I did not get why you need two fields: SC and SCholder. If SC is just recalculated on every time step by addition certain values to it, why not just do this:

Code:

forAll(T, cellI)
{
    SC[cellI] += 2/(250*(2^((100 - T[cellI])/10)));
}

though if you really need to keep values of SC (maybe it is modified somewhere else), create SC0 as you create SC and then:

Code:

forAll(T, cellI)
{
    SC[cellI] = SC0[cellI] + 2/(250*(2^((100 - T[cellI])/10)));
}
SC0 = SC

As a description of the problem is rather vague, I'm not quite sure that my reply is relevant to what you're looking for.

sur4j March 15, 2014 12:36

Thank you very much, that simplifies things for me. The part I am stuck with is the creation of the volScalarField for this case so that I can view the SC results from my simulation, I do not understand how to set up the constructor. I currently have:

Code:

volScalarField SC
(
    IOobject
    (
        "SC",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh//but I dont know what to put down here
);

SC is a percentage therefore it has no units and should start at 0.

alexeym March 15, 2014 17:39

Then you can create the field with

Code:

volScalarField SC
(
    IOobject
    (
        "SC",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh,
    dimless // as SC units are percents
);

this is an invocation of the following constructor - http://foam.sourceforge.net/docs/cpp...0d8f8a5fec0dc8, and it is friendly with NO_READ option.

sur4j March 16, 2014 06:20

Thank you very much for your help.

When I try:
Code:

forAll(T, cellI) {   
SC[cellI] += 2/((((T[cellI])/10)));
}

I get the following error when using the wmake command on the solver:
Code:

Making dependency list for source file cureFoam.C
SOURCE=cureFoam.C ;  g++ -m32 -Dlinux -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3  -DNoRepository -ftemplate-depth-100 -I/opt/openfoam222/src/finiteVolume/lnInclude -ItractionDisplacement/lnInclude -IlnInclude -I. -I/opt/openfoam222/src/OpenFOAM/lnInclude -I/opt/openfoam222/src/OSspecific/POSIX/lnInclude  -fPIC -c $SOURCE -o Make/linuxGccDPOpt/cureFoam.o
cureFoam.C: In function ‘int main(int, char**)’:
cureFoam.C:81:50: error: invalid operands of types ‘int’ and ‘double’ to binary ‘operator^’
readSolidDisplacementFoamControls.H:3:11: warning: unused variable ‘nCorr’ [-Wunused-variable]
readSolidDisplacementFoamControls.H:5:8: warning: unused variable ‘convergenceTolerance’ [-Wunused-variable]
make: *** [Make/linuxGccDPOpt/cureFoam.o] Error 1

When I change the solver code to:
Code:

int exp;
int base;
    exp = ((100 - T.internalField()[cellI])/10);
    base = 250*(2^exp);
    SC.internalField()[cellI] += 2/base;

It compiles fine but when I run this on a test case I only get the first cell value and every other cell value stays 0, this value in the first cell is also constant in all time step folders.

How do I get this to work with doubles rather than integers and also why is it only calculating the first cell and why is it constant in all time step folders?

alexeym March 16, 2014 06:39

:D

^ operator in C/C++ is something different (http://en.wikipedia.org/wiki/Operato...wise_operators)

If you need exponentiation, you have to use pow (http://www.cplusplus.com/reference/cmath/pow/) function.

I did not pay attention to this as I thought expression you'd like to add to SC is valid.

sur4j March 16, 2014 07:49

Thanks :)

It compiles fine now with but I am still having the same problem with only a single value appearing and only for the first cell in all time step files:
Code:

FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "67";
    object      SC;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 0 0 0 0];

internalField  nonuniform List<scalar>
400
(
6.17207e-264
0
0
0
0
0
0
0
.....

It works as expected and populates all cells in the time step folders if I use the following code for example:
Code:

forAll(T.internalField(), cellI)
{
SC.internalField()[cellI] += T.internalField()[cellI];
}

So it is definitely a problem with the code in the forAll loop in the main .C file, I cant figure out why this is happening and any help from you with this would be greatly appreciated. Thanks.

alexeym March 16, 2014 07:53

OK. What is the meaning of that 100?

sur4j March 16, 2014 08:18

It is the temperature at which the reaction starts, I had tested this in excel a while back and it worked fine, just having problems adding it into openfoam.

alexeym March 16, 2014 08:22

Units?

You can plot 2/250*(pow(2,((100 - T)/10))) in paraFoam using Calculator filter and see what you're adding to SC.

sur4j March 16, 2014 08:23

Ahh sorry about this, messed up on the brackets :D

Closed the denominator in brackets and works fine now. Thank you very much for your help!

sharonyue April 2, 2014 21:59

Hi guys,

Im facing the same "NO READ" problem.

In twoPhaseEulerFoam, just like k-Epsilon model in this solver. I make the drag model very simple so I dump the template and make the GidaspowSchillerNaumann model into the main code. But its alike with the original one.

Post all the code is tough, but something wrong is here:

Code:

volScalarField K
    (
        IOobject
        (
            "K",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        0.75*Cds*phase2->rho()*bp/d1
        //0.75*Cds*phase2->rho()*Ur*bp/d1
    );

This can be compiled with no error. But when I run the case:
Code:

--> FOAM FATAL ERROR:
NO_READ specified for read-constructor of object n of class IOobject

Any ideas?

alexeym April 8, 2015 09:07

Hi,

You error is NOT "the same" maybe it is similar. What is the type of

Code:

0.75*Cds*phase2->rho()*bp/d1
?


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