From e952090de92bef5cdb7c14b0aa2119efeaf99f6a Mon Sep 17 00:00:00 2001 From: Ben Dudson Date: Wed, 11 Mar 2026 11:06:15 -0700 Subject: [PATCH 1/4] Options: Convert Tensor to Tensor and similar Read Tensor from NetCDF files, and allow conversion to Tensor and then Field3D. --- include/bout/array.hxx | 8 ++++++++ include/bout/utils.hxx | 18 +++++++++++++++++- src/sys/options.cxx | 29 ++++++++++++++++++++++++----- src/sys/options/options_netcdf.cxx | 6 ++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/include/bout/array.hxx b/include/bout/array.hxx index 2305fe3b22..000ded9616 100644 --- a/include/bout/array.hxx +++ b/include/bout/array.hxx @@ -227,6 +227,14 @@ public: ptr = get(new_size); } + /*! + * Change shape of the container. + * Invalidates contents. + */ + void reshape(std::tuple new_shape) { + reallocate(std::get<0>(new_shape)); + } + /*! * Holds a static variable which controls whether * memory blocks (dataBlock) are put into a store diff --git a/include/bout/utils.hxx b/include/bout/utils.hxx index 560f97f954..5088a025c1 100644 --- a/include/bout/utils.hxx +++ b/include/bout/utils.hxx @@ -5,7 +5,7 @@ * simple but common calculations * ************************************************************************** - * Copyright 2010 - 2025 BOUT++ contributors + * Copyright 2010 - 2026 BOUT++ contributors * * Contact: Ben Dudson, dudson2@llnl.gov * @@ -231,6 +231,14 @@ public: data.reallocate(new_size_1 * new_size_2); } + /*! + * Change shape of the container. + * Invalidates contents. + */ + void reshape(std::tuple new_shape) { + reallocate(std::get<0>(new_shape), std::get<1>(new_shape)); + } + Matrix& operator=(const Matrix& other) { n1 = other.n1; n2 = other.n2; @@ -331,6 +339,14 @@ public: data.reallocate(new_size_1 * new_size_2 * new_size_3); } + /*! + * Change shape of the container. + * Invalidates contents. + */ + void reshape(std::tuple new_shape) { + reallocate(std::get<0>(new_shape), std::get<1>(new_shape), std::get<2>(new_shape)); + } + Tensor& operator=(const Tensor& other) { n1 = other.n1; n2 = other.n2; diff --git a/src/sys/options.cxx b/src/sys/options.cxx index d60e91079d..8bd83c0bfb 100644 --- a/src/sys/options.cxx +++ b/src/sys/options.cxx @@ -744,27 +744,46 @@ FieldPerp Options::as(const FieldPerp& similar_to) const { } namespace { -/// Visitor to convert an int, BoutReal or Array/Matrix/Tensor to the -/// appropriate container +/// Primary declaration of ConvertContainer, for specialization below. +/// No definition needed unless it is used. template -struct ConvertContainer { +struct ConvertContainer; + +/// Visitor to convert an int, BoutReal or Array/Matrix/Tensor to the +/// appropriate container. Templated on both the container class C +/// and scalar type Scalar. +template