diff --git a/src/postProcessing/foamCalcFunctions/Make/files b/src/postProcessing/foamCalcFunctions/Make/files index 016ae77604..36dacdedb6 100644 --- a/src/postProcessing/foamCalcFunctions/Make/files +++ b/src/postProcessing/foamCalcFunctions/Make/files @@ -6,5 +6,6 @@ field/mag/mag.C field/magSqr/magSqr.C field/magGrad/magGrad.C field/div/div.C +field/randomise/randomise.C LIB = $(FOAM_LIBBIN)/libfoamCalcFunctions diff --git a/src/postProcessing/foamCalcFunctions/field/randomise/randomise.C b/src/postProcessing/foamCalcFunctions/field/randomise/randomise.C new file mode 100644 index 0000000000..be8befc080 --- /dev/null +++ b/src/postProcessing/foamCalcFunctions/field/randomise/randomise.C @@ -0,0 +1,164 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +#include "randomise.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + namespace calcTypes + { + defineTypeNameAndDebug(randomise, 0); + addToRunTimeSelectionTable(calcType, randomise, dictionary); + } +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::calcTypes::randomise::randomise() +: + calcType() +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::calcTypes::randomise::~randomise() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::calcTypes::randomise::init() +{ + argList::validArgs.append("randomise"); + argList::validArgs.append("perturbation"); + argList::validArgs.append("fieldName1 .. fieldNameN"); +} + + +void Foam::calcTypes::randomise::preCalc +( + const argList& args, + const Time& runTime, + const fvMesh& mesh +) +{ + if (args.additionalArgs().size() < 3) + { + Info<< nl + << "must specify perturbation magnitude and one" + << "or more fields" + << nl; + args.printUsage(); + FatalError.exit(); + } +} + + +void Foam::calcTypes::randomise::calc +( + const argList& args, + const Time& runTime, + const fvMesh& mesh +) +{ + const stringList& params = args.additionalArgs(); + const scalar pertMag = readScalar(IStringStream(params[1])()); + + Random rand(1234567); + + for (label fieldi=2; fieldi + ( + fieldHeader, + pertMag, + rand, + mesh, + processed + ); + writeRandomField + ( + fieldHeader, + pertMag, + rand, + mesh, + processed + ); + writeRandomField + ( + fieldHeader, + pertMag, + rand, + mesh, + processed + ); + writeRandomField + ( + fieldHeader, + pertMag, + rand, + mesh, + processed + ); + + if (!processed) + { + FatalError + << "Unable to process " << fieldName << nl + << "No call to randomise for fields of type " + << fieldHeader.headerClassName() << nl << nl + << exit(FatalError); + } + } + else + { + Info<< " No " << fieldName << endl; + } + } +} + + +// ************************************************************************* // diff --git a/src/postProcessing/foamCalcFunctions/field/randomise/randomise.H b/src/postProcessing/foamCalcFunctions/field/randomise/randomise.H new file mode 100644 index 0000000000..61a01dc9de --- /dev/null +++ b/src/postProcessing/foamCalcFunctions/field/randomise/randomise.H @@ -0,0 +1,141 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::calcTypes::randomise + +Description + Adds a random component to a field, with a given perturbation magnitude. + +SourceFiles + randomise.C + +\*---------------------------------------------------------------------------*/ + +#ifndef randomise_H +#define randomise_H + +#include "calcType.H" +#include "Random.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +namespace calcTypes +{ + +/*---------------------------------------------------------------------------*\ + Class randomise Declaration +\*---------------------------------------------------------------------------*/ + +class randomise +: + public calcType +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + randomise(const randomise&); + + //- Disallow default bitwise assignment + void operator=(const randomise&); + + +protected: + + // Member Functions + + // Calculation routines + + //- Initialise - typically setting static variables, + // e.g. command line arguments + virtual void init(); + + //- Pre-time loop calculations + virtual void preCalc + ( + const argList& args, + const Time& runTime, + const fvMesh& mesh + ); + + //- Time loop calculations + virtual void calc + ( + const argList& args, + const Time& runTime, + const fvMesh& mesh + ); + + + // I-O + + //- Write component fields + template + void writeRandomField + ( + const IOobject& header, + const scalar pertMag, + Random& rand, + const fvMesh& mesh, + bool& processed + ); + + +public: + + //- Runtime type information + TypeName("randomise"); + + + // Constructors + + //- Construct null + randomise(); + + + // Destructor + + virtual ~randomise(); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace calcTypes +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository +# include "writeRandomField.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/postProcessing/foamCalcFunctions/field/randomise/writeRandomField.C b/src/postProcessing/foamCalcFunctions/field/randomise/writeRandomField.C new file mode 100644 index 0000000000..b43af5e83a --- /dev/null +++ b/src/postProcessing/foamCalcFunctions/field/randomise/writeRandomField.C @@ -0,0 +1,73 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +template +void Foam::calcTypes::randomise::writeRandomField +( + const IOobject& header, + const scalar pertMag, + Random& rand, + const fvMesh& mesh, + bool& processed +) +{ + typedef GeometricField fieldType; + + if (header.headerClassName() == fieldType::typeName) + { + Info<< " Reading " << header.name() << endl; + fieldType field(header, mesh); + + forAll(field, cellI) + { + Type rndPert; + rand.randomise(rndPert); + rndPert = 2.0*rndPert - pTraits::one; + rndPert /= mag(rndPert); + field[cellI] += pertMag*rndPert; + } + + fieldType randomisedField + ( + IOobject + ( + header.name() + "Random", + mesh.time().timeName(), + mesh, + IOobject::NO_READ + ), + field + ); + + Info<< " Writing " << header.name() << "Random" << endl; + randomisedField.write(); + + processed = true; + } +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //