utility.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 // library configuration
20 #include "libmesh/libmesh_config.h"
21 
22 // System includes
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <sstream>
28 
29 #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
30 #include <sys/utsname.h>
31 #endif
32 
33 #ifdef LIBMESH_HAVE_PWD_H
34 #include <pwd.h>
35 #endif
36 
37 #ifdef LIBMESH_HAVE_DIRECT_H
38 #include <direct.h>
39 #endif
40 
41 // Local includes
42 #include "libmesh/utility.h"
43 #include "libmesh/timestamp.h"
44 
45 namespace libMesh
46 {
47 
48 
49 //-----------------------------------------------------------------------
50 // Utility members
51 
52 
53 // The system_info function duplicates some of the
54 // functionality found in the perf_log function.
55 // This way you can get information about a user's
56 // system without creating a perf_log object.
57 std::string Utility::system_info()
58 {
59  std::ostringstream oss;
60 
61  std::string date = Utility::get_timestamp();
62 
63 #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
64  // Get system information
65  struct utsname sysInfo;
66  uname(&sysInfo);
67 #endif
68 
69  // Get user information
70 #ifdef LIBMESH_HAVE_GETPWUID
71  struct passwd * p = getpwuid(getuid());
72 #endif
73 
74 
75  oss << '\n'
76  << " ---------------------------------------------------------------------\n"
77  << "| Time: " << date << '\n'
78 #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
79  << "| OS: " << sysInfo.sysname << '\n'
80  << "| HostName: " << sysInfo.nodename << '\n'
81  << "| OS Release " << sysInfo.release << '\n'
82  << "| OS Version: " << sysInfo.version << '\n'
83  << "| Machine: " << sysInfo.machine << '\n'
84 #else
85  << "| OS: " << "Unknown" << '\n'
86  << "| HostName: " << "Unknown" << '\n'
87  << "| OS Release " << "Unknown" << '\n'
88  << "| OS Version: " << "Unknown" << '\n'
89  << "| Machine: " << "Unknown" << '\n'
90 #endif
91 #ifdef LIBMESH_HAVE_GETPWUID
92  << "| Username: " << p->pw_name << '\n'
93 #else
94  << "| Username: " << "Unknown" << '\n'
95 #endif
96  << " ---------------------------------------------------------------------\n";
97 
98  return oss.str();
99 }
100 
101 
102 
103 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
104 
105 std::string Utility::complex_filename (const std::string & basename,
106  const unsigned int r_o_c)
107 {
108  std::string name(basename);
109 
110  if (r_o_c == 0)
111  name.append(".real");
112 
113  else
114  name.append(".imag");
115 
116  return name;
117 }
118 
119 
120 
121 void Utility::prepare_complex_data(const std::vector<Complex> & source,
122  std::vector<Real> & real_part,
123  std::vector<Real> & imag_part)
124 {
125  const unsigned int len = source.size();
126 
127  real_part.resize(len);
128  imag_part.resize(len);
129 
130  for (unsigned int i=0; i<len; i++)
131  {
132  real_part[i] = source[i].real();
133  imag_part[i] = source[i].imag();
134  }
135 }
136 
137 #endif // #ifdef LIBMESH_USE_COMPLEX_NUMBERS
138 
139 
140 int Utility::mkdir(const char* pathname) {
141 #if defined(LIBMESH_HAVE_MKDIR)
142  return ::mkdir(pathname, 0755);
143 #elif LIBMESH_HAVE_DECL__MKDIR
144  return _mkdir(pathname);
145 #else
146  libmesh_error_msg("Function mkdir not available on this system.");
147 #endif
148 
149 }
150 
151 } // namespace libMesh
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
std::string complex_filename(const std::string &basename, unsigned int r_o_c=0)
Definition: utility.C:105
std::string get_timestamp()
Definition: timestamp.C:37
int mkdir(const char *pathname)
Definition: utility.C:140
std::string system_info()
Definition: utility.C:57
void prepare_complex_data(const std::vector< Complex > &source, std::vector< Real > &real_part, std::vector< Real > &imag_part)
Definition: utility.C:121