CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

codeStream how to read a dictionary keyword in another file

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 13, 2024, 12:53
Default codeStream how to read a dictionary keyword in another file
  #1
Member
 
Ali B.
Join Date: Mar 2020
Location: abzrg.github.io
Posts: 34
Rep Power: 6
reverseila is on a distinguished road
Hello foamers,

EDIT: replace "keyword" with "entry" in the title


I want to read a scalar from let's say constant/transportProperties.nu and store it into a keyword nu in 0/U.

One can achieve that by defining a third dictionary file that contains the keyword nu and its value, and this file is then #included in both transportProperties and 0/U, which is perfectly doable and valid.

However, I have problem doing that using codeStream:

Code:
/* file: 0/U */

NU #codeStream
{

    codeInclude
    #{
        #include "fvCFD.H"
    #};

    codeOptions
    #{
        -I$(LIB_SRC)/finiteVolume/lnInclude \
        -I$(LIB_SRC)/meshTools/lnInclude
    #};

    codeLibs
    #{
        -lmeshTools \
        -lfiniteVolume
    #};

    code
    #{
        const IOdictionary& d = static_cast<const IOdictionary&>(dict.parent().parent());

        dimensionedScalar nu
        (
           "nu",
           dimViscosity,
           d.db().lookupObject<IOdictionary>("transportProperties")
        );

        nu.writeEntry("", os);
        // or
        // os  << nu.value();
    #};
};


// ...

Re 44000;
L 0.06; // cm
U_inlet_x    #calc"$Re * $NU / $L";
But this gives me the following segfault:

Code:
/*---------------------------------------------------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  2206                                  |
|   \\  /    A nd           | Website:  www.openfoam.com                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
Build  : f1da147925-20220907 OPENFOAM=2206 patch=220907 version=v2206
Arch   : "LSB;label=64;scalar=64"
Exec   : simpleFoam
Date   : Mar 13 2024
Time   : 21:16:27
Host   : temple.local
PID    : 54182
I/O    : uncollated
Case   : /Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase
nProcs : 1
trapFpe: Floating point exception trapping enabled (FOAM_SIGFPE).
fileModificationChecking : Monitoring run-time modified files using timeStampMaster (fileModificationSkew 5, maxFileModificationPolls 20)
allowSystemOperations : Allowing user-supplied system call operations

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time

Create mesh for time = 0


SIMPLE: no convergence criteria found. Calculations will run for 2000 steps.

Reading field p

Reading field U

Using #codeStream at line 27 in file "0/U"
Using #codeStream with "/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib"
Could not load "/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib"
dlopen(/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib, 0x0009): tried: '/Users/ali/OpenFOAM/ali-v2206/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/OpenFOAM-v2206/site/2206/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/sys-openmpi/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ThirdParty-v2206/platforms/darwin64ClangDPInt64/lib/sys-openmpi/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ThirdParty-v2206/platforms/darwin64ClangDPInt64/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ThirdParty-v2206/platforms/darwin64Clang/fftw-3.3.10/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ThirdParty-v2206/platforms/darwin64Clang/CGAL-4.14.3/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ThirdParty-v2206/platforms/darwin64Clang/boost_1_74_0/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/opt/homebrew/Cellar/open-mpi/5.0.2/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/dummy/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file), '/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib' (no such file)
Creating new library in "dynamicCode/_2204d144ecda5b30bfa6a9010863d5ebdf133b0c/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib"
Invoking wmake libso /Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/_2204d144ecda5b30bfa6a9010863d5ebdf133b0c
wmake libso /Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/_2204d144ecda5b30bfa6a9010863d5ebdf133b0c
    ln: ./lnInclude
    dep: codeStreamTemplate.C
    Ctoo: codeStreamTemplate.C
/Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/0/U.#codeStream:52:27: warning: 'dimensioned' is deprecated: Since 2018-11 [-Wdeprecated-declarations]
   52 |         dimensionedScalar nu
      |                           ^
/Volumes/OpenFOAM/OpenFOAM-v2206/src/OpenFOAM/lnInclude/dimensionedType.H:399:9: note: 'dimensioned' has been explicitly marked deprecated here
  399 |         FOAM_DEPRECATED(2018-11)
      |         ^
/Volumes/OpenFOAM/OpenFOAM-v2206/src/OpenFOAM/lnInclude/stdFoam.H:45:35: note: expanded from macro 'FOAM_DEPRECATED'
   45 | # define FOAM_DEPRECATED(since) [[deprecated("Since " #since)]]
      |                                   ^
1 warning generated.
    link: /Volumes/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/_2204d144ecda5b30bfa6a9010863d5ebdf133b0c/../platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib
#0  Foam::error::printStack(Foam::Ostream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#1  Foam::sigSegv::sigHandler(int) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#2  _sigtramp in /usr/lib/system/libsystem_platform.dylib
#3  Foam::IOdictionary const& Foam::objectRegistry::lookupObject<Foam::IOdictionary>(Foam::word const&, bool) const in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libfiniteVolume.dylib
#4  Foam::IOdictionary const& Foam::objectRegistry::lookupObject<Foam::IOdictionary>(Foam::word const&, bool) const in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libfiniteVolume.dylib
#5  codeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c in dynamicCode/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_2204d144ecda5b30bfa6a9010863d5ebdf133b0c.dylib
#6  Foam::functionEntries::codeStream::evaluate(Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#7  Foam::functionEntries::codeStream::execute(Foam::dictionary const&, Foam::primitiveEntry&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#8  Foam::functionEntry::execute(Foam::word const&, Foam::dictionary const&, Foam::primitiveEntry&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#9  Foam::primitiveEntry::expandFunction(Foam::word const&, Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#10  Foam::primitiveEntry::acceptToken(Foam::token const&, Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#11  Foam::primitiveEntry::read(Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#12  Foam::primitiveEntry::readEntry(Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#13  Foam::primitiveEntry::primitiveEntry(Foam::keyType const&, Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#14  Foam::primitiveEntry::primitiveEntry(Foam::keyType const&, Foam::dictionary const&, Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#15  Foam::entry::New(Foam::dictionary&, Foam::Istream&, Foam::entry::inputMode, int) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#16  Foam::dictionary::read(Foam::Istream&, bool) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#17  Foam::dictionary::read(Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#18  Foam::operator>>(Foam::Istream&, Foam::dictionary&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#19  Foam::baseIOdictionary::readData(Foam::Istream&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#20  Foam::fileOperations::uncollatedFileOperation::read(Foam::regIOobject&, bool, Foam::IOstreamOption::streamFormat, Foam::word const&) const in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#21  Foam::regIOobject::readHeaderOk(Foam::IOstreamOption::streamFormat, Foam::word const&) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#22  Foam::localIOdictionary::localIOdictionary(Foam::IOobject const&, Foam::word const&, Foam::dictionary const*) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#23  Foam::localIOdictionary::localIOdictionary(Foam::IOobject const&, Foam::word const&, Foam::dictionary const*) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/lib/libOpenFOAM.dylib
#24  Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>::readFields() in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/bin/simpleFoam
#25  Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>::GeometricField(Foam::IOobject const&, Foam::fvMesh const&, bool) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/bin/simpleFoam
#26  Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>::GeometricField(Foam::IOobject const&, Foam::fvMesh const&, bool) in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/bin/simpleFoam
#27  main in /Volumes/OpenFOAM/OpenFOAM-v2206/platforms/darwin64ClangDPInt64Debug/bin/simpleFoam
#28  start in /usr/lib/dyld
[1]    54182 segmentation fault  simpleFoam
Am I missing something here? Not sure where lies the problem. I am trying to debug it, but it's not a trivial task, and need to spend more time on that.

This seems to be a useful feature to use (i.e. getting access to another dictionary from other files and storing it in the dictionary entry of the same type), but I couldn't find much information online about it.

Last edited by reverseila; March 14, 2024 at 12:03.
reverseila is offline   Reply With Quote

Old   March 14, 2024, 15:20
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
You are grabbing the IOdictionary from the objectRegistry lookup. I suspect that nobody has yet loaded though. For your particular case, it would probably make more sense to use a regular dictionary #include and have it expand out (automatically) before the codeStream gets thrown at the compiler.
Can't remember offhand if codeStream also supports the dictionary context (codeContext keyword?) for handling this type of thing, but take a look for something like that.
olesen is offline   Reply With Quote

Old   March 15, 2024, 18:52
Default
  #3
Member
 
Ali B.
Join Date: Mar 2020
Location: abzrg.github.io
Posts: 34
Rep Power: 6
reverseila is on a distinguished road
Thank you Mark for your reply.

Quote:
Originally Posted by olesen View Post
You are grabbing the IOdictionary from the objectRegistry lookup. I suspect that nobody has yet loaded though.
Yes, apparently they are not loaded yet, and if I understand it correctly, according to the order of the code (simpleFoam), in the createFields.H, we first construct the U and then later on proceed to create transportProperties.

Quote:
For your particular case, it would probably make more sense to use a regular dictionary #include and have it expand out (automatically) before the codeStream gets thrown at the compiler.
I used #include as you suggested, and as some of the OpenFOAM tutorial cases too have used it (like some files in the 0/include/ folder), and it worked perfectly.

Quote:
Can't remember offhand if codeStream also supports the dictionary context (codeContext keyword?) for handling this type of thing, but take a look for something like that.
I've also checked whether the codeContext works in codeStream, and it seems that it does not have such keyword. Note that I don't know how to access "an object" that has a member named codeContext, using the dict parameter alone. In the example provided in the git commit b59ae32d68a..., the codeContext was accessed via this, but this does not exist in the non-static member function. Also, there is no codeStream in the output of git grep --cached codeContext in the $FOAM_SRC directory (OpenFOAM-v2206).

Code:
NU #codeStream
{
    code
    #{
        const dictionary& d = dict.codeContext();
        os << d.getOrDefault<scalar>("myNu", 1e-5);
    #};

    codeContext
    #{
            myNu 1e-5;
    #}

    codeInclude
    #{
    #};

    codeOptions
    #{
    #};

    codeLibs
    #{
    #};
};
Code:
wmake libso /Users/ali/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/dynamicCode/_c720e454df5e4847c8cd4763a2af1e41ff1a4df0
    ln: ./lnInclude
    dep: codeStreamTemplate.C
    Ctoo: codeStreamTemplate.C
/Users/ali/OpenFOAM/ali-v2206/run/incompressible/simpleFoam/backwardFacingStep_baseCase/0/U.#codeStream:45:36: error: no member named 'codeContext' in 'Foam::dictionary'
   45 |         const dictionary& d = dict.codeContext();
      |                               ~~~~ ^
1 error generated.
make: *** [/Volumes/OpenFOAM/OpenFOAM-v2206/wmake/rules/General/transform:35: Make/darwin64ClangDPInt64Debug/codeStreamTemplate.o] Error 1


--> FOAM FATAL IO ERROR: (openfoam-2206 patch=220907)
Failed wmake "dynamicCode/_c720e454df5e4847c8cd4763a2af1e41ff1a4df0/platforms/darwin64ClangDPInt64Debug/lib/libcodeStream_c720e454df5e4847c8cd4763a2af1e41ff1a4df0.dylib"


file: 0/U at line 17.

    From static Foam::functionEntries::codeStream::streamingFunctionType Foam::functionEntries::codeStream::getFunction(const Foam::dictionary &, const Foam::dictionary &)
    in file db/dictionary/functionEntries/codeStream/codeStream.C at line 192.

FOAM exiting
reverseila is offline   Reply With Quote

Old   March 16, 2024, 06:45
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
The "codeContext" belongs to the dynamic code interface and possibly holds reference to a dictionary. A dictionary does not have a codeContext method (not even sure what that would supposed to mean).
You'll have to work at it a bit more...

Another idea that looks a bit messier but is actually probably more elegant: in your code section, declare the dimensionedScalar nu as 'static', default initialized. You can then subsequently test it, if it has an empty name it is not properly populated with a value. In this case, use the db().getObjectPtr to lookup the transportProperties IOdictionary. If that returns a non-nullptr, you can use it directly, otherwise you will need to manually load and read the IOdictionary yourself. The IOdictionary::readContents method (not sure when that was added) makes this convenient. From either of these dictionary sources you then finalise the value of nu.
Since your nu is marked as static, the content will persist and this entire lookup and/or load will only occur once.
olesen is offline   Reply With Quote

Reply

Tags
codestream


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SparceImage v1.7.x Issue on MAC OS X rcarmi OpenFOAM Installation 4 August 14, 2014 06:42
Trouble compiling utilities using source-built OpenFOAM Artur OpenFOAM Programming & Development 14 October 29, 2013 10:59
[OpenFOAM] Annoying issue of automatic "Rescale to Data Range " with paraFoam/paraview 3.12 keepfit ParaView 60 September 18, 2013 03:23
[swak4Foam] Error bulding swak4Foam sfigato OpenFOAM Community Contributions 18 August 22, 2013 12:41
friction forces icoFoam ofslcm OpenFOAM 3 April 7, 2012 10:57


All times are GMT -4. The time now is 20:04.