Files
openfoam/applications/test/dictionary/testDictEval3
Mark Olesen efd1ac4b5f 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.
2021-05-18 15:30:00 +02:00

76 lines
1.6 KiB
C++

/*--------------------------------*- 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;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
expr1 "hypot(3,4)";
expr2 hypot(6,8);
eval1 #eval
#{
(
${FOAM_API} >= 1812 && !${FOAM_API} > 2100
? $expr1
: $expr2
)
#};
eval2 #eval
#{
(false ? pi() * 2 * $expr1 : pi() * 2 * $expr2)
#};
eval3 #eval
#{
(bool(0.1) ? 10 : 0)
#};
eval4 #eval
#{
2 * mag(true) * pi()
#};
eval4b #eval
#{
2 * mag(!false) * pi()
#};
eval5 #eval
#{
2 * mag(${FOAM_API} >= 1909) * pi()
#};
// Can use round or floor for this:
apiYear #eval{ round($FOAM_API / 100) };
// Should not need round or floor:
apiMonth #eval{ ($FOAM_API % 100) };
// Could flag as an input error or treat as zero
empty #eval "";
// Field of specified length
random #eval 4 { vector(rand(), 0, 0) ; /* trailing rubbish */ ; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //