| 1 |
// Copyright(C) 1999-2021, 2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "Ioss_Field.h" |
| 8 |
#include "Ioss_VariableType.h" |
| 9 |
#include "transform/Iotr_VectorMagnitude.h" |
| 10 |
#include <cmath> |
| 11 |
#include <cstddef> |
| 12 |
|
| 13 |
#include "Ioss_Transform.h" |
| 14 |
#include "Ioss_TransformFactory.h" |
| 15 |
|
| 16 |
namespace Iotr { |
| 17 |
|
| 18 |
const VM_Factory *VM_Factory::factory() |
| 19 |
{ |
| 20 |
static VM_Factory registerThis; |
| 21 |
return ®isterThis; |
| 22 |
} |
| 23 |
|
| 24 |
VM_Factory::VM_Factory() : Ioss::TransformFactory("vector magnitude") |
| 25 |
{ |
| 26 |
Ioss::TransformFactory::alias("vector magnitude", "length"); |
| 27 |
} |
| 28 |
|
| 29 |
Ioss::Transform *VM_Factory::make(const std::string & /*unused*/) const |
| 30 |
{ |
| 31 |
return new VectorMagnitude(); |
| 32 |
} |
| 33 |
|
| 34 |
VectorMagnitude::VectorMagnitude() = default; |
| 35 |
|
| 36 |
const Ioss::VariableType *VectorMagnitude::output_storage(const Ioss::VariableType *in) const |
| 37 |
{ |
| 38 |
static const Ioss::VariableType *v2d = Ioss::VariableType::factory("vector_2d"); |
| 39 |
static const Ioss::VariableType *v3d = Ioss::VariableType::factory("vector_3d"); |
| 40 |
static const Ioss::VariableType *sca = Ioss::VariableType::factory("scalar"); |
| 41 |
if (in == v2d || in == v3d) { |
| 42 |
return sca; |
| 43 |
} |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
|
| 47 |
size_t VectorMagnitude::output_count(size_t in) const |
| 48 |
{ |
| 49 |
// Does not modify the entity count... |
| 50 |
return in; |
| 51 |
} |
| 52 |
|
| 53 |
bool VectorMagnitude::internal_execute(const Ioss::Field &field, void *data) |
| 54 |
{ |
| 55 |
auto *rdata = static_cast<double *>(data); |
| 56 |
|
| 57 |
size_t count = field.transformed_count(); |
| 58 |
if (field.transformed_storage()->component_count() == 3) { |
| 59 |
int j = 0; |
| 60 |
for (size_t i = 0; i < count; i++) { |
| 61 |
rdata[i] = std::sqrt(rdata[j] * rdata[j] + rdata[j + 1] * rdata[j + 1] + |
| 62 |
rdata[j + 2] * rdata[j + 2]); |
| 63 |
j += 3; |
| 64 |
} |
| 65 |
} |
| 66 |
else { |
| 67 |
int j = 0; |
| 68 |
for (size_t i = 0; i < count; i++) { |
| 69 |
rdata[i] = std::sqrt(rdata[j] * rdata[j] + rdata[j + 1] * rdata[j + 1]); |
| 70 |
j += 2; |
| 71 |
} |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
} // namespace Iotr |