| 1 |
/* |
| 2 |
* (C) Crown Copyright 2024 Met Office |
| 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 |
*/ |
| 7 |
|
| 8 |
#include "atlas/util/PackVectorFields.h" |
| 9 |
|
| 10 |
#include <map> |
| 11 |
#include <string> |
| 12 |
#include <type_traits> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include "atlas/array.h" |
| 16 |
#include "atlas/array/helpers/ArrayForEach.h" |
| 17 |
#include "atlas/functionspace.h" |
| 18 |
#include "atlas/option.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
#include "atlas/util/Config.h" |
| 21 |
#include "eckit/config/LocalConfiguration.h" |
| 22 |
|
| 23 |
namespace atlas { |
| 24 |
namespace util { |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
using array::helpers::arrayForEachDim; |
| 29 |
using eckit::LocalConfiguration; |
| 30 |
|
| 31 |
void addOrReplaceField(FieldSet& fieldSet, const Field& field) { |
| 32 |
const auto fieldName = field.name(); |
| 33 |
if (fieldSet.has(fieldName)) { |
| 34 |
fieldSet[fieldName] = field; |
| 35 |
} |
| 36 |
else { |
| 37 |
fieldSet.add(field); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
Field& getOrCreateField(FieldSet& fieldSet, const FunctionSpace& functionSpace, const Config& config) { |
| 42 |
const auto fieldName = config.getString("name"); |
| 43 |
if (!fieldSet.has(fieldName)) { |
| 44 |
auto field = functionSpace.createField(config); |
| 45 |
field->set_dirty(false); // We will inherit the dirty state from the component fields. |
| 46 |
fieldSet.add(field); |
| 47 |
} |
| 48 |
return fieldSet[fieldName]; |
| 49 |
} |
| 50 |
|
| 51 |
void checkFieldCompatibility(const Field& componentField, const Field& vectorField) { |
| 52 |
ATLAS_ASSERT(componentField.functionspace().size() == vectorField.functionspace().size()); |
| 53 |
ATLAS_ASSERT(componentField.levels() == vectorField.levels()); |
| 54 |
ATLAS_ASSERT(componentField.variables() == 0); |
| 55 |
ATLAS_ASSERT(vectorField.variables() > 0); |
| 56 |
|
| 57 |
const auto checkStandardShape = [](const Field& field) { |
| 58 |
// Check for "standard" Atlas field shape. |
| 59 |
auto dim = 0; |
| 60 |
const auto rank = field.rank(); |
| 61 |
const auto shape = field.shape(); |
| 62 |
if (field.functionspace().size() != shape[dim++]) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
if (const auto levels = field.levels(); levels && (dim >= rank || levels != shape[dim++])) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
if (const auto variables = field.variables(); variables && (dim >= rank || variables != shape[dim++])) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
if (dim != rank) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
return true; |
| 75 |
}; |
| 76 |
|
| 77 |
ATLAS_ASSERT(checkStandardShape(componentField)); |
| 78 |
ATLAS_ASSERT(checkStandardShape(vectorField)); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename ComponentField, typename VectorField, typename Functor> |
| 82 |
void copyFieldData(ComponentField& componentField, VectorField& vectorField, const Functor& copier) { |
| 83 |
checkFieldCompatibility(componentField, vectorField); |
| 84 |
|
| 85 |
auto componentViewVariant = array::make_view_variant(componentField); |
| 86 |
|
| 87 |
const auto componentVisitor = [&](auto componentView) { |
| 88 |
if constexpr (array::is_rank<1, 2>(componentView)) { |
| 89 |
using ComponentView = std::decay_t<decltype(componentView)>; |
| 90 |
constexpr auto ComponentRank = ComponentView::rank(); |
| 91 |
using Value = typename ComponentView::non_const_value_type; |
| 92 |
|
| 93 |
auto vectorView = array::make_view<Value, ComponentRank + 1>(vectorField); |
| 94 |
constexpr auto Dims = std::make_integer_sequence<int, ComponentRank>{}; |
| 95 |
arrayForEachDim(Dims, execution::par, std::tie(componentView, vectorView), copier); |
| 96 |
} |
| 97 |
else { |
| 98 |
ATLAS_THROW_EXCEPTION("Unsupported component field rank: " + std::to_string(componentView.rank())); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
std::visit(componentVisitor, componentViewVariant); |
| 103 |
} |
| 104 |
|
| 105 |
} // namespace |
| 106 |
|
| 107 |
FieldSet pack_vector_fields(const FieldSet& fields, FieldSet packedFields) { |
| 108 |
// Get the number of variables for each vector field. |
| 109 |
auto vectorSizeMap = std::map<std::string, idx_t>{}; |
| 110 |
for (const auto& field : fields) { |
| 111 |
auto vectorFieldName = std::string{}; |
| 112 |
if (field.metadata().get("vector_component.vector_field_name", vectorFieldName)) { |
| 113 |
++vectorSizeMap[vectorFieldName]; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Pack vector fields. |
| 118 |
for (const auto& field : fields) { |
| 119 |
auto vectorFieldName = std::string{}; |
| 120 |
if (!field.metadata().get("vector_component.vector_field_name", vectorFieldName)) { |
| 121 |
// Not a vector component field. |
| 122 |
addOrReplaceField(packedFields, field); |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
// Field is vector field component. |
| 127 |
auto vectorSize = vectorSizeMap[vectorFieldName]; |
| 128 |
auto vectorIndex = size_t{}; |
| 129 |
field.metadata().get("vector_component.index", vectorIndex); |
| 130 |
const auto& componentField = field; |
| 131 |
|
| 132 |
// Get or create vector field. |
| 133 |
const auto vectorFieldConfig = option::name(vectorFieldName) | option::levels(componentField.levels()) | |
| 134 |
option::vector(vectorSize) | option::datatype(componentField.datatype()); |
| 135 |
auto& vectorField = getOrCreateField(packedFields, componentField.functionspace(), vectorFieldConfig); |
| 136 |
|
| 137 |
// Copy field data. |
| 138 |
const auto copier = [&](auto&& componentElem, auto&& vectorElem) { vectorElem(vectorIndex) = componentElem; }; |
| 139 |
copyFieldData(componentField, vectorField, copier); |
| 140 |
|
| 141 |
// Copy metadata. |
| 142 |
const auto componentFieldMetadata = componentField.metadata(); |
| 143 |
auto componentFieldMetadataVector = std::vector<LocalConfiguration>(vectorSize); |
| 144 |
vectorField.metadata().get("component_field_metadata", componentFieldMetadataVector); |
| 145 |
componentFieldMetadataVector[vectorIndex] = componentFieldMetadata; |
| 146 |
vectorField.metadata().set("component_field_metadata", componentFieldMetadataVector); |
| 147 |
|
| 148 |
// If any component is dirty, the whole field is dirty. |
| 149 |
vectorField.set_dirty(vectorField.dirty() || componentField.dirty()); |
| 150 |
} |
| 151 |
return packedFields; |
| 152 |
} |
| 153 |
|
| 154 |
FieldSet unpack_vector_fields(const FieldSet& fields, FieldSet unpackedFields) { |
| 155 |
for (const auto& field : fields) { |
| 156 |
auto componentFieldMetadataVector = std::vector<LocalConfiguration>{}; |
| 157 |
if (!field.metadata().get("component_field_metadata", componentFieldMetadataVector)) { |
| 158 |
// Not a vector field. |
| 159 |
addOrReplaceField(unpackedFields, field); |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
// Field is vector. |
| 164 |
const auto& vectorField = field; |
| 165 |
|
| 166 |
auto vectorIndex = 0; |
| 167 |
for (const auto& componentFieldMetadata : componentFieldMetadataVector) { |
| 168 |
// Get or create field. |
| 169 |
auto componentFieldName = std::string{}; |
| 170 |
componentFieldMetadata.get("name", componentFieldName); |
| 171 |
const auto componentFieldConfig = option::name(componentFieldName) | option::levels(vectorField.levels()) | |
| 172 |
option::datatype(vectorField.datatype()); |
| 173 |
auto& componentField = getOrCreateField(unpackedFields, vectorField.functionspace(), componentFieldConfig); |
| 174 |
|
| 175 |
// Copy field data. |
| 176 |
const auto copier = [&](auto&& componentElem, auto&& vectorElem) { |
| 177 |
componentElem = vectorElem(vectorIndex); |
| 178 |
}; |
| 179 |
copyFieldData(componentField, vectorField, copier); |
| 180 |
|
| 181 |
// Copy metadata. |
| 182 |
componentField.metadata() = componentFieldMetadata; |
| 183 |
componentField.set_dirty(vectorField.dirty()); |
| 184 |
|
| 185 |
++vectorIndex; |
| 186 |
} |
| 187 |
} |
| 188 |
return unpackedFields; |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace util |
| 192 |
} // namespace atlas |