Commit 455c8ef5 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: simplify use of case-relative paths

- provide relativePath() for argList and for Time.
  These are relative to the case globalPath().
  Eg,

     Info<< "output: " << runTime.relativePath(outputFile) << nl;
parent ce6cd338
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -38,11 +38,13 @@ using namespace Foam;
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
argList::noCheckProcessorDirectories(); // parallel OK, but without checks
// argList::noFunctionObjects();
argList::addOption("label", "value", "Test parsing of label");
argList::addOption("scalar", "value", "Test parsing of scalar");
argList::addOption("string", "value", "Test string lookup");
argList::addOption("relative", "PATH", "Test relativePath");
// These are actually lies (never had -parseLabel, -parseScalar etc),
// but good for testing...
......@@ -70,20 +72,27 @@ int main(int argc, char *argv[])
argList::addArgument("label");
argList::noMandatoryArgs();
argList args(argc, argv);
#include "setRootCase.H"
Info<< "command-line ("
Pout<< "command-line ("
<< args.options().size() << " options, "
<< args.args().size() << " args)" << nl
<< " " << args.commandLine().c_str() << nl << nl;
Info<< "rootPath: " << args.rootPath() << nl
Pout<< "rootPath: " << args.rootPath() << nl
<< "globalCase: " << args.globalCaseName() << nl
<< "globalPath: " << args.globalPath() << nl
<< nl;
Info<<"have: "
<<args.count({"label", "scalar"}) << " options" << nl;
if (args.found("relative"))
{
Pout<< "input path: " << args["relative"] << nl
<< "relative : " << args.relativePath(args["relative"], true) << nl
<< nl;
}
Info<< "have: "
<< args.count({"label", "scalar"}) << " options" << nl;
label ival;
scalar sval;
......
......@@ -27,7 +27,7 @@ Description
#include "argList.H"
#include "IOstreams.H"
#include "TimePaths.H"
#include "Time.H"
#include "timeSelector.H"
using namespace Foam;
......@@ -58,13 +58,31 @@ bool print(const instantList& instants)
int main(int argc, char *argv[])
{
argList::addNote("Test timeSelector");
argList::addNote("Test timeSelector and TimePaths");
timeSelector::addOptions(true, true);
argList::noLibs();
argList::noFunctionObjects();
argList::addOption("relative", "PATH", "Test relativePath");
#include "setRootCase.H"
#include "createTime.H"
Pout<< "Time" << nl
<< "rootPath: " << runTime.rootPath() << nl
<< "path: " << runTime.path() << nl
<< "globalCase: " << runTime.globalCaseName() << nl
<< "globalPath: " << runTime.globalPath() << nl
<< nl;
if (args.found("relative"))
{
Pout<< "input path: " << args["relative"] << nl
<< "relative : " << runTime.relativePath(args["relative"], true)
<< nl
<< nl;
}
autoPtr<TimePaths> timePaths;
......
......@@ -94,7 +94,7 @@ if (doLagrangian)
);
Info<< " Lagrangian: "
<< writer.output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(writer.output()) << nl;
writer.writeTimeValue(mesh.time().value());
writer.writeGeometry();
......
......@@ -107,7 +107,7 @@ Description
);
Info<< " Surface : "
<< writer.output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(writer.output()) << nl;
writer.writeTimeValue(timeValue);
......@@ -211,7 +211,7 @@ Description
);
Info<< " FaceZone : "
<< writer.output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(writer.output()) << nl;
writer.beginFile(fz.name());
......
......@@ -42,7 +42,7 @@ if (faceSetName.size())
);
Info<< " faceSet : "
<< outputName.relative(runTime.globalPath()) << nl;
<< runTime.relativePath(outputName) << nl;
vtk::writeFaceSet
(
......@@ -70,7 +70,7 @@ if (pointSetName.size())
);
Info<< " pointSet : "
<< outputName.relative(runTime.globalPath()) << nl;
<< runTime.relativePath(outputName) << nl;
vtk::writePointSet
(
......
......@@ -113,7 +113,7 @@ Description
);
Info<< " Internal : "
<< internalWriter->output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(internalWriter->output()) << nl;
internalWriter->writeTimeValue(mesh.time().value());
internalWriter->writeGeometry();
......@@ -163,7 +163,7 @@ Description
);
Info<< " Boundaries: "
<< writer->output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(writer->output()) << nl;
writer->writeTimeValue(timeValue);
writer->writeGeometry();
......@@ -229,7 +229,7 @@ Description
);
Info<< " Boundary : "
<< writer->output().relative(runTime.globalPath()) << nl;
<< runTime.relativePath(writer->output()) << nl;
writer->writeTimeValue(timeValue);
writer->writeGeometry();
......
......@@ -650,7 +650,7 @@ int main(int argc, char *argv[])
if (args.found("overwrite") && isDir(regionDir))
{
Info<< "Removing old directory "
<< regionDir.relative(runTime.globalPath())
<< runTime.relativePath(regionDir)
<< nl << endl;
rmDir(regionDir);
}
......
......@@ -693,8 +693,7 @@ int main(int argc, char *argv[])
vtkWriter->writeGeometry();
Info<< "Writing VTK to "
<< ((vtkOutputDir/outputName).ext(vtkWriter->ext()))
.relative(runTime.globalPath()) << nl;
<< runTime.relativePath(vtkWriter->output()) << nl;
}
}
else
......
......@@ -35,11 +35,12 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<IOobject>& ip)
os << "IOobject: "
<< io.type() << token::SPACE
<< io.name() << token::SPACE
<< "readOpt:" << token::SPACE << io.readOpt() << token::SPACE
<< "writeOpt:" << token::SPACE << io.writeOpt() << token::SPACE
<< "globalObject:" << token::SPACE << io.globalObject() << token::SPACE
<< io.path() << endl;
<< io.name()
<< " local: " << io.local()
<< " readOpt: " << io.readOpt()
<< " writeOpt: " << io.writeOpt()
<< " globalObject: " << io.globalObject()
<< token::SPACE << io.path() << endl;
return os;
}
......
......@@ -133,28 +133,6 @@ Foam::TimePaths::TimePaths
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::fileName Foam::TimePaths::caseSystem() const
{
if (processorCase_)
{
return ".."/system();
}
return system();
}
Foam::fileName Foam::TimePaths::caseConstant() const
{
if (processorCase_)
{
return ".."/constant();
}
return constant();
}
Foam::instantList Foam::TimePaths::findTimes
(
const fileName& directory,
......
......@@ -108,86 +108,64 @@ public:
// Member Functions
//- True if case running with parallel distributed directories
//- (ie. not NFS mounted)
inline bool distributed() const;
//- Return true if this is a processor case
bool processorCase() const
{
return processorCase_;
}
inline bool processorCase() const;
//- Return root path
const fileName& rootPath() const
{
return rootPath_;
}
inline const fileName& rootPath() const;
//- Return global case name
const fileName& globalCaseName() const
{
return globalCaseName_;
}
inline const fileName& globalCaseName() const;
//- Return case name
const fileName& caseName() const
{
return case_;
}
inline const fileName& caseName() const;
//- Return case name
fileName& caseName()
{
return case_;
}
//- The case name for modification (use with caution)
inline fileName& caseName();
//- Return system name
const word& system() const
{
return system_;
}
//- Return path for the case
inline fileName path() const;
//- Return global path for the case
inline fileName globalPath() const;
//- Return the input relative to the globalPath by stripping off
//- a leading value of the globalPath
//
// \param input the directory or filename to make case-relative
// \param caseTag replace globalPath with \<case\> for later
// use with expand(), or prefix \<case\> if the file name was
// not an absolute location
inline fileName relativePath
(
const fileName& input,
const bool caseTag = false
) const;
//- Return the system name for the case, which is
//- \c ../system() for parallel runs.
fileName caseSystem() const;
//- Return constant name
const word& constant() const
{
return constant_;
}
//- Is case running with parallel distributed directories
// (i.e. not NFS mounted)
bool distributed() const
{
return distributed_;
}
inline const word& constant() const;
//- Return system name
inline const word& system() const;
//- Return the constant name for the case, which is
//- \c ../constant() for parallel runs.
fileName caseConstant() const;
inline fileName caseConstant() const;
//- Return path for the case
fileName path() const
{
return rootPath()/caseName();
}
//- Return the system name for the case, which is
//- \c ../system() for parallel runs.
inline fileName caseSystem() const;
//- Return global path for the case
fileName globalPath() const
{
return rootPath()/globalCaseName();
}
//- Return constant path
inline fileName constantPath() const;
//- Return system path
fileName systemPath() const
{
return path()/system();
}
//- Return constant path
fileName constantPath() const
{
return path()/constant();
}
inline fileName systemPath() const;
// Searching
......@@ -223,6 +201,10 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "TimePathsI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 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 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 * * * * * * * * * * * * * //
inline bool Foam::TimePaths::distributed() const
{
return distributed_;
}
inline bool Foam::TimePaths::processorCase() const
{
return processorCase_;
}
inline const Foam::fileName& Foam::TimePaths::rootPath() const
{
return rootPath_;
}
inline const Foam::fileName& Foam::TimePaths::globalCaseName() const
{
return globalCaseName_;
}
inline const Foam::fileName& Foam::TimePaths::caseName() const
{
return case_;
}
inline Foam::fileName& Foam::TimePaths::caseName()
{
return case_;
}
inline Foam::fileName Foam::TimePaths::path() const
{
return rootPath()/caseName();
}
inline Foam::fileName Foam::TimePaths::globalPath() const
{
return rootPath()/globalCaseName();
}
inline Foam::fileName Foam::TimePaths::relativePath
(
const fileName& input,
const bool caseTag
) const
{
return input.relative(globalPath(), caseTag);
}
inline const Foam::word& Foam::TimePaths::constant() const
{
return constant_;
}
inline const Foam::word& Foam::TimePaths::system() const
{
return system_;
}
inline Foam::fileName Foam::TimePaths::caseConstant() const
{
if (processorCase_)
{
return ".."/constant();
}
return constant();
}
inline Foam::fileName Foam::TimePaths::caseSystem() const
{
if (processorCase_)
{
return ".."/system();
}
return system();
}
inline Foam::fileName Foam::TimePaths::constantPath() const
{
return path()/constant();
}
inline Foam::fileName Foam::TimePaths::systemPath() const
{
return path()/system();
}
// ************************************************************************* //
......@@ -25,7 +25,6 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline Foam::scalar Foam::TimeState::timeOutputValue() const
{
return timeToUserTime(value());
......
......@@ -304,6 +304,19 @@ public:
// \note This is guaranteed to be an absolute path
inline fileName globalPath() const;
//- Return the input relative to the globalPath by stripping off
//- a leading value of the globalPath
//
// \param input the directory or filename to make case-relative
// \param caseTag replace globalPath with \<case\> for later
// use with expand(), or prefix \<case\> if the file name was
// not an absolute location
inline fileName relativePath
(
const fileName& input,
const bool caseTag = false
) const;
//- Return distributed flag
//- (i.e. are rootPaths different on different machines)
inline bool distributed() const;
......
......@@ -87,6 +87,16 @@ inline Foam::fileName Foam::argList::globalPath() const
}
inline Foam::fileName Foam::argList::relativePath
(
const fileName& input,
const bool caseTag
) const
{
return input.relative(globalPath(), caseTag);
}
inline bool Foam::argList::distributed() const
{
return parRunControl_.distributed();
......
......@@ -96,7 +96,7 @@ namespace Foam
//- OpenFOAM version (name or stringified number) as a std::string
extern const std::string version;
//- Test if the patch string appeared to be in use,
//- Test if the patch string appears to be in use,
//- which is when it is defined (non-zero).
bool patched();
......
......@@ -367,7 +367,7 @@ std::string Foam::fileName::nameLessExt(const std::string& str)
Foam::fileName Foam::fileName::relative
(
const fileName& parent,
const bool caseRelative
const bool caseTag
) const
{
const auto top = parent.size();
......@@ -383,7 +383,7 @@ Foam::fileName Foam::fileName::relative
&& f.startsWith(parent)
)
{
if (caseRelative)
if (caseTag)
{
return "<case>"/f.substr(top+1);
}
......@@ -392,7 +392,7 @@ Foam::fileName Foam::fileName::relative
return f.substr(top+1);
}
}
else if (caseRelative && f.size() && !f.isAbsolute())
else if (caseTag && f.size() && !f.isAbsolute())
{
return "<case>"/f;
}
......
......@@ -294,13 +294,13 @@ public:
//- where possible.
//
// \param parent the parent directory
// \param caseRelative replace the parent with \<case\> for later
// \param caseTag replace the parent with \<case\> for later
// use with expand(), or prefix \<case\> if the file name was
// not an absolute location
fileName relative
(
const fileName& parent,
const bool caseRelative = false