diff --git a/applications/test/gravityMeshObject/Make/files b/applications/test/gravityMeshObject/Make/files new file mode 100644 index 0000000000..a002388ea1 --- /dev/null +++ b/applications/test/gravityMeshObject/Make/files @@ -0,0 +1,3 @@ +Test-gravityMeshObject.C + +EXE = $(FOAM_USER_APPBIN)/Test-gravityMeshObject diff --git a/applications/test/gravityMeshObject/Make/options b/applications/test/gravityMeshObject/Make/options new file mode 100644 index 0000000000..fa15f12452 --- /dev/null +++ b/applications/test/gravityMeshObject/Make/options @@ -0,0 +1,5 @@ +EXE_INC = \ + -I$(LIB_SRC)/finiteVolume/lnInclude + +EXE_LIBS = \ + -lfiniteVolume diff --git a/applications/test/gravityMeshObject/Test-gravityMeshObject.C b/applications/test/gravityMeshObject/Test-gravityMeshObject.C new file mode 100644 index 0000000000..bee9e70120 --- /dev/null +++ b/applications/test/gravityMeshObject/Test-gravityMeshObject.C @@ -0,0 +1,88 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2023 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 . + +Description + Test loading of different gravity items + +\*---------------------------------------------------------------------------*/ + +#include "MeshObject.H" +#include "gravityMeshObject.H" +#include "IOobjectList.H" +#include "IOstreams.H" +#include "argList.H" +#include "Time.H" + +using namespace Foam; + +void printInfo(const meshObjects::gravity& g) +{ + Pout<< "name:" << g.uniformDimensionedVectorField::name() + << " type:" << g.type() + << " value:" << g.value() << nl; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + #include "setRootCase.H" + #include "createTime.H" + + IOobjectList objects(runTime, runTime.constant()); + + Info<< "Found: " << objects << nl << endl; + + for (const IOobject& io : objects.sorted()) + { + if (io.name() == meshObjects::gravity::typeName) + { + const auto& g = meshObjects::gravity::New(runTime); + printInfo(g); + } + else + { + const auto& g = meshObjects::gravity::New(io.name(), runTime); + printInfo(g); + } + + Pout<< "registered:" << flatOutput(runTime.sortedToc()) << nl << endl; + } + + meshObjects::gravity::Delete("g", runTime); + meshObjects::gravity::Delete("something-not-in-registry", runTime); + + Info<< "after Delete" << nl; + Pout<< "registered:" << flatOutput(runTime.sortedToc()) << endl; + + Info<< "\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/gravityMeshObject/case1/constant/banana b/applications/test/gravityMeshObject/case1/constant/banana new file mode 100644 index 0000000000..d0fdecff6d --- /dev/null +++ b/applications/test/gravityMeshObject/case1/constant/banana @@ -0,0 +1,21 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2212 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class uniformDimensionedVectorField; + location "constant"; + object banana; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [0 1 -2 0 0 0 0]; +value (0 0 -10); + +// ************************************************************************* // diff --git a/applications/test/gravityMeshObject/case1/constant/g b/applications/test/gravityMeshObject/case1/constant/g new file mode 100644 index 0000000000..eabbfe9bf2 --- /dev/null +++ b/applications/test/gravityMeshObject/case1/constant/g @@ -0,0 +1,21 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2212 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class uniformDimensionedVectorField; + location "constant"; + object g; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [0 1 -2 0 0 0 0]; +value (0 -9.81 0); + +// ************************************************************************* // diff --git a/applications/test/gravityMeshObject/case1/constant/saturn b/applications/test/gravityMeshObject/case1/constant/saturn new file mode 100644 index 0000000000..ab70026f42 --- /dev/null +++ b/applications/test/gravityMeshObject/case1/constant/saturn @@ -0,0 +1,21 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2212 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class uniformDimensionedVectorField; + location "constant"; + object saturn; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [0 1 -2 0 0 0 0]; +value (-100 0 0); + +// ************************************************************************* // diff --git a/applications/test/gravityMeshObject/case1/system/controlDict b/applications/test/gravityMeshObject/case1/system/controlDict new file mode 100644 index 0000000000..b8d3cc1611 --- /dev/null +++ b/applications/test/gravityMeshObject/case1/system/controlDict @@ -0,0 +1,34 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2212 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + location "system"; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +application blockMesh; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 10; + +deltaT 1; + +writeControl timeStep; + +writeInterval 1; + +// ************************************************************************* // diff --git a/src/OpenFOAM/meshes/MeshObject/MeshObject.C b/src/OpenFOAM/meshes/MeshObject/MeshObject.C index 6ca39b7b3a..6423b898c2 100644 --- a/src/OpenFOAM/meshes/MeshObject/MeshObject.C +++ b/src/OpenFOAM/meshes/MeshObject/MeshObject.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,6 +40,18 @@ Foam::MeshObject::MeshObject(const Mesh& mesh) {} +template class MeshObjectType, class Type> +Foam::MeshObject::MeshObject +( + const word& objName, + const Mesh& mesh +) +: + MeshObjectType(objName, mesh.thisDb()), + mesh_(mesh) +{} + + // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // template class MeshObjectType, class Type> @@ -50,11 +62,9 @@ const Type& Foam::MeshObject::New Args&&... args ) { - const Type* ptr = - mesh.thisDb().objectRegistry::template cfindObject - ( - Type::typeName - ); + Type* ptr = + mesh.thisDb().objectRegistry::template + getObjectPtr(Type::typeName); if (ptr) { @@ -68,34 +78,71 @@ const Type& Foam::MeshObject::New << " for region " << mesh.name() << endl; } - Type* objectPtr = new Type(mesh, std::forward(args)...); + ptr = new Type(mesh, std::forward(args)...); - regIOobject::store(static_cast*>(objectPtr)); + regIOobject::store(static_cast*>(ptr)); - return *objectPtr; + return *ptr; +} + + +template class MeshObjectType, class Type> +template +const Type& Foam::MeshObject::New +( + const word& objName, + const Mesh& mesh, + Args&&... args +) +{ + Type* ptr = + mesh.thisDb().objectRegistry::template + getObjectPtr(objName); + + if (ptr) + { + return *ptr; + } + + if (meshObject::debug) + { + Pout<< "MeshObject::New('" << objName + << "', const " << Mesh::typeName + << "&, ...) : constructing " << objName + << " of type " << Type::typeName + << " for region " << mesh.name() << endl; + } + + ptr = new Type(objName, mesh, std::forward(args)...); + + regIOobject::store(static_cast*>(ptr)); + + return *ptr; } // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * // template class MeshObjectType, class Type> -bool Foam::MeshObject::Delete(const Mesh& mesh) +bool Foam::MeshObject::Delete +( + const word& objName, + const Mesh& mesh +) { Type* ptr = - mesh.thisDb().objectRegistry::template getObjectPtr - ( - Type::typeName - ); + mesh.thisDb().objectRegistry::template + getObjectPtr(objName); if (ptr) { if (meshObject::debug) { Pout<< "MeshObject::Delete(const Mesh&) : deleting " - << Type::typeName << endl; + << objName << endl; } - return mesh.thisDb().checkOut(ptr); + return mesh.thisDb().checkOut(static_cast*>(ptr)); } return false; diff --git a/src/OpenFOAM/meshes/MeshObject/MeshObject.H b/src/OpenFOAM/meshes/MeshObject/MeshObject.H index 4500261f94..11554ee3da 100644 --- a/src/OpenFOAM/meshes/MeshObject/MeshObject.H +++ b/src/OpenFOAM/meshes/MeshObject/MeshObject.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -63,12 +63,13 @@ Note MeshObjects of type UpdateableMeshObject. SourceFiles + meshObject.C MeshObject.C \*---------------------------------------------------------------------------*/ -#ifndef MeshObject_H -#define MeshObject_H +#ifndef Foam_MeshObject_H +#define Foam_MeshObject_H #include "regIOobject.H" #include "objectRegistry.H" @@ -78,7 +79,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations class mapPolyMesh; /*---------------------------------------------------------------------------*\ @@ -90,10 +91,9 @@ class MeshObject : public MeshObjectType { - protected: - // Reference to Mesh + //- Reference to the mesh const Mesh& mesh_; @@ -101,30 +101,52 @@ public: // Constructors - //- Construct on Mesh type + //- Construct with Type::typeName on Mesh explicit MeshObject(const Mesh& mesh); - // Selectors + //- Construct with given object name on Mesh + MeshObject(const word& objName, const Mesh& mesh); - //- Get existing or create a new MeshObject + + // Factory Methods + + //- Get existing or create a new MeshObject. Registered with typeName template static const Type& New(const Mesh& mesh, Args&&... args); + //- Get existing or create a new MeshObject using supplied + //- registration name + template + static const Type& New + ( + const word& objName, + const Mesh& mesh, + Args&&... args + ); + //- Destructor virtual ~MeshObject() = default; - //- Static destructor - static bool Delete(const Mesh& mesh); + //- Static destructor using supplied registration name + static bool Delete(const word& objName, const Mesh& mesh); + + //- Static destructor using Type::typeName + static bool Delete(const Mesh& mesh) + { + return Delete(Type::typeName, mesh); + } // Member Functions - const Mesh& mesh() const + //- Reference to the mesh + const Mesh& mesh() const noexcept { return mesh_; } + //- Dummy write virtual bool writeData(Ostream& os) const { return true; @@ -136,19 +158,21 @@ public: Class meshObject Declaration \*---------------------------------------------------------------------------*/ +//- The meshObject is a concrete regIOobject class meshObject : public regIOobject { public: - // Declare name of the class and its debug switch + //- Runtime declaration and debug switch ClassName("meshObject"); + // Constructors - //- Construct from name and instance on registry - meshObject(const word& typeName, const objectRegistry& obr); + //- Construct with given object name on a registry + meshObject(const word& objName, const objectRegistry& obr); // Static Member Functions @@ -187,9 +211,9 @@ class TopologicalMeshObject public: //- Construct from name and instance on registry - TopologicalMeshObject(const word& typeName, const objectRegistry& obr) + TopologicalMeshObject(const word& objName, const objectRegistry& obr) : - meshObject(typeName, obr) + meshObject(objName, obr) {} }; @@ -206,9 +230,9 @@ class GeometricMeshObject public: //- Construct from name and instance on registry - GeometricMeshObject(const word& typeName, const objectRegistry& obr) + GeometricMeshObject(const word& objName, const objectRegistry& obr) : - TopologicalMeshObject(typeName, obr) + TopologicalMeshObject(objName, obr) {} }; @@ -225,9 +249,9 @@ class MoveableMeshObject public: //- Construct from name and instance on registry - MoveableMeshObject(const word& typeName, const objectRegistry& obr) + MoveableMeshObject(const word& objName, const objectRegistry& obr) : - GeometricMeshObject(typeName, obr) + GeometricMeshObject(objName, obr) {} virtual bool movePoints() = 0; @@ -246,9 +270,9 @@ class UpdateableMeshObject public: //- Construct from name and instance on registry - UpdateableMeshObject(const word& typeName, const objectRegistry& obr) + UpdateableMeshObject(const word& objName, const objectRegistry& obr) : - MoveableMeshObject(typeName, obr) + MoveableMeshObject(objName, obr) {} virtual void updateMesh(const mapPolyMesh& mpm) = 0; diff --git a/src/OpenFOAM/meshes/MeshObject/meshObject.C b/src/OpenFOAM/meshes/MeshObject/meshObject.C index dae46d2f7b..7f7b352d8c 100644 --- a/src/OpenFOAM/meshes/MeshObject/meshObject.C +++ b/src/OpenFOAM/meshes/MeshObject/meshObject.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2013 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -38,15 +38,18 @@ namespace Foam // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::meshObject::meshObject(const word& typeName, const objectRegistry& obr) +Foam::meshObject::meshObject(const word& objName, const objectRegistry& obr) : regIOobject ( IOobject ( - typeName, + objName, obr.instance(), - obr + obr, + IOobject::NO_READ, + IOobject::NO_WRITE, + IOobject::REGISTER ) ) {} diff --git a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.C b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.C index 0da1a5a3d9..d0de1e97ec 100644 --- a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.C +++ b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,14 +40,14 @@ namespace meshObjects // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::meshObjects::gravity::gravity(const Time& runTime) +Foam::meshObjects::gravity::gravity(const word& name, const Time& runTime) : - MeshObject(runTime), + MeshObject(name, runTime), uniformDimensionedVectorField ( IOobject ( - "g", // Must be identical to gravity::typeName! + name, runTime.constant(), runTime, IOobject::MUST_READ_IF_MODIFIED, diff --git a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H index 85fe3b9c3f..fc84e49a62 100644 --- a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H +++ b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -70,14 +70,38 @@ public: TypeNameNoDebug("g"); - //- Construct on Time with MUST_READ_IF_MODIFIED from "constant" - explicit gravity(const Time& runTime); + // Constructors + + //- Construct with given name on Time from \c constant + //- (MUST_READ_IF_MODIFIED) + gravity(const word& name, const Time& runTime); + + //- Construct "g" field on Time from \c constant + //- (MUST_READ_IF_MODIFIED) + explicit gravity(const Time& runTime) + : + gravity("g", runTime) + {} + + //- Return named gravity field cached or construct on Time + static const gravity& New(const word& name, const Time& runTime) + { + return MeshObject::New + ( + name, + runTime + ); + } + + //- Return gravity "g" field cached or construct on Time + static const gravity& New(const Time& runTime) + { + return MeshObject::New + ( + runTime + ); + } - //- Return cached object or construct on Time - static const gravity& New(const Time& runTime) - { - return MeshObject::New(runTime); - } //- Destructor virtual ~gravity() = default;