Debian Codegraph @

src/atlas/output/Gmsh.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <fstream>
12 #include <map>
13 #include <ostream>
14 #include <sstream>
15 #include <string>
16
17 #include "eckit/exception/Exceptions.h"
18
19 #include "atlas/output/Gmsh.h"
20 #include "atlas/output/detail/GmshImpl.h"
21 #include "atlas/parallel/mpi/mpi.h"
22
23 namespace atlas {
24 namespace output {
25
26 // -----------------------------------------------------------------------------
27
28 std::string GmshFileStream::parallelPathName(const eckit::PathName& path, int part) {
29 std::stringstream s;
30 // s << path.dirName() << "/" << path.baseName(false) << "_p" << part <<
31 // ".msh";
32 s << path.asString() << ".p" << part;
33 return s.str();
34 }
35
36 // -----------------------------------------------------------------------------
37
38 GmshFileStream::GmshFileStream(const eckit::PathName& file_path, const char* mode, int part): std::ofstream() {
39 eckit::PathName par_path(file_path);
40 std::ios_base::openmode omode = std::ios_base::out;
41 if (std::string(mode) == "w") {
42 omode = std::ios_base::out;
43 }
44 else if (std::string(mode) == "a") {
45 omode = std::ios_base::app;
46 }
47
48 if (part < 0 || mpi::size() == 1) {
49 std::ofstream::open(file_path.localPath(), omode);
50 }
51 else {
52 if (mpi::rank() == 0) {
53 eckit::PathName par_path(file_path);
54 std::ofstream par_file(par_path.localPath(), std::ios_base::out);
55 if (!par_file.is_open()) {
56 throw eckit::CantOpenFile(par_path);
57 }
58 for (idx_t p = 0; p < mpi::size(); ++p) {
59 par_file << "Merge \"" << parallelPathName(file_path, p) << "\";" << std::endl;
60 }
61 par_file.close();
62 }
63 eckit::PathName path(parallelPathName(file_path, part));
64 std::ofstream::open(path.localPath(), omode);
65 }
66 }
67
68 //----------------------------------------------------------------------------------------------------------------------
69
70 Gmsh::Gmsh(const Output& output): Output(output) {}
71
72 Gmsh::Gmsh(std::ostream& s): Output(new detail::GmshImpl(s)) {}
73
74 Gmsh::Gmsh(std::ostream& s, const eckit::Parametrisation& c): Output(new detail::GmshImpl(s, c)) {}
75
76 Gmsh::Gmsh(const eckit::PathName& p, const std::string& mode): Output(new detail::GmshImpl(p, mode)) {}
77
78 Gmsh::Gmsh(const eckit::PathName& p, const std::string& mode, const eckit::Parametrisation& c):
79 Output(new detail::GmshImpl(p, mode, c)) {}
80
81 Gmsh::Gmsh(const eckit::PathName& p): Output(new detail::GmshImpl(p)) {}
82
83 Gmsh::Gmsh(const eckit::PathName& p, const eckit::Parametrisation& c): Output(new detail::GmshImpl(p, c)) {}
84
85 } // namespace output
86 } // namespace atlas