OpenLB 1.7
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 }
58 void setLogOutDir(std::string logOutDir_)
59 {
60 createDirectory(logOutDir_);
61 logOutDir = logOutDir_;
62 }
63 void setImageOutDir(std::string imageOutDir_)
64 {
65 createDirectory(imageOutDir_);
66 createDirectory(imageOutDir_+"data/");
67 imageOutDir = imageOutDir_;
68 }
69 void setVtkOutDir(std::string vtkOutDir_)
70 {
71 createDirectory(vtkOutDir_);
72 createDirectory(vtkOutDir_+"data/");
73 vtkOutDir = vtkOutDir_;
74 }
75 void setGnuplotOutDir(std::string gnuplotOutDir_)
76 {
77 createDirectory(gnuplotOutDir_);
78 createDirectory(gnuplotOutDir_+"data/");
79 gnuplotOutDir = gnuplotOutDir_;
80 }
81 void makeCustomDir(std::string customDir)
82 {
83 createDirectory(customDir);
84 }
85 std::string getOlbDir() const
86 {
87 return olbDir;
88 }
89 std::string getLogOutDir() const
90 {
91 return logOutDir;
92 }
93 std::string getImageOutDir() const
94 {
95 return imageOutDir;
96 }
97 std::string getVtkOutDir() const
98 {
99 return vtkOutDir;
100 }
101 std::string getGnuplotOutDir() const
102 {
103 return gnuplotOutDir;
104 }
105private:
106 Directories() : clout(std::cout,"Directories")
107 {
108 setOlbDir("");
109 setLogOutDir("./tmp/");
110 setImageOutDir("./tmp/imageData/");
111 setVtkOutDir("./tmp/vtkData/");
112 setGnuplotOutDir("./tmp/gnuplotData/");
113 }
114 ~Directories() { }
115
116 void createDirectory(std::string path)
117 {
118 int rank = 0;
119#ifdef PARALLEL_MODE_MPI
120 rank = singleton::mpi().getRank();
121#endif
122 if (rank == 0) {
123 struct stat statbuf;
124 if (stat(path.c_str(), &statbuf) != 0) {
125#ifdef _WIN32
126 mkdir(path.c_str());
127#else /* Unix */
128 if (mkdir(path.c_str(), 0775) == 0) {
129 clout << "Directory " << path << " created." << std::endl;
130 }
131 //else
132 // clout << "Directory " << path << " failed." << std::endl;
133#endif
134 }
135 }
136 }
137
138private:
139 mutable OstreamManager clout;
140
141 std::string olbDir;
142 std::string logOutDir;
143 std::string imageOutDir;
144 std::string vtkOutDir;
145 std::string gnuplotOutDir;
146
147 friend Directories& directories();
148};
149
151{
152 static Directories singleton;
153 return singleton;
154}
155
156template<typename T>
157inline void checkValue(T input)
158{
159 if (280877.9 < input && input < 280878.1) {
160 std::cout << "Error: stop simulation due to 280878" << std::endl;
161 exit(-1);
162 }
163}
164
165inline void exit(int exitcode)
166{
167#ifdef PARALLEL_MODE_MPI
168 MPI_Abort(MPI_COMM_WORLD, exitcode);
169#else
170 std::exit(exitcode);
171#endif
172}
173
174} // namespace singleton
175
176} // namespace olb
177
178#endif
std::string getVtkOutDir() const
Definition singleton.h:97
std::string getGnuplotOutDir() const
Definition singleton.h:101
void setOutputDir(std::string outputDir)
Definition singleton.h:51
void setVtkOutDir(std::string vtkOutDir_)
Definition singleton.h:69
std::string getImageOutDir() const
Definition singleton.h:93
void setLogOutDir(std::string logOutDir_)
Definition singleton.h:58
void setGnuplotOutDir(std::string gnuplotOutDir_)
Definition singleton.h:75
void setOlbDir(std::string olbDir_)
Definition singleton.h:46
std::string getOlbDir() const
Definition singleton.h:85
friend Directories & directories()
Definition singleton.h:150
void setImageOutDir(std::string imageOutDir_)
Definition singleton.h:63
std::string getLogOutDir() const
Definition singleton.h:89
void makeCustomDir(std::string customDir)
Definition singleton.h:81
int getRank() const
Returns the process ID.
Wrapper functions that simplify the use of MPI.
void checkValue(T input)
Definition singleton.h:157
void exit(int exitcode)
Definition singleton.h:165
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.