Nektar++
Loading...
Searching...
No Matches
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
51std::string Filter::v_SetupOutput(const std::string ext,
52 const ParamMap &pParams)
53{
54 // get the backup switch
55 bool backup = m_session->GetBackups();
56
57 // determinte the root
58 bool root = m_session->GetComm()->TreatAsRankZero();
59
60 std::string outname;
61
62 // set up the base output file name
63 auto it = pParams.find("OutputFile");
64 if (it == pParams.end())
65 {
66 outname = m_session->GetSessionName();
67 }
68 else
69 {
70 ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
71 outname = it->second;
72 }
73
74 // remove any aextension from the input name and add the correct one
75 outname = fs::path(outname).replace_extension("").string() + ext;
76 fs::path specPath(outname);
77 // filters output are handled by root (rank 0) only. check if the path is
78 // already exists
79 if (backup && root && fs::exists(specPath))
80 {
81 // rename. foo/output-name.ext -> foo/output-name.bak0.ext and in case
82 // foo/output-name.bak0.chk already exists,
83 // foo/output-name.bak0.ext ->foo/output-name.bak1.chk
84 fs::path bakPath = specPath;
85 int cnt = 0;
86 while (fs::exists(bakPath))
87 {
88 bakPath = specPath.parent_path();
89 // this is for parallel in time
90 bakPath += specPath.stem();
91 bakPath += fs::path(".bak" + std::to_string(cnt++));
92 bakPath += specPath.extension();
93 }
94 std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
95 try
96 {
97 fs::rename(specPath, bakPath);
98 }
99 catch (fs::filesystem_error &e)
100 {
101 ASSERTL0(e.code() == std::errc::no_such_file_or_directory,
102 "Filesystem error: " + std::string(e.what()));
103 }
104 }
105 // return the output file name
106 return LibUtilities::PortablePath(specPath);
107}
108
109std::string Filter::v_SetupOutput(const std::string ext,
110 const std::string inname)
111{
112 // get the backup switch
113 bool backup = m_session->GetBackups();
114
115 // determinte the root
116 bool root = m_session->GetComm()->TreatAsRankZero();
117
118 // for the history point filter we have already established the file name
119 std::string outname = inname;
120
121 // remove any aextension from the input name and add the correct one
122 outname = fs::path(outname).replace_extension("").string() + ext;
123 // Path to output
124 fs::path specPath(outname);
125
126 // filters output are handled by root (rank 0) only. check if the path is
127 // already exists
128 if (backup && root && fs::exists(specPath))
129 {
130 // rename. foo/output-name.ext -> foo/output-name.bak0.ext and in case
131 // foo/output-name.bak0.chk already exists,
132 // foo/output-name.bak0.ext ->foo/output-name.bak1.chk
133 //
134 fs::path bakPath = specPath;
135 int cnt = 0;
136 while (fs::exists(bakPath))
137 {
138 bakPath = specPath.parent_path();
139 // this is for parallel in time
140 bakPath += specPath.stem();
141 bakPath += fs::path(".bak" + std::to_string(cnt++));
142 bakPath += specPath.extension();
143 }
144 std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
145 try
146 {
147 fs::rename(specPath, bakPath);
148 }
149 catch (fs::filesystem_error &e)
150 {
151 ASSERTL0(e.code() == std::errc::no_such_file_or_directory,
152 "Filesystem error: " + std::string(e.what()));
153 }
154 }
155
156 return LibUtilities::PortablePath(specPath);
157}
158
159} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
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)
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.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
FilterFactory & GetFilterFactory()