| 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 <map> |
| 12 |
#include <string> |
| 13 |
|
| 14 |
#include "atlas/field/Field.h" |
| 15 |
#include "atlas/field/FieldSet.h" |
| 16 |
#include "atlas/functionspace/FunctionSpace.h" |
| 17 |
#include "atlas/mesh/Mesh.h" |
| 18 |
#include "atlas/mesh/Nodes.h" |
| 19 |
#include "atlas/mesh/actions/BuildXYZField.h" |
| 20 |
#include "atlas/output/detail/GmshIO.h" |
| 21 |
#include "atlas/output/detail/GmshImpl.h" |
| 22 |
#include "atlas/runtime/Exception.h" |
| 23 |
#include "atlas/runtime/Log.h" |
| 24 |
|
| 25 |
namespace atlas { |
| 26 |
namespace output { |
| 27 |
namespace detail { |
| 28 |
|
| 29 |
// ----------------------------------------------------------------------------- |
| 30 |
|
| 31 |
void GmshImpl::defaults() { |
| 32 |
config_.binary = false; |
| 33 |
config_.nodes = "xy"; |
| 34 |
config_.gather = false; |
| 35 |
config_.ghost = false; |
| 36 |
config_.elements = true; |
| 37 |
config_.edges = false; |
| 38 |
config_.levels.clear(); |
| 39 |
config_.file = "output.msh"; |
| 40 |
config_.info = false; |
| 41 |
config_.openmode = "w"; |
| 42 |
config_.coordinates = "xy"; |
| 43 |
|
| 44 |
config_.configured_land_water = false; |
| 45 |
config_.land = true; |
| 46 |
config_.water = true; |
| 47 |
} |
| 48 |
|
| 49 |
// ----------------------------------------------------------------------------- |
| 50 |
|
| 51 |
namespace /*anonymous*/ { |
| 52 |
|
| 53 |
// ----------------------------------------------------------------------------- |
| 54 |
|
| 55 |
void merge(GmshImpl::Configuration& present, const eckit::Parametrisation& update) { |
| 56 |
update.get("binary", present.binary); |
| 57 |
update.get("nodes", present.nodes); |
| 58 |
update.get("gather", present.gather); |
| 59 |
update.get("ghost", present.ghost); |
| 60 |
update.get("elements", present.elements); |
| 61 |
update.get("edges", present.edges); |
| 62 |
update.get("levels", present.levels); |
| 63 |
update.get("file", present.file); |
| 64 |
update.get("info", present.info); |
| 65 |
update.get("openmode", present.openmode); |
| 66 |
update.get("coordinates", present.coordinates); |
| 67 |
if (update.has("water") || update.has("land")) { |
| 68 |
update.get("land", present.land); |
| 69 |
update.get("water", present.water); |
| 70 |
present.configured_land_water = true; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// ----------------------------------------------------------------------------- |
| 75 |
|
| 76 |
detail::GmshIO writer(const GmshImpl::Configuration& c) { |
| 77 |
detail::GmshIO gmsh; |
| 78 |
GmshImpl::setGmshConfiguration(gmsh, c); |
| 79 |
return gmsh; |
| 80 |
} |
| 81 |
|
| 82 |
// ----------------------------------------------------------------------------- |
| 83 |
|
| 84 |
std::ios_base::openmode openmode(const GmshImpl::Configuration& c) { |
| 85 |
std::ios_base::openmode omode(std::ios_base::out); |
| 86 |
if (std::string(c.openmode) == "w") { |
| 87 |
omode = std::ios_base::out; |
| 88 |
} |
| 89 |
else if (std::string(c.openmode) == "a") { |
| 90 |
omode = std::ios_base::app; |
| 91 |
} |
| 92 |
if (c.binary) { |
| 93 |
omode |= std::ios::binary; |
| 94 |
} |
| 95 |
return omode; |
| 96 |
} |
| 97 |
|
| 98 |
// ----------------------------------------------------------------------------- |
| 99 |
|
| 100 |
} // anonymous namespace |
| 101 |
|
| 102 |
// ----------------------------------------------------------------------------- |
| 103 |
|
| 104 |
void GmshImpl::setGmshConfiguration(detail::GmshIO& gmsh, const GmshImpl::Configuration& c) { |
| 105 |
gmsh.options.set("ascii", not c.binary); |
| 106 |
gmsh.options.set("nodes", c.nodes); |
| 107 |
gmsh.options.set("gather", c.gather); |
| 108 |
gmsh.options.set("ghost", c.ghost); |
| 109 |
gmsh.options.set("elements", c.elements); |
| 110 |
gmsh.options.set("edges", c.edges); |
| 111 |
gmsh.options.set("levels", c.levels); |
| 112 |
gmsh.options.set("info", c.info); |
| 113 |
gmsh.options.set("nodes", c.coordinates); |
| 114 |
if (c.configured_land_water) { |
| 115 |
gmsh.options.set("land", c.land); |
| 116 |
gmsh.options.set("water", c.water); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// ----------------------------------------------------------------------------- |
| 121 |
|
| 122 |
GmshImpl::GmshImpl(std::ostream&) { |
| 123 |
defaults(); |
| 124 |
ATLAS_NOTIMPLEMENTED; // JIRA issue ATLAS-254 |
| 125 |
} |
| 126 |
|
| 127 |
// ----------------------------------------------------------------------------- |
| 128 |
|
| 129 |
GmshImpl::GmshImpl(std::ostream&, const eckit::Parametrisation& config) { |
| 130 |
defaults(); |
| 131 |
merge(config_, config); |
| 132 |
ATLAS_NOTIMPLEMENTED; // JIRA issue ATLAS-254 |
| 133 |
} |
| 134 |
|
| 135 |
// ----------------------------------------------------------------------------- |
| 136 |
|
| 137 |
GmshImpl::GmshImpl(const eckit::PathName& file, const std::string& mode) { |
| 138 |
defaults(); |
| 139 |
config_.file = file.asString(); |
| 140 |
config_.openmode = std::string(mode); |
| 141 |
} |
| 142 |
|
| 143 |
// ----------------------------------------------------------------------------- |
| 144 |
|
| 145 |
GmshImpl::GmshImpl(const eckit::PathName& file, const std::string& mode, const eckit::Parametrisation& config) { |
| 146 |
defaults(); |
| 147 |
merge(config_, config); |
| 148 |
config_.file = file.asString(); |
| 149 |
config_.openmode = std::string(mode); |
| 150 |
} |
| 151 |
|
| 152 |
// ----------------------------------------------------------------------------- |
| 153 |
|
| 154 |
GmshImpl::GmshImpl(const eckit::PathName& file) { |
| 155 |
defaults(); |
| 156 |
config_.file = file.asString(); |
| 157 |
} |
| 158 |
|
| 159 |
// ----------------------------------------------------------------------------- |
| 160 |
|
| 161 |
GmshImpl::GmshImpl(const eckit::PathName& file, const eckit::Parametrisation& config) { |
| 162 |
defaults(); |
| 163 |
merge(config_, config); |
| 164 |
config_.file = file.asString(); |
| 165 |
} |
| 166 |
|
| 167 |
// ----------------------------------------------------------------------------- |
| 168 |
|
| 169 |
GmshImpl::~GmshImpl() = default; |
| 170 |
|
| 171 |
// ----------------------------------------------------------------------------- |
| 172 |
|
| 173 |
void GmshImpl::write(const Mesh& mesh, const eckit::Parametrisation& config) const { |
| 174 |
GmshImpl::Configuration c = config_; |
| 175 |
merge(c, config); |
| 176 |
|
| 177 |
if (c.coordinates == "xyz" and not mesh.nodes().has_field("xyz")) { |
| 178 |
Log::debug() << "Building xyz representation for nodes" << std::endl; |
| 179 |
mesh::actions::BuildXYZField("xyz")(const_cast<Mesh&>(mesh)); |
| 180 |
} |
| 181 |
|
| 182 |
writer(c).write(mesh, c.file); |
| 183 |
config_.openmode = "a"; |
| 184 |
} |
| 185 |
|
| 186 |
// ----------------------------------------------------------------------------- |
| 187 |
|
| 188 |
void GmshImpl::write(const Field& field, const eckit::Parametrisation& config) const { |
| 189 |
GmshImpl::Configuration c = config_; |
| 190 |
merge(c, config); |
| 191 |
writer(c).write(field, c.file, openmode(c)); |
| 192 |
config_.openmode = "a"; |
| 193 |
} |
| 194 |
|
| 195 |
// ----------------------------------------------------------------------------- |
| 196 |
|
| 197 |
void GmshImpl::write(const FieldSet& fields, const eckit::Parametrisation& config) const { |
| 198 |
GmshImpl::Configuration c = config_; |
| 199 |
merge(c, config); |
| 200 |
writer(c).write(fields, fields.field(0).functionspace(), c.file, openmode(c)); |
| 201 |
config_.openmode = "a"; |
| 202 |
} |
| 203 |
|
| 204 |
// ----------------------------------------------------------------------------- |
| 205 |
|
| 206 |
void GmshImpl::write(const Field& field, const FunctionSpace& functionspace, |
| 207 |
const eckit::Parametrisation& config) const { |
| 208 |
GmshImpl::Configuration c = config_; |
| 209 |
merge(c, config); |
| 210 |
writer(c).write(field, functionspace, c.file, openmode(c)); |
| 211 |
config_.openmode = "a"; |
| 212 |
} |
| 213 |
|
| 214 |
// ----------------------------------------------------------------------------- |
| 215 |
|
| 216 |
void GmshImpl::write(const FieldSet& fields, const FunctionSpace& functionspace, |
| 217 |
const eckit::Parametrisation& config) const { |
| 218 |
GmshImpl::Configuration c = config_; |
| 219 |
merge(c, config); |
| 220 |
writer(c).write(fields, functionspace, c.file, openmode(c)); |
| 221 |
config_.openmode = "a"; |
| 222 |
} |
| 223 |
|
| 224 |
// ----------------------------------------------------------------------------- |
| 225 |
|
| 226 |
static OutputBuilder<detail::GmshImpl> __gmsh("gmsh"); |
| 227 |
|
| 228 |
// ----------------------------------------------------------------------------- |
| 229 |
|
| 230 |
} // namespace detail |
| 231 |
} // namespace output |
| 232 |
} // namespace atlas |