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/)
-   -   Problem writing to dictionary (https://www.cfd-online.com/Forums/openfoam-programming-development/169628-problem-writing-dictionary.html)

MrAndersson April 14, 2016 10:17

Problem writing to dictionary
 
Hello!

I am developing an OpenFOAM model for electrolytes and in this context one module predicts diffusivities and is supposed to write its results to a dictionary for others to read. Writing to dictionaries seems straight-forward enough, but for some reason I can't get it to work. This is how I define the IODictionary to be written to:

Code:

IOdictionary electrolyteProperties
(
    IOobject
    (
        "electrolyteProperties",
        runTime.constant(),
        mesh,
        IOobject::MUST_READ_IF_MODIFIED,
        IOobject::NO_WRITE
    )
);

I then define a subdictionary "DDict" inside "electrolyteDict":

Code:

dictionary& DDict = electrolyteProperties.subDict("D");
DDict contains dimensions and values of diffusivities for different species and may look something like this:

Code:

D
{
    dimensions    [ 0  2 -1  0  0  0  0];
    values
        (
            1e-12
            1e-12
            1e-12
        );
}

I try to write the entry by using set:

Code:

electrolyteProperties.set("values",DVals);
A printout of DVals gives:

Code:

DVals: 3(4.34118e-12 4.56812e-12 3.49564e-12)
which is what I want to write to the dictionary. The whole thing compiles without warning and runs without problems, the values just never get changed to the calculated ones. I have also tried to write directly to the parent dictionary (electrolyteProperties) to see whether the problem has to do with the usage of subDict, but that doesn't work either.

Anyone have an idea of what may cause my problem?

MrAndersson April 14, 2016 10:54

Actually, printing the dictionary (using Info) gives exactly what I want, but it never ends up in the file. Do I need to call some write function of some sort? Just writing DDict.write() or electrolyteProperties.write() does not work, then the compiler complains that function call is ambiguous.

alexeym April 14, 2016 15:57

Hi,

You can resolve write method ambiguity, for example, like this:

Code:

  IOdictionary electrolyteProperties
  (
      IOobject
      (
          "electrolyteProperties",
          runTime.constant(),
          mesh,
          IOobject::MUST_READ_IF_MODIFIED,
          IOobject::NO_WRITE
      )
  );

  dictionary& DDict = electrolyteProperties.subDict("D");

  scalarList DVals;
  ... fill DVals ...
  DDict.set("values", DVals);
  electrolyteProperties.regIOobject::write();


MrAndersson April 15, 2016 06:00

Thanks a lot Alexey! That solved my problem.

/Rasmus

klausb September 12, 2017 15:46

declaration issues with update code
 
Hello,

I run into compile errors with this code which I want to use to update some values in a dictionary:

code:

Code:

// IO test changing p-minIter value in fvSolutions file

// Declare dictionary
IOdictionary fvSolution(IOobject("fvSolution",runTime.system(),mesh,IOobject::NO_READ,IOobject::AUTO_WRITE));

// Declare subdictionary
dictionary mySubDictUpdate(fvSolution.subDict("p"));

// Update some values
mySubDictUpdate.set("minIter", 33 );
mySubDictUpdate.set("maxIter", 500 );

errors:

myPCG_solve.C:111:1: error: ‘IOdictionary’ was not declared in this scope
IOdictionary fvSolution(IOobject("fvSolution",runTime.system(), mesh,IOobject::NO_READ,IOobject::AUTO_WRITE));
^
myPCG_solve.C:114:1: error: ‘dictionary’ was not declared in this scope
dictionary mySubDictUpdate(fvSolution.subDict("p"));

myPCG_solve.C:117:1: error: ‘mySubDictUpdate’ was not declared in this scope
mySubDictUpdate.set("minIter", 33 );
^
myPCG_solve.C:122:1: error: ‘fvSolution’ was not declared in this scope
fvSolution.Foam::IOobject::write();
^
myPCG_solve.C:122:18: error: ‘Foam::IOobject’ has not been declared
fvSolution.Foam::IOobject::write();

How can I declare the object and dictionaries "in this scope"?

Klaus


All times are GMT -4. The time now is 03:02.