ENH: adjust token in preparation for separate expression tokenization

- minor simplification of #if/#endif handling

ENH: improve input robustness with negative-prefixed expansions (#2095)

- especially in blockMeshDict it is useful to negate an value directly.
  Eg,
  ```
     xmax  100;
     xmin  -$xmax;
  ```
  However, this fails since the dictionary expansion is a two-step
  process of tokenization followed by expansion. After the expansion
  the given input would now be the following:
  ```
     xmax  100;
     xmin  - 100;
  ```
  and retrieving a scalar value for 'xmin' fails.

  Counteract this by being more generous on tokenized input when
  attempting to retrieve a label or scalar value.
  If a '-' is found where a number is expected, use it to negate the
  subsequent value.

  The previous solution was to invoke an 'eval':
  ```
     xmax  100;
     xmin  #eval{-$xmax};
  ```
  which adds additional clutter.
This commit is contained in:
Mark Olesen
2021-05-17 16:50:15 +02:00
parent 739c1c1d61
commit efd1ac4b5f
28 changed files with 507 additions and 166 deletions

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDictCalc1;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDictEval1;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDictEval1;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDictEval1;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,121 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v2012 |
| \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Test expansion with negative signs
value 0.5;
radius 3;
negValue -$value;
select1 10;
sqrt05 #eval{ sqrt(0.5) };
vector ( -10 ${{-$sqrt05}} $value );
corner ( ${{ -$radius*sqrt(0.5) }} 1 0 );
corner2 ${{
vector(-${radius}*sqrt(0.5), ${radius}*sqrt(0.5), 2)
}};
// Just a future idea (2021-05-14) - does not yet work!
#if 0
corner3 #eval #{
variables ( "outer = $radius*sqrt(0.5)" );
vector(-outer, outer, 2)
#};
#endif
// The brace-bracket #eval with multi-lines failed for v2012 and earlier
corner2b #eval
{
vector(-${radius}*sqrt(0.5), $radius*sqrt(0.5), 2)
};
corner2c #eval
${{
vector(-${radius}*sqrt(0.5), $radius*sqrt(0.5), 2)
}};
longSlurp #eval
{
// This is actually a very simple expression
1 + 2
// With a long comment that is stripped out
// during reading anyhow.
};
longVariable
${{
// This is actually a very simple expression in variable syntax
1 + 2
/*
// With a long comment that is stripped out
// during reading anyhow.
*/
}};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Geometric parameters
rxo 2;
ryo 3;
rzo 4;
// Geometric parameters
outerRadius 1;
innerRatio 0.75;
geometry
{
sphere
{
type sphere;
origin (0 0 0);
radius ($rxo $ryo $rzo);
}
innerSphere
{
$sphere
// Different solutions to the same problem
radius_v1
(
${{ $innerRatio*$rxo }}
${{ $innerRatio*$ryo }}
${{ $innerRatio*$rzo }}
);
radius_v2 #eval{ $innerRatio*vector($rxo, $ryo, $rzo) };
radius_v3 ${{ $innerRatio*$[(vector) ../sphere/radius] }};
// Inplace overwrite the same value
radius ${{ $innerRatio*$[(vector) radius] }};
}
}
// ************************************************************************* //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Test some parsing

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDictRegex;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#inputMode merge

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0;
format ascii;
class dictionary;
object testDict;
object dictionary;
note "test with foamDictionary -expand";
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,3 @@
Test-dictionary3.C
EXE = $(FOAM_USER_APPBIN)/Test-dictionary3

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,78 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-dictionary3
Description
Test expressions and re-expansions
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "dictionary.H"
#include "vector.H"
#include "StringStream.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
{
IStringStream is
(
"value 10;"
"scalar1 $value;"
"scalar2 -$value;"
// Use #eval expansion entirely
"vector1 ${{vector($value, -$value, $value)}};"
"vector2 ($value -$value $value);"
);
dictionary dict(is);
Info<< "input dictionary:" << dict << nl;
Info<< "value: " << dict.get<scalar>("value") << nl;
Info<< "scalar1: " << dict.get<scalar>("scalar1") << nl;
Info<< "scalar2: " << dict.get<scalar>("scalar2") << nl;
Info<< "vector1: " << dict.get<vector>("vector1") << nl;
Info<< "vector2: " << dict.get<vector>("vector2") << nl;
}
return 0;
}
// ************************************************************************* //