Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
field_layout_helpers.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2025 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <utils/Vector.hpp>
23#include <utils/index.hpp>
24
25#include <span>
26#include <vector>
27
28// Function to extract a 3D block from the halo field
29template <typename Container>
30auto extract_block(Container const &in_array, Utils::Vector3i const &dimensions,
31 Utils::Vector3i const &start, Utils::Vector3i const &stop,
32 Utils::MemoryOrder memory_order,
33 Utils::MemoryOrder output_memory_order) {
34 // Calculate the size of the block excluding halo regions
35 auto const block_dim = stop - start;
36 auto const size = Utils::product(block_dim);
37
38 // Output vector to hold the block
39 std::vector<typename Container::value_type> out_array(size);
40
41 // Extract the block
42 for (int x = 0; x < block_dim[0]; ++x) {
43 for (int y = 0; y < block_dim[1]; ++y) {
44 for (int z = 0; z < block_dim[2]; ++z) {
45 // Compute indices for input and output arrays
46 auto const in_index = Utils::get_linear_index(
47 x + start[0], y + start[1], z + start[2], dimensions, memory_order);
48
49 auto const out_index =
50 Utils::get_linear_index(x, y, z, block_dim, output_memory_order);
51
52 // Copy the value
53 out_array[out_index] = in_array[in_index];
54 }
55 }
56 }
57
58 return out_array;
59}
60
61// Function to pad the 3D cropped field with zeros to restore the halo regions
62template <typename T>
63auto pad_with_zeros_discard_imag(std::span<T> cropped_array,
64 Utils::Vector3i cropped_dim,
65 Utils::Vector3i pad_left,
66 Utils::Vector3i pad_right) {
67
68 // Calculate dimensions and strides
69 Utils::Vector3i padded_dim = cropped_dim + pad_left + pad_right;
70 auto const cropped_xy_stride = cropped_dim[1] * cropped_dim[2];
71 auto const padded_xy_stride = padded_dim[1] * padded_dim[2];
72
73 // Output vector to hold the padded field (initialized with zeros)
74 std::vector<typename T::value_type> padded_array(
75 padded_dim[0] * padded_dim[1] * padded_dim[2]);
76
77 // Calculate the starting position in the padded array for the inner field
78 auto const padded_start_x = pad_left[0] * padded_xy_stride;
79 auto const padded_start_y = pad_left[1] * padded_dim[2] + pad_left[2];
80
81 // Fill in the original cropped field into the padded array by chunks
82 for (int x = 0; x < cropped_dim[0]; ++x) {
83 auto const cropped_x_offset = x * cropped_xy_stride;
84 auto const padded_x_offset = padded_start_x + x * padded_xy_stride;
85
86 for (int y = 0; y < cropped_dim[1]; ++y) {
87 auto const cropped_y_offset = cropped_x_offset + y * cropped_dim[2];
88 auto const padded_y_offset =
89 padded_x_offset + y * padded_dim[2] + padded_start_y;
90
91 // Copy a contiguous slice of the z-dimension at once
92 for (int i = 0; i < cropped_dim[2]; i++) {
93 padded_array[padded_y_offset + i] =
94 cropped_array[cropped_y_offset + i].real();
95 }
96 }
97 }
98
99 return padded_array;
100}
Vector implementation and trait types for boost qvm interoperability.
auto pad_with_zeros_discard_imag(std::span< T > cropped_array, Utils::Vector3i cropped_dim, Utils::Vector3i pad_left, Utils::Vector3i pad_right)
auto extract_block(Container const &in_array, Utils::Vector3i const &dimensions, Utils::Vector3i const &start, Utils::Vector3i const &stop, Utils::MemoryOrder memory_order, Utils::MemoryOrder output_memory_order)
T product(Vector< T, N > const &v)
Definition Vector.hpp:375
int get_linear_index(int a, int b, int c, const Vector3i &adim, MemoryOrder memory_order=MemoryOrder::COLUMN_MAJOR)
get the linear index from the position (a,b,c) in a 3D grid of dimensions adim.
Definition index.hpp:43
MemoryOrder
Definition index.hpp:32