| 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 <algorithm> |
| 12 |
|
| 13 |
#include "atlas/array.h" |
| 14 |
#include "atlas/field/Field.h" |
| 15 |
#include "atlas/mesh/Connectivity.h" |
| 16 |
#include "atlas/mesh/Nodes.h" |
| 17 |
#include "atlas/parallel/mpi/mpi.h" |
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
#include "atlas/runtime/Log.h" |
| 20 |
#include "atlas/util/CoordinateEnums.h" |
| 21 |
|
| 22 |
using atlas::array::make_datatype; |
| 23 |
using atlas::array::make_shape; |
| 24 |
|
| 25 |
namespace atlas { |
| 26 |
namespace mesh { |
| 27 |
|
| 28 |
//------------------------------------------------------------------------------------------------------ |
| 29 |
|
| 30 |
Nodes::Nodes(): size_(0) { |
| 31 |
global_index_ = add(Field("glb_idx", make_datatype<gidx_t>(), make_shape(size()))); |
| 32 |
remote_index_ = add(Field("remote_idx", make_datatype<idx_t>(), make_shape(size()))); |
| 33 |
partition_ = add(Field("partition", make_datatype<int>(), make_shape(size()))); |
| 34 |
xy_ = add(Field("xy", make_datatype<double>(), make_shape(size(), 2))); |
| 35 |
xy_.set_variables(2); |
| 36 |
lonlat_ = add(Field("lonlat", make_datatype<double>(), make_shape(size(), 2))); |
| 37 |
lonlat_.set_variables(2); |
| 38 |
ghost_ = add(Field("ghost", make_datatype<int>(), make_shape(size()))); |
| 39 |
flags_ = add(Field("flags", make_datatype<int>(), make_shape(size()))); |
| 40 |
halo_ = add(Field("halo", make_datatype<int>(), make_shape(size()))); |
| 41 |
|
| 42 |
edge_connectivity_ = &add(new Connectivity("edge")); |
| 43 |
cell_connectivity_ = &add(new Connectivity("cell")); |
| 44 |
} |
| 45 |
|
| 46 |
Nodes::Connectivity& Nodes::add(Connectivity* connectivity) { |
| 47 |
connectivities_[connectivity->name()] = connectivity; |
| 48 |
return *connectivity; |
| 49 |
} |
| 50 |
|
| 51 |
Field Nodes::add(const Field& field) { |
| 52 |
ATLAS_ASSERT(field); |
| 53 |
ATLAS_ASSERT(!field.name().empty()); |
| 54 |
|
| 55 |
if (has_field(field.name())) { |
| 56 |
std::stringstream msg; |
| 57 |
msg << "Trying to add field '" << field.name() << "' to Nodes, but Nodes already has a field with this name."; |
| 58 |
throw_Exception(msg.str(), Here()); |
| 59 |
} |
| 60 |
fields_[field.name()] = field; |
| 61 |
return field; |
| 62 |
} |
| 63 |
|
| 64 |
void Nodes::remove_field(const std::string& name) { |
| 65 |
if (!has_field(name)) { |
| 66 |
std::stringstream msg; |
| 67 |
msg << "Trying to remove field `" << name << "' in Nodes, but no field with this name is present in Nodes."; |
| 68 |
throw_Exception(msg.str(), Here()); |
| 69 |
} |
| 70 |
fields_.erase(name); |
| 71 |
} |
| 72 |
|
| 73 |
const Field& Nodes::field(const std::string& name) const { |
| 74 |
if (!has_field(name)) { |
| 75 |
std::stringstream msg; |
| 76 |
msg << "Trying to access field `" << name << "' in Nodes, but no field with this name is present in Nodes."; |
| 77 |
throw_Exception(msg.str(), Here()); |
| 78 |
} |
| 79 |
return fields_.find(name)->second; |
| 80 |
} |
| 81 |
|
| 82 |
Field& Nodes::field(const std::string& name) { |
| 83 |
return const_cast<Field&>(static_cast<const Nodes*>(this)->field(name)); |
| 84 |
} |
| 85 |
|
| 86 |
void Nodes::resize(idx_t size) { |
| 87 |
if (size != size_) { |
| 88 |
idx_t previous_size = size_; |
| 89 |
size_ = size; |
| 90 |
for (FieldMap::iterator it = fields_.begin(); it != fields_.end(); ++it) { |
| 91 |
Field& field = it->second; |
| 92 |
array::ArrayShape shape = field.shape(); |
| 93 |
shape[0] = size_; |
| 94 |
field.resize(shape); |
| 95 |
} |
| 96 |
|
| 97 |
auto glb_idx = array::make_view<gidx_t, 1>(global_index()); |
| 98 |
auto part = array::make_view<int, 1>(partition()); |
| 99 |
auto flag = array::make_view<int, 1>(flags()); |
| 100 |
auto _halo = array::make_view<int, 1>(halo()); |
| 101 |
|
| 102 |
const int mpi_rank = static_cast<int>(mpi::rank()); |
| 103 |
for (idx_t n = previous_size; n < size_; ++n) { |
| 104 |
glb_idx(n) = 1 + n; |
| 105 |
part(n) = mpi_rank; |
| 106 |
flag(n) = 0; |
| 107 |
_halo(n) = std::numeric_limits<int>::max(); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
const Field& Nodes::field(idx_t idx) const { |
| 113 |
ATLAS_ASSERT(idx < nb_fields()); |
| 114 |
idx_t c(0); |
| 115 |
for (FieldMap::const_iterator it = fields_.begin(); it != fields_.end(); ++it) { |
| 116 |
if (idx == c) { |
| 117 |
const Field& field = it->second; |
| 118 |
return field; |
| 119 |
} |
| 120 |
c++; |
| 121 |
} |
| 122 |
throw_Exception("Should not be here!", Here()); |
| 123 |
static Field f; |
| 124 |
return f; |
| 125 |
} |
| 126 |
Field& Nodes::field(idx_t idx) { |
| 127 |
return const_cast<Field&>(static_cast<const Nodes*>(this)->field(idx)); |
| 128 |
} |
| 129 |
|
| 130 |
void Nodes::print(std::ostream& os) const { |
| 131 |
os << "Nodes[\n"; |
| 132 |
os << "\t size=" << size() << ",\n"; |
| 133 |
os << "\t fields=\n"; |
| 134 |
for (idx_t i = 0; i < nb_fields(); ++i) { |
| 135 |
os << "\t\t" << field(i); |
| 136 |
if (i != nb_fields() - 1) { |
| 137 |
os << ","; |
| 138 |
} |
| 139 |
os << "\n"; |
| 140 |
} |
| 141 |
os << "]"; |
| 142 |
} |
| 143 |
|
| 144 |
size_t Nodes::footprint() const { |
| 145 |
size_t size = sizeof(*this); |
| 146 |
for (FieldMap::const_iterator it = fields_.begin(); it != fields_.end(); ++it) { |
| 147 |
size += (*it).second.footprint(); |
| 148 |
} |
| 149 |
for (ConnectivityMap::const_iterator it = connectivities_.begin(); it != connectivities_.end(); ++it) { |
| 150 |
size += (*it).second->footprint(); |
| 151 |
} |
| 152 |
size += metadata_.footprint(); |
| 153 |
return size; |
| 154 |
} |
| 155 |
|
| 156 |
const IrregularConnectivity& Nodes::connectivity(const std::string& name) const { |
| 157 |
if ((connectivities_.find(name) == connectivities_.end())) { |
| 158 |
std::stringstream msg; |
| 159 |
msg << "Trying to access connectivity `" << name |
| 160 |
<< "' in Nodes, but no connectivity with this name is present in " |
| 161 |
"Nodes."; |
| 162 |
throw_Exception(msg.str(), Here()); |
| 163 |
} |
| 164 |
return *connectivities_.find(name)->second; |
| 165 |
} |
| 166 |
IrregularConnectivity& Nodes::connectivity(const std::string& name) { |
| 167 |
if ((connectivities_.find(name) == connectivities_.end())) { |
| 168 |
std::stringstream msg; |
| 169 |
msg << "Trying to access connectivity `" << name |
| 170 |
<< "' in Nodes, but no connectivity with this name is present in " |
| 171 |
"Nodes."; |
| 172 |
throw_Exception(msg.str(), Here()); |
| 173 |
} |
| 174 |
return *connectivities_.find(name)->second; |
| 175 |
} |
| 176 |
|
| 177 |
void Nodes::updateDevice() const { |
| 178 |
std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.updateDevice(); }); |
| 179 |
} |
| 180 |
|
| 181 |
void Nodes::updateHost() const { |
| 182 |
std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.updateHost(); }); |
| 183 |
} |
| 184 |
|
| 185 |
void Nodes::syncHostDevice() const { |
| 186 |
std::for_each(fields_.begin(), fields_.end(), [](const FieldMap::value_type& v) { v.second.syncHostDevice(); }); |
| 187 |
} |
| 188 |
|
| 189 |
//----------------------------------------------------------------------------- |
| 190 |
|
| 191 |
extern "C" { |
| 192 |
|
| 193 |
Nodes* atlas__mesh__Nodes__create() { |
| 194 |
return new Nodes(); |
| 195 |
} |
| 196 |
|
| 197 |
void atlas__mesh__Nodes__delete(Nodes* This) { |
| 198 |
ATLAS_ASSERT(This != nullptr); |
| 199 |
} |
| 200 |
|
| 201 |
idx_t atlas__mesh__Nodes__size(Nodes* This) { |
| 202 |
ATLAS_ASSERT(This != nullptr); |
| 203 |
return This->size(); |
| 204 |
} |
| 205 |
void atlas__mesh__Nodes__resize(Nodes* This, idx_t size) { |
| 206 |
ATLAS_ASSERT(This != nullptr); |
| 207 |
This->resize(size); |
| 208 |
} |
| 209 |
idx_t atlas__mesh__Nodes__nb_fields(Nodes* This) { |
| 210 |
ATLAS_ASSERT(This != nullptr); |
| 211 |
return This->nb_fields(); |
| 212 |
} |
| 213 |
|
| 214 |
void atlas__mesh__Nodes__add_field(Nodes* This, field::FieldImpl* field) { |
| 215 |
ATLAS_ASSERT(This != nullptr); |
| 216 |
ATLAS_ASSERT(field != nullptr); |
| 217 |
This->add(field); |
| 218 |
} |
| 219 |
|
| 220 |
void atlas__mesh__Nodes__remove_field(Nodes* This, char* name) { |
| 221 |
ATLAS_ASSERT(This != nullptr); |
| 222 |
This->remove_field(std::string(name)); |
| 223 |
} |
| 224 |
|
| 225 |
int atlas__mesh__Nodes__has_field(Nodes* This, char* name) { |
| 226 |
ATLAS_ASSERT(This != nullptr); |
| 227 |
return This->has_field(std::string(name)); |
| 228 |
} |
| 229 |
|
| 230 |
field::FieldImpl* atlas__mesh__Nodes__field_by_name(Nodes* This, char* name) { |
| 231 |
ATLAS_ASSERT(This != nullptr); |
| 232 |
return This->field(std::string(name)).get(); |
| 233 |
} |
| 234 |
|
| 235 |
field::FieldImpl* atlas__mesh__Nodes__field_by_idx(Nodes* This, idx_t idx) { |
| 236 |
ATLAS_ASSERT(This != nullptr); |
| 237 |
return This->field(idx).get(); |
| 238 |
} |
| 239 |
|
| 240 |
util::Metadata* atlas__mesh__Nodes__metadata(Nodes* This) { |
| 241 |
ATLAS_ASSERT(This != nullptr); |
| 242 |
return &This->metadata(); |
| 243 |
} |
| 244 |
|
| 245 |
void atlas__mesh__Nodes__str(Nodes* This, char*& str, int& size) { |
| 246 |
ATLAS_ASSERT(This != nullptr); |
| 247 |
std::stringstream ss; |
| 248 |
ss << *This; |
| 249 |
std::string s = ss.str(); |
| 250 |
size = static_cast<int>(s.size()); |
| 251 |
str = new char[size + 1]; |
| 252 |
std::strncpy(str, s.c_str(), size + 1); |
| 253 |
} |
| 254 |
|
| 255 |
IrregularConnectivity* atlas__mesh__Nodes__edge_connectivity(Nodes* This) { |
| 256 |
ATLAS_ASSERT(This != nullptr); |
| 257 |
return &This->edge_connectivity(); |
| 258 |
} |
| 259 |
|
| 260 |
IrregularConnectivity* atlas__mesh__Nodes__cell_connectivity(Nodes* This) { |
| 261 |
ATLAS_ASSERT(This != nullptr); |
| 262 |
return &This->cell_connectivity(); |
| 263 |
} |
| 264 |
|
| 265 |
IrregularConnectivity* atlas__mesh__Nodes__connectivity(Nodes* This, char* name) { |
| 266 |
ATLAS_ASSERT(This != nullptr); |
| 267 |
return &This->connectivity(std::string(name)); |
| 268 |
} |
| 269 |
|
| 270 |
void atlas__mesh__Nodes__add_connectivity(Nodes* This, IrregularConnectivity* connectivity) { |
| 271 |
ATLAS_ASSERT(This != nullptr); |
| 272 |
ATLAS_ASSERT(connectivity != nullptr); |
| 273 |
This->add(connectivity); |
| 274 |
} |
| 275 |
|
| 276 |
field::FieldImpl* atlas__mesh__Nodes__xy(Nodes* This) { |
| 277 |
ATLAS_ASSERT(This != nullptr); |
| 278 |
return This->xy().get(); |
| 279 |
} |
| 280 |
|
| 281 |
field::FieldImpl* atlas__mesh__Nodes__lonlat(Nodes* This) { |
| 282 |
ATLAS_ASSERT(This != nullptr); |
| 283 |
return This->lonlat().get(); |
| 284 |
} |
| 285 |
|
| 286 |
field::FieldImpl* atlas__mesh__Nodes__global_index(Nodes* This) { |
| 287 |
ATLAS_ASSERT(This != nullptr); |
| 288 |
return This->global_index().get(); |
| 289 |
} |
| 290 |
|
| 291 |
field::FieldImpl* atlas__mesh__Nodes__remote_index(Nodes* This) { |
| 292 |
ATLAS_ASSERT(This != nullptr); |
| 293 |
return This->remote_index().get(); |
| 294 |
} |
| 295 |
|
| 296 |
field::FieldImpl* atlas__mesh__Nodes__partition(Nodes* This) { |
| 297 |
ATLAS_ASSERT(This != nullptr); |
| 298 |
return This->partition().get(); |
| 299 |
} |
| 300 |
|
| 301 |
field::FieldImpl* atlas__mesh__Nodes__ghost(Nodes* This) { |
| 302 |
ATLAS_ASSERT(This != nullptr); |
| 303 |
return This->ghost().get(); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
} // namespace mesh |
| 308 |
} // namespace atlas |