CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

OF9, Error after #codeStream internalField

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By LuckyTran

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 2, 2021, 17:44
Default OF9, Error after #codeStream internalField
  #1
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
Hi all,



I am getting an error here and I believe the relevant output is here:





(START ERROR)

codeStream : dictionary:"/home/user/OpenFOAM/user-9/mysimulation/0/region1/T" master-only-reading:0


--> FOAM FATAL IO ERROR:
wrong token type - expected Scalar, found on line 2 the word 'ٌ@33333ٌ@�\�s�x�@33333ٌ@��:.�@33333ٌ@33333ٌ@�lh�@ 33333ٌ@33333ٌ@33333ٌ@33333ٌ@33333ٌ@33333ٌ@33333ٌ@3 3333ٌ@33333ٌ@33333ٌ@�'

file: IStringStream.sourceFile at line 2.

From function Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::doubleScalar&)
in file lnInclude/Scalar.C at line 101.

FOAM exiting


(END ERROR)



I am just trying out codeStream for the first time and below is my code. I am concerned it might be the wrong writeEntry command, but if anyone knows for sure let me know please. Below is my codeStream.



Thanks!



Code:
dimensions      [ 0 0 0 1 0 0 0 ];

internalField  #codeStream
{
        codeInclude
        #{
            #include "fvCFD.H"
                #include <math.h>  
        #};
        
        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/meshTools/lnInclude
        #};
        
        codeLibs
        #{
            -lmeshTools \
            -lfiniteVolume
        #};
        
        code
        #{
            const IOdictionary& d = static_cast<const IOdictionary&>(dict);
            const fvMesh& mesh = refCast<const fvMesh>(d.db());
            scalarField T(mesh.nCells());
            
            const scalar maxt = 90;
            const scalar core_r = 0.762; 
            const scalar avg_t = 40;
            const scalar a = (maxt - avg_t)/pow(core_r, 2);
            double r;
            
            forAll(T, i)
            {
                // Get Cell positions
                const scalar x = mesh.C()[i][0]; 
                const scalar y = mesh.C()[i][1];
                //const scalar z = mesh.C()[i][2];
                r = sqrt(pow(x, 2) + pow(y,2));
                if (r < core_r)
                {
                    T[i] = maxt - a*pow(r, 2);
                } else {
                    T[i] = avg_t;
                }
            }
            writeEntry(os, "", T);  // Write to input dictionary
        #};
};
Dicanic is offline   Reply With Quote

Old   September 2, 2021, 21:53
Default Really strange Error, on openFoam 8 too.
  #2
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I found that I can only set T[i] with hardcode? I am so confused right now.
If I set T[i] = 75; for example, it works.

If I set T[i] = Th; where Th is ANY variable type and even the same value (75), it will break and give me a new error

--> FOAM FATAL IO ERROR:
wrong token type - expected Scalar, found on line 2 the word '��E����@�\��\��@
��З*�@�9L��@f��Gs��@�'

Really this makes codeStream useless to me, why would it be doing this?
Dicanic is offline   Reply With Quote

Old   September 2, 2021, 23:10
Default
  #3
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I am thinking this may be a memory overflow error of some sort? I have around 2 million cells. If I even add a if statement it won't work, but if I set it to a stagnant value... it does.



This one is a real head scratcher.
Dicanic is offline   Reply With Quote

Old   September 6, 2021, 19:51
Default Any help appreciated, am stuck
  #4
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I have tried every iteration of the #codeStream and read all the relevant source code and combed the internet. I absolutely cannot initialize a temperature field and neither have I seen it done successfully with #codeStream.

It should be as easy as implementing these:

https://www.cfd-china.com/assets/upl...1-solution.pdf

The above examples work, but the temperature will not work in any circumstances I try. This is a very small blockmesh with 1000 cells, so it is not a memory error.

Code:
internalField #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);
		const fvMesh& mesh = refCast<const fvMesh>(d.db());
		scalarField T(mesh.nCells(), 0.);
		
		double maxt = 90;
		double avg_t = 50;
		const scalar block = 0.05;
			
		forAll(T, i)
		{
			// Get Cell positions
			const scalar x = mesh.C()[i][0]; 
			const scalar y = mesh.C()[i][1];

			//const scalar z = mesh.C()[i][2];
			///const scalar r = pow(pow(x, 2.0) + pow(y, 2.0),0.5);
			if (x+y >= block)
			{
				T[i] = maxt;
			} else {
				T[i] = avg_t;
			}
		}
		writeEntry(os, "", T);  // Write to input dictionary
		//Pout << "Initialization of internalField"; 
	#};
};

This gives error:
Code:
on processor 0 have masterSize:826176 and localSize:826176

on processor 0 after waiting: have masterSize:826176 and localSize:826176
Opening cached dictionary:"/home/user/OpenFOAM/user-9/2DTestGround/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libcodeStream_5bcb7371585e69419bfd74cf784f21d4784acc5e.so"
codeStream : dictionary:"/home/user/OpenFOAM/user-9/2DTestGround/0/T" master-only-reading:0


--> FOAM FATAL IO ERROR: 
error in IOstream "IStringStream.sourceFile" for operation operator>>(Istream&, List<T>&) : reading entry

file: IStringStream.sourceFile at line 2.

    From function void Foam::IOstream::fatalCheck(const char*) const
    in file db/IOstreams/IOstreams/IOstream.C at line 109.

FOAM exiting
Dicanic is offline   Reply With Quote

Old   September 7, 2021, 03:52
Default
  #5
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,679
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
well... for starters... anything with the const type qualifier is read only
LuckyTran is offline   Reply With Quote

Old   September 7, 2021, 09:53
Default const type
  #6
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I can see how this is relevant when I add x and y together, but I get the same problem when using x alone in the boolean operator.


These snippets of code:



Code:
        const IOdictionary& d = static_cast<const IOdictionary&>(dict);
        const fvMesh& mesh = refCast<const fvMesh>(d.db());
        scalarField T(mesh.nCells(), 0.);

as well as


Code:
            const scalar x = mesh.C()[i][0]; 
            const scalar y = mesh.C()[i][1];

work in all the above tutorials but the code also only reads them so declaring them as const is appropriate as far as I can tell. The only thing I am writing to is scalarField T. For example, I could take my exact code and only change T to alpha, and it would run and I would see the results as intended. I must be referencing T incorrectly but I am not sure how.
Dicanic is offline   Reply With Quote

Old   September 7, 2021, 11:42
Default Well at least the error is consistent across versions
  #7
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
With new code (using as few consts as possible)

Code:
internalField #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);
		const fvMesh& mesh = refCast<const fvMesh>(d.db());
		scalarField T(mesh.nCells(), 0.);
		
		scalar maxt = 90.0;
		scalar avg_t = 40.0;
		scalar block = 0.025;
			
		forAll(T, i)
		{
			// Get Cell positions
			scalar x = mesh.C()[i][0];
			scalar y = mesh.C()[i][1];

			if (x >= block)
			{
				T[i] = scalar(maxt);
			} else {
				T[i] = scalar(avg_t);
			}
		}
		T.writeEntry("", os);  // Write to input dictionary
	#};
};

Code:
--> FOAM FATAL IO ERROR: (openfoam-2106)
Bad token - could not get scalar value
file: input at line 2.
    From Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::doubleScalar&)
    in file lnInclude/Scalar.C at line 141.
FOAM exiting
OpenFOAM-2106:~/OpenFOAM/user-2106/run/2DTestGround/
Dicanic is offline   Reply With Quote

Old   September 7, 2021, 12:41
Default
  #8
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,679
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
you have a token error... Check all your { } ( ) pairs and ;'s

I would start with removing the semicolon at the end of your bracket for internalField
Dicanic likes this.
LuckyTran is offline   Reply With Quote

Old   September 7, 2021, 17:56
Default
  #9
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I couldn't figure it out even with trying every permutation of semicolon and brackets. I instead just used the setExprFields utility on the v2106 of OpenFOAM, and then ran it on OF 9 (as v2106 isn't very friendly with solid only chtMultiRegionFoam). If anyone figures out how to get codeStream to work with temperature, let me know. I am just flabbergasted at this error and it might be something simple that I did wrong.

Thanks for the help thus far!
Dicanic is offline   Reply With Quote

Old   September 8, 2021, 05:03
Default
  #10
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,679
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
A token error is really simple indeed
LuckyTran is offline   Reply With Quote

Old   September 8, 2021, 07:29
Default
  #11
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
I don't believe the error message is suggesting the token is actually in my dictionary and I have tried just in case. It compiles the dictionary code just fine and then makes a bad stream that gives a token error. The function it points to is not much help.

This also wouldn't necessarily make sense since this code snippet works verbatim with other scalar and vector fields as well. Thus, it might be that I am giving T the wrong datatype (I"ve tried everything here) or I am referencing T wrong.
Dicanic is offline   Reply With Quote

Old   September 8, 2021, 11:38
Default
  #12
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,679
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
This
Code:
internalField
{

};
And this
Code:
internalField
{

}
Are not equivalent


It's also only one block of the dict so you may have made the same mistake elsewhere. The good news is that the parser tells you the token error is on this block (line 2).
LuckyTran is offline   Reply With Quote

Old   September 8, 2021, 12:04
Default
  #13
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
Yes, but per my previous replies. It does not change the end result.

Code:
--> FOAM FATAL IO ERROR: 
wrong token type - expected Scalar, found on line 2 the word '�Sg��V@㛿�!V@w��c?V@lL1�Q�V@ŴJYV@���TV@��>�E'

file: IStringStream.sourceFile at line 2.

    From function Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::doubleScalar&)
    in file lnInclude/Scalar.C at line 101.

FOAM exiting
Seeing I am pretty sure I didn't type �Sg��V@㛿�!V@w��c?V@lL1�Q�V@ŴJYV@���TV@��>�E on line 2, I assume it is a bad stream.

Here is my entire T dictionary

Code:
/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  8
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      binary;
    class       volScalarField;
    location    "0";
    object      T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [ 0 0 0 1 0 0 0 ];

internalField  #codeStream
{
        codeInclude
        #{
            #include "fvCFD.H"
            #include <math.h>  
        #};
        
        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/meshTools/lnInclude
        #};
        
        codeLibs
        #{
            -lmeshTools \
            -lfiniteVolume
        #};
        
        code
        #{
            const IOdictionary& d = static_cast<const IOdictionary&>(dict);
            const fvMesh& mesh = refCast<const fvMesh>(d.db());
            scalarField T(mesh.nCells());
            
            const scalar maxt = 90;
            const scalar core_r = 0.762; 
            const scalar avg_t = 40;
            const scalar a = (maxt - avg_t)/pow(core_r, 2);
            double r;
            
            forAll(T, i)
            {
                // Get Cell positions
                const scalar x = mesh.C()[i][0]; 
                const scalar y = mesh.C()[i][1];
                //const scalar z = mesh.C()[i][2];
                r = sqrt(pow(x, 2) + pow(y,2));
                if (r < core_r)
                {
                    T[i] = maxt - a*pow(r, 2);
                } else {
                    T[i] = avg_t;
                }
            }
            writeEntry(os, "", T);  // Write to input dictionary
        #};
}

boundaryField
{
    inlet
    {
        type            fixedValue;
        value		 uniform 40;
    }
    outlet
    {
        type            inletOutlet;
        inletValue	 uniform 40;
        value		 uniform 40;        
    }
    walls
    {
        type            zeroGradient;
    }	
}


// ************************************************************************* //
Go ahead and try it, maybe something in my dependencies is incorrectly installed if it should work on a simple blockmesh case on your side. I am still hoping I have a glaring error due to my tunnel vision because I would very much love to actually use a codestream versus the setExprFields utility. Nonetheless, I have made codestream work with both U (as a vector) and alpha as a scalar and in the simple translation from alpha to T, it still produces this error.
Dicanic is offline   Reply With Quote

Old   September 8, 2021, 16:58
Default
  #14
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,679
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
Your codeStream block works for me after I spent the time to manually retype it into a random case of mine in OF 2.4. You have a typo in your dict somewhere. It's that simple. Or maybe you have something extraordinary going on in your custom solver. Then I can't tell you where in the universe the token error might appear.

T.writeEntry("", os); is slightly safer to use if you continue to have trouble

I'm done
LuckyTran is offline   Reply With Quote

Old   September 8, 2021, 17:01
Default
  #15
New Member
 
George
Join Date: Oct 2020
Posts: 24
Rep Power: 5
Dicanic is on a distinguished road
Could you post the manually retyped dict? I've looked at mine for hours and don't think I am going to find it.

T.writeEntry("", os) is for the ESI version which I have posted a dict also attempting to get it to work with that.
Dicanic is offline   Reply With Quote

Old   June 8, 2022, 02:46
Default
  #16
New Member
 
mill
Join Date: Feb 2021
Posts: 9
Rep Power: 5
mill is on a distinguished road
Hi Dicanic
I meet the similar problem when i use codeStream to set alpha of water. It shows a fatal error:
Code:
--> FOAM FATAL IO ERROR
expected keyword 'uniform' or 'nonuniform' , found codeInclude
The version of OPenFoam is V9.
mill is offline   Reply With Quote

Old   August 22, 2023, 13:37
Default
  #17
New Member
 
Join Date: Jul 2021
Posts: 12
Rep Power: 4
Legacy is on a distinguished road
I was having this issue and I changed the write format from binary to ascii and it fixed it.
Legacy is offline   Reply With Quote

Reply

Tags
codestream, openfoam 9


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
[OpenFOAM] How to correctly show the result of #codeStream# internalField? chengdi ParaView 24 July 14, 2022 04:26
nonUniform internalField using codeStream Saleh Abuhanieh OpenFOAM Programming & Development 9 September 17, 2021 07:48
PROBLEMS DEFINING A LIST USING #CODESTREAM !! Help please !! angatri_14 OpenFOAM Programming & Development 1 August 21, 2020 17:16
twoPhaseEulerFoam confined plunging jet simulation failed ves OpenFOAM Running, Solving & CFD 2 June 17, 2020 05:08
How to create the internalField with #codeStream chun OpenFOAM Running, Solving & CFD 3 May 6, 2017 10:35


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