OpenLB 1.8.1
Loading...
Searching...
No Matches
singleton.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2006, 2007 Jonas Latt
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
27#ifndef SINGLETON_H
28#define SINGLETON_H
29
30#include <string>
31#include <stdlib.h>
32#include "io/ostreamManager.h"
34#ifdef _WIN32
35#include <direct.h>
36#else //f defined __linux__
37#include <sys/stat.h>
38#endif
39
40namespace olb {
41
42namespace singleton {
43
45public:
46 void setOlbDir(std::string olbDir_)
47 {
48 createDirectory(olbDir_);
49 olbDir = olbDir_;
50 }
51 void setOutputDir(std::string outputDir)
52 {
53 setLogOutDir(outputDir);
54 setImageOutDir(outputDir + "imageData/");
55 setVtkOutDir(outputDir + "vtkData/");
56 setGnuplotOutDir(outputDir + "gnuplotData/");
57 setIntrospectionOutDir(outputDir + "introspectionData/");
58 }
59 void setLogOutDir(std::string logOutDir_)
60 {
61 createDirectory(logOutDir_);
62 logOutDir = logOutDir_;
63 }
64 void setImageOutDir(std::string imageOutDir_)
65 {
66 createDirectory(imageOutDir_);
67 createDirectory(imageOutDir_+"data/");
68 imageOutDir = imageOutDir_;
69 }
70 void setVtkOutDir(std::string vtkOutDir_)
71 {
72 createDirectory(vtkOutDir_);
73 createDirectory(vtkOutDir_+"data/");
74 vtkOutDir = vtkOutDir_;
75 }
76 void setGnuplotOutDir(std::string gnuplotOutDir_)
77 {
78 createDirectory(gnuplotOutDir_);
79 createDirectory(gnuplotOutDir_+"data/");
80 gnuplotOutDir = gnuplotOutDir_;
81 }
82 void setIntrospectionOutDir(std::string introspectionOutDir_)
83 {
84 createDirectory(introspectionOutDir_);
85 introspectionOutDir = introspectionOutDir_;
86 }
87 void makeCustomDir(std::string customDir)
88 {
89 createDirectory(customDir);
90 }
91 std::string getOlbDir() const
92 {
93 return olbDir;
94 }
95 std::string getLogOutDir() const
96 {
97 return logOutDir;
98 }
99 std::string getImageOutDir() const
100 {
101 return imageOutDir;
102 }
103 std::string getVtkOutDir() const
104 {
105 return vtkOutDir;
106 }
107 std::string getGnuplotOutDir() const
108 {
109 return gnuplotOutDir;
110 }
111 std::string getIntrospectionOutDir() const
112 {
113 return introspectionOutDir;
114 }
115private:
116 Directories() : clout(std::cout,"Directories")
117 {
118 setOlbDir("");
119 setLogOutDir("./tmp/");
120 setImageOutDir("./tmp/imageData/");
121 setVtkOutDir("./tmp/vtkData/");
122 setGnuplotOutDir("./tmp/gnuplotData/");
123 setIntrospectionOutDir("./tmp/introspectionData/");
124 }
125 ~Directories() { }
126
127 void createDirectory(std::string path)
128 {
129 int rank = 0;
130#ifdef PARALLEL_MODE_MPI
131 rank = singleton::mpi().getRank();
132#endif
133 if (rank == 0) {
134 struct stat statbuf;
135 if (stat(path.c_str(), &statbuf) != 0) {
136#ifdef _WIN32
137 mkdir(path.c_str());
138#else /* Unix */
139 if (mkdir(path.c_str(), 0775) == 0) {
140 clout << "Directory " << path << " created." << std::endl;
141 }
142 //else
143 // clout << "Directory " << path << " failed." << std::endl;
144#endif
145 }
146 }
147 }
148
149private:
150 mutable OstreamManager clout;
151
152 std::string olbDir;
153 std::string logOutDir;
154 std::string imageOutDir;
155 std::string vtkOutDir;
156 std::string gnuplotOutDir;
157 std::string introspectionOutDir;
158
159 friend Directories& directories();
160};
161
163{
164 static Directories singleton;
165 return singleton;
166}
167
168template<typename T>
169inline void checkValue(T input)
170{
171 if (280877.9 < input && input < 280878.1) {
172 std::cout << "Error: stop simulation due to 280878" << std::endl;
173 exit(-1);
174 }
175}
176
177inline void exit(int exitcode)
178{
179#ifdef PARALLEL_MODE_MPI
180 MPI_Abort(MPI_COMM_WORLD, exitcode);
181#else
182 std::exit(exitcode);
183#endif
184}
185
186} // namespace singleton
187
188} // namespace olb
189
190#endif
std::string getVtkOutDir() const
Definition singleton.h:103
std::string getGnuplotOutDir() const
Definition singleton.h:107
void setOutputDir(std::string outputDir)
Definition singleton.h:51
void setVtkOutDir(std::string vtkOutDir_)
Definition singleton.h:70
std::string getImageOutDir() const
Definition singleton.h:99
void setLogOutDir(std::string logOutDir_)
Definition singleton.h:59
void setGnuplotOutDir(std::string gnuplotOutDir_)
Definition singleton.h:76
void setOlbDir(std::string olbDir_)
Definition singleton.h:46
std::string getOlbDir() const
Definition singleton.h:91
friend Directories & directories()
Definition singleton.h:162
void setIntrospectionOutDir(std::string introspectionOutDir_)
Definition singleton.h:82
void setImageOutDir(std::string imageOutDir_)
Definition singleton.h:64
std::string getIntrospectionOutDir() const
Definition singleton.h:111
std::string getLogOutDir() const
Definition singleton.h:95
void makeCustomDir(std::string customDir)
Definition singleton.h:87
int getRank() const
Returns the process ID.
Wrapper functions that simplify the use of MPI.
void checkValue(T input)
Definition singleton.h:169
void exit(int exitcode)
Definition singleton.h:177
MpiManager & mpi()
Directories & directories()
Definition singleton.h:162
Top level namespace for all of OpenLB.