ENH: MeshObject: specify name (instead of typeName)

This commit is contained in:
Mattijs Janssens 2023-02-01 15:32:14 +00:00 committed by Mark OLESEN
parent 8b63f64503
commit c0e63a8de7
12 changed files with 346 additions and 55 deletions

View File

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

View File

@ -0,0 +1,5 @@
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude
EXE_LIBS = \
-lfiniteVolume

View File

@ -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 <http://www.gnu.org/licenses/>.
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<uniformDimensionedVectorField>())
{
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;
}
// ************************************************************************* //

View File

@ -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);
// ************************************************************************* //

View File

@ -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);
// ************************************************************************* //

View File

@ -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);
// ************************************************************************* //

View File

@ -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;
// ************************************************************************* //

View File

@ -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<Mesh, MeshObjectType, Type>::MeshObject(const Mesh& mesh)
{}
template<class Mesh, template<class> class MeshObjectType, class Type>
Foam::MeshObject<Mesh, MeshObjectType, Type>::MeshObject
(
const word& objName,
const Mesh& mesh
)
:
MeshObjectType<Mesh>(objName, mesh.thisDb()),
mesh_(mesh)
{}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
template<class Mesh, template<class> class MeshObjectType, class Type>
@ -50,11 +62,9 @@ const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
Args&&... args
)
{
const Type* ptr =
mesh.thisDb().objectRegistry::template cfindObject<Type>
(
Type::typeName
);
Type* ptr =
mesh.thisDb().objectRegistry::template
getObjectPtr<Type>(Type::typeName);
if (ptr)
{
@ -68,34 +78,71 @@ const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh, std::forward<Args>(args)...);
ptr = new Type(mesh, std::forward<Args>(args)...);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(ptr));
return *objectPtr;
return *ptr;
}
template<class Mesh, template<class> class MeshObjectType, class Type>
template<class... Args>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
(
const word& objName,
const Mesh& mesh,
Args&&... args
)
{
Type* ptr =
mesh.thisDb().objectRegistry::template
getObjectPtr<Type>(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>(args)...);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(ptr));
return *ptr;
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
template<class Mesh, template<class> class MeshObjectType, class Type>
bool Foam::MeshObject<Mesh, MeshObjectType, Type>::Delete(const Mesh& mesh)
bool Foam::MeshObject<Mesh, MeshObjectType, Type>::Delete
(
const word& objName,
const Mesh& mesh
)
{
Type* ptr =
mesh.thisDb().objectRegistry::template getObjectPtr<Type>
(
Type::typeName
);
mesh.thisDb().objectRegistry::template
getObjectPtr<Type>(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<MeshObjectType<Mesh>*>(ptr));
}
return false;

View File

@ -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<Mesh>
{
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<class... Args>
static const Type& New(const Mesh& mesh, Args&&... args);
//- Get existing or create a new MeshObject using supplied
//- registration name
template<class... Args>
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<Mesh>(typeName, obr)
TopologicalMeshObject<Mesh>(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<Mesh>(typeName, obr)
GeometricMeshObject<Mesh>(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<Mesh>(typeName, obr)
MoveableMeshObject<Mesh>(objName, obr)
{}
virtual void updateMesh(const mapPolyMesh& mpm) = 0;

View File

@ -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
)
)
{}

View File

@ -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<Time, TopologicalMeshObject, gravity>(runTime),
MeshObject<Time, TopologicalMeshObject, gravity>(name, runTime),
uniformDimensionedVectorField
(
IOobject
(
"g", // Must be identical to gravity::typeName!
name,
runTime.constant(),
runTime,
IOobject::MUST_READ_IF_MODIFIED,

View File

@ -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<Time, TopologicalMeshObject, gravity>::New
(
name,
runTime
);
}
//- Return gravity "g" field cached or construct on Time
static const gravity& New(const Time& runTime)
{
return MeshObject<Time, TopologicalMeshObject, gravity>::New
(
runTime
);
}
//- Return cached object or construct on Time
static const gravity& New(const Time& runTime)
{
return MeshObject<Time, TopologicalMeshObject, gravity>::New(runTime);
}
//- Destructor
virtual ~gravity() = default;