Nektar++
Filters/Filter.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Filter.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Base class for filters.
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
37namespace Nektar::SolverUtils
38{
40{
41 static FilterFactory instance;
42 return instance;
43}
44
46 const std::shared_ptr<EquationSystem> &pEquation)
47 : m_session(pSession), m_equ(pEquation)
48{
49}
50
52{
53}
54
55std::string Filter::v_SetupOutput(const std::string ext,
56 const ParamMap &pParams)
57{
58 // get the backup switch
59 bool backup = m_session->GetBackups();
60
61 // determinte the root
62 bool root = m_session->GetComm()->TreatAsRankZero();
63
64 std::string outname;
65
66 // set up the base output file name
67 auto it = pParams.find("OutputFile");
68 if (it == pParams.end())
69 {
70 outname = m_session->GetSessionName();
71 }
72 else
73 {
74 ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
75 outname = it->second;
76 }
77
78 // remove any aextension from the input name and add the correct one
79 outname = fs::path(outname).replace_extension("").string() + ext;
80 fs::path specPath(outname);
81 // filters output are handled by root (rank 0) only. check if the path is
82 // already exists
83 if (backup && root && fs::exists(specPath))
84 {
85 // rename. foo/output-name.ext -> foo/output-name.bak0.ext and in case
86 // foo/output-name.bak0.chk already exists,
87 // foo/output-name.bak0.ext ->foo/output-name.bak1.chk
88 fs::path bakPath = specPath;
89 int cnt = 0;
90 while (fs::exists(bakPath))
91 {
92 bakPath = specPath.parent_path();
93 // this is for parallel in time
94 bakPath += specPath.stem();
95 bakPath += fs::path(".bak" + std::to_string(cnt++));
96 bakPath += specPath.extension();
97 }
98 std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
99 try
100 {
101 fs::rename(specPath, bakPath);
102 }
103 catch (fs::filesystem_error &e)
104 {
105 ASSERTL0(e.code() == std::errc::no_such_file_or_directory,
106 "Filesystem error: " + std::string(e.what()));
107 }
108 }
109 // return the output file name
110 return LibUtilities::PortablePath(specPath);
111}
112std::string Filter::v_SetupOutput(const std::string ext,
113 const std::string inname)
114{
115 // get the backup switch
116 bool backup = m_session->GetBackups();
117
118 // determinte the root
119 bool root = m_session->GetComm()->TreatAsRankZero();
120
121 // for the history point filter we have already established the file name
122 std::string outname = inname;
123
124 // remove any aextension from the input name and add the correct one
125 outname = fs::path(outname).replace_extension("").string() + ext;
126 // Path to output
127 fs::path specPath(outname);
128
129 // filters output are handled by root (rank 0) only. check if the path is
130 // already exists
131 if (backup && root && fs::exists(specPath))
132 {
133 // rename. foo/output-name.ext -> foo/output-name.bak0.ext and in case
134 // foo/output-name.bak0.chk already exists,
135 // foo/output-name.bak0.ext ->foo/output-name.bak1.chk
136 //
137 fs::path bakPath = specPath;
138 int cnt = 0;
139 while (fs::exists(bakPath))
140 {
141 bakPath = specPath.parent_path();
142 // this is for parallel in time
143 bakPath += specPath.stem();
144 bakPath += fs::path(".bak" + std::to_string(cnt++));
145 bakPath += specPath.extension();
146 }
147 std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
148 try
149 {
150 fs::rename(specPath, bakPath);
151 }
152 catch (fs::filesystem_error &e)
153 {
154 ASSERTL0(e.code() == std::errc::no_such_file_or_directory,
155 "Filesystem error: " + std::string(e.what()));
156 }
157 }
158
159 return LibUtilities::PortablePath(specPath);
160}
161
162} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
Provides a generic Factory class.
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:93
SOLVER_UTILS_EXPORT Filter(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation)
virtual SOLVER_UTILS_EXPORT ~Filter()
std::map< std::string, std::string > ParamMap
Definition: Filter.h:66
virtual SOLVER_UTILS_EXPORT std::string v_SetupOutput(const std::string ext, const ParamMap &pParams)
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:66
std::shared_ptr< SessionReader > SessionReaderSharedPtr
FilterFactory & GetFilterFactory()