Commit f84ebb9a authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: driver/parser/scanner for plain field expressions

parent 17f5560c
......@@ -158,6 +158,13 @@ $(expr)/exprDriver/exprDriver.C
$(expr)/exprDriver/exprDriverFields.C
$(expr)/exprDriver/exprDriverIO.C
fieldExpr = $(expr)/fields
$(fieldExpr)/fieldExpr.C
$(fieldExpr)/fieldExprDriver.C
$(fieldExpr)/fieldExprDriverFields.C
$(fieldExpr)/fieldExprLemonParser.lyy-m4
$(fieldExpr)/fieldExprScanner.cc
ops = primitives/ops
$(ops)/flipOp.C
......
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory
# Manually create ragel scanner and lemon parser header
prefix=fieldExpr
"${WM_PROJECT_DIR:?}/wmake/scripts/makeParser" \
-prefix="$prefix" \
-scanner=Scanner.rl \
-parser=LemonParser.lyy-m4 \
"$@"
#------------------------------------------------------------------------------
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
\*---------------------------------------------------------------------------*/
#include "fieldExprFwd.H"
#include "defineDebugSwitch.H"
// * * * * * * * * * * * * * * * * Globals * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
defineDebugSwitchWithName(fieldExpr, "fieldExpr", 0);
registerDebugSwitchWithName(fieldExpr, fieldExpr, "fieldExpr");
} // End namespace expressions
} // End namespace Foam
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
\*---------------------------------------------------------------------------*/
#include "fieldExprDriver.H"
#include "fieldExprScanner.H"
#include "error.H"
#include "className.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
namespace fieldExpr
{
defineTypeNameAndDebug(parseDriver, 0);
} // End namespace fieldExpr
} // End namespace expressions
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::expressions::fieldExpr::parseDriver::parseDriver()
:
parseDriver(1)
{}
Foam::expressions::fieldExpr::parseDriver::parseDriver
(
const label len
)
:
parsing::genericRagelLemonDriver(),
expressions::exprDriver(),
size_(Foam::max(1, len))
{}
Foam::expressions::fieldExpr::parseDriver::parseDriver
(
const label len,
const dictionary& dict
)
:
parsing::genericRagelLemonDriver(),
expressions::exprDriver(dict),
size_(len)
{}
Foam::expressions::fieldExpr::parseDriver::parseDriver
(
const label len,
const parseDriver& driver_
)
:
parsing::genericRagelLemonDriver(),
expressions::exprDriver(driver_),
size_(len)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
unsigned Foam::expressions::fieldExpr::parseDriver::parse
(
const std::string& expr,
size_t pos,
size_t len
)
{
scanner scan(this->debugScanner());
scan.process(expr, pos, len, *this);
return 0;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
Class
Foam::expressions::fieldExpr::parseDriver
Description
Driver for generic primitive field expressions
In addition to the standard mathematical functions, operations and
logical and relational operations, the volume expression support the
following driver-specific functions:
Functions
\table
Function | Description | Number of arguments |
rand | Random field | 0/1 |
\endtable
SourceFiles
fieldExprDriver.C
fieldExprDriverFields.C
fieldExprDriverTemplates.C
\*---------------------------------------------------------------------------*/
#ifndef expressions_fieldExprDriver_H
#define expressions_fieldExprDriver_H
#include "fieldExprFwd.H"
#include "exprDriver.H"
#include "primitiveFields.H"
#include "genericRagelLemonDriver.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
namespace fieldExpr
{
/*---------------------------------------------------------------------------*\
Class parseDriver Declaration
\*---------------------------------------------------------------------------*/
class parseDriver
:
public parsing::genericRagelLemonDriver,
public expressions::exprDriver
{
protected:
// Protected Data
//- The field size
label size_;
// Protected Member Functions
// No copy copy construct
parseDriver(const parseDriver&) = delete;
// No copy assignment
void operator=(const parseDriver&) = delete;
public:
ClassName("fieldExpr::driver");
// Constructors
//- Construct null - uses size = 1
parseDriver();
//- Construct with specified size
explicit parseDriver(const label len);
//- Construct for specified size with given dictionary
parseDriver(const label len, const dictionary& dict);
//- Construct for specified size with copy of driver context
parseDriver(const label len, const parseDriver& driver_);
//- Destructor
virtual ~parseDriver() = default;
// Public Member Functions
//- The underlying field size for the expression
virtual label size() const
{
return size_;
}
//- The underlying point field size for the expression
virtual label pointSize() const
{
return size_;
}
// Evaluation
//- Perform parsing on (sub) string
using genericRagelLemonDriver::content;
//- Execute the parser
virtual unsigned parse
(
const std::string& expr,
size_t pos = 0,
size_t len = std::string::npos
);
// Fields
//- Set result
template<class Type>
void setResult(Field<Type>* ptr, bool pointVal = false)
{
result().setResult<Type>(ptr, pointVal);
}
// New Fields
//- Return named field (variable) if available
template<class Type>
tmp<Field<Type>>
getField(const word& fieldName) const;
// Custom Field Functions
//- A uniform random field
tmp<scalarField> field_rand(label seed=0, bool gaussian=false) const;
//- A Gaussian random field
tmp<scalarField> field_randGaussian(label seed=0) const
{
return field_rand(seed, true);
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fieldExpr
} // End namespace expressions
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "fieldExprDriverTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
\*---------------------------------------------------------------------------*/
#include "fieldExprDriver.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::tmp<Foam::scalarField>
Foam::expressions::fieldExpr::parseDriver::field_rand
(
label seed,
bool gaussian
) const
{
auto tresult = tmp<scalarField>::New(this->size());
fill_random(tresult.ref(), seed, gaussian);
return tresult;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Foam::tmp<Foam::Field<Type>>
Foam::expressions::fieldExpr::parseDriver::getField
(
const word& name
) const
{
bool isPointVal = false;
bool isUniformVal = false;
tmp<Field<Type>> tfield;
if (hasVariable(name) && variable(name).isType<Type>())
{
const expressions::exprResult& var = variable(name);
isPointVal = var.isPointValue();
isUniformVal = var.isUniform();
tfield = var.cref<Type>().clone();
}
if (tfield.valid())
{
const label fldLen = tfield().size();
const label len = (isPointVal ? this->pointSize() : this->size());
if (returnReduce((fldLen == len), andOp<bool>()))
{
return tfield;
}
if (!isUniformVal)
{
WarningInFunction
<< "Variable " << name
<< " does not fit the size and is not a uniform value." << nl
<< "Using average value" << endl;
}
return tmp<Field<Type>>::New(this->size(), gAverage(tfield));
}
return tfield;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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/>.
Namespace
Foam::expressions::fieldExpr
Description
Namespace for primitive field expressions parsing and evaluation
\*---------------------------------------------------------------------------*/
#ifndef expressions_fieldExprFwd_H
#define expressions_fieldExprFwd_H
namespace Foam
{
namespace expressions
{
namespace fieldExpr
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Forward Declarations
class parser;
class scanner;
class parseDriver;
union scanToken;
//- Static debugging option
extern int debug;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fieldExpr
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Typedef for fieldExpr parseDriver
typedef fieldExpr::parseDriver fieldExprDriver;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expressions
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
#define TOK_QUESTION 1
#define TOK_COLON 2
#define TOK_LOR 3
#define TOK_LAND 4
#define TOK_BIT_XOR 5
#define TOK_BIT_AND 6
#define TOK_EQUAL 7
#define TOK_NOT_EQUAL 8
#define TOK_LESS_EQ 9
#define TOK_GREATER_EQ 10
#define TOK_LESS 11
#define TOK_GREATER 12
#define TOK_PLUS 13
#define TOK_MINUS 14
#define TOK_TIMES 15
#define TOK_DIVIDE 16
#define TOK_PERCENT 17
#define TOK_NEGATE 18
#define TOK_NOT 19
#define TOK_DOT 20
#define TOK_NUMBER 21
#define TOK_ZERO 22
#define TOK_PI 23
#define TOK_LPAREN 24
#define TOK_RPAREN 25
#define TOK_DEG_TO_RAD 26
#define TOK_RAD_TO_DEG 27
#define TOK_SCALAR_ID 28
#define TOK_MIN 29
#define TOK_COMMA 30
#define TOK_MAX 31
#define TOK_SUM 32
#define TOK_AVERAGE 33
#define TOK_EXP 34
#define TOK_LOG 35
#define TOK_LOG10 36
#define TOK_SQR 37