TDMS
Time Domain Maxwell Solver
All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
arrays.h
Go to the documentation of this file.
1/**
2 * @file arrays.h
3 * @brief Classes describing arrays, vertices etc.
4 */
5#pragma once
6
7#include <complex>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12#include <fftw3.h>
13
14#include "globals.h"
15#include "matlabio.h"
16#include "utils.h"
17
18template<typename T>
20public:
21 T ***x = nullptr;
22 T ***y = nullptr;
23 T ***z = nullptr;
24
25 T ***operator[](char c) const {
26 switch (c) {
27 case 'x':
28 return x;
29 case 'y':
30 return y;
31 case 'z':
32 return z;
33 default:
34 throw std::runtime_error("Have no element " + std::string(1, c));
35 }
36 }
37 T ***operator[](AxialDirection d) const {
38 switch (d) {
39 case AxialDirection::X:
40 return x;
41 case AxialDirection::Y:
42 return y;
43 case AxialDirection::Z:
44 return z;
45 default:
46 throw std::runtime_error("Have no element " + std::to_string(d));
47 }
48 }
49
50 /**
51 * @brief Allocates x, y, and z as (K_total+1) * (J_total+1) * (I_total+1)
52 * arrays
53 *
54 * @param I_total,J_total,K_total Dimensions of the tensor size to set
55 */
56 void allocate(int I_total, int J_total, int K_total) {
57 x = (T ***) malloc(K_total * sizeof(T **));
58 y = (T ***) malloc(K_total * sizeof(T **));
59 z = (T ***) malloc(K_total * sizeof(T **));
60 for (int k = 0; k < K_total; k++) {
61 x[k] = (T **) malloc(J_total * sizeof(T *));
62 y[k] = (T **) malloc(J_total * sizeof(T *));
63 z[k] = (T **) malloc(J_total * sizeof(T *));
64 for (int j = 0; j < J_total; j++) {
65 x[k][j] = (T *) malloc(I_total * sizeof(T));
66 y[k][j] = (T *) malloc(I_total * sizeof(T));
67 z[k][j] = (T *) malloc(I_total * sizeof(T));
68 }
69 }
70 }
71};
72
73template<typename T>
74class Vector {
75protected:
76 int n = 0; // Number of elements
77 T *vector = nullptr;// Internal array
78
79public:
80 Vector() = default;
81
82 explicit Vector(const mxArray *ptr) {
83 n = (int) mxGetNumberOfElements(ptr);
84 vector = (T *) malloc((unsigned) (n * sizeof(T)));
85
86 auto matlab_ptr = mxGetPr(ptr);
87 for (int i = 0; i < n; i++) { vector[i] = (T) matlab_ptr[i]; }
88 }
89
90 bool has_elements() { return vector != nullptr; }
91
92 inline T operator[](int value) const { return vector[value]; };
93
94 inline int size() const { return n; };
95};
96
98 std::vector<double> x;
99 std::vector<double> y;
100};
101
103public:
104 fftw_complex *v = nullptr; // Flat fftw vector
105 fftw_plan plan = nullptr; // fftw plan for the setup
106 std::complex<double> **cm = nullptr;// Column major matrix
107
108 void initialise(int n_rows, int n_cols);
109
111};
112
113/**
114 * Container for storing snapshots of the full-field
115 */
117public:
118 std::complex<double> Ex = 0.;//< x-component of the electric field
119 std::complex<double> Ey = 0.;//< y-component of the electric field
120 std::complex<double> Ez = 0.;//< z-component of the electric field
121 std::complex<double> Hx = 0.;//< x-component of the magnetic field
122 std::complex<double> Hy = 0.;//< y-component of the magnetic field
123 std::complex<double> Hz = 0.;//< z-component of the magnetic field
124
125 FullFieldSnapshot() = default;
126
127 /**
128 * @brief Return the component of the field corresponding to the index
129 * provided.
130 *
131 * 0 = Ex, 1 = Ey, 2 = Ez, 3 = Hx, 4 = Hy, 5 = Hz.
132 * This is the indexing order that other storage containers use.
133 *
134 * Throws an error if provided an index <0 or >5.
135 *
136 * @param index Field component index to fetch
137 * @return std::complex<double> The field component
138 */
139 std::complex<double> operator[](int index) {
140 switch (index) {
141 case 0:
142 return Ex;
143 break;
144 case 1:
145 return Ey;
146 break;
147 case 2:
148 return Ez;
149 break;
150 case 3:
151 return Hx;
152 break;
153 case 4:
154 return Hy;
155 break;
156 case 5:
157 return Hz;
158 break;
159 default:
160 throw std::runtime_error("Index " + std::to_string(index) +
161 " does not correspond to a field component.");
162 break;
163 }
164 }
165
166 /**
167 * @brief Multiplies the electric field components by `factor`.
168 * @param factor to scale the field by
169 */
170 void multiply_E_by(std::complex<double> factor) {
171 Ex *= factor;
172 Ey *= factor;
173 Ez *= factor;
174 }
175 /**
176 * @brief Multiplies the magnetic field components by `factor`.
177 * @param factor to scale the field by
178 */
179 void multiply_H_by(std::complex<double> factor) {
180 Hx *= factor;
181 Hy *= factor;
182 Hz *= factor;
183 }
184};
Definition arrays.h:102
Definition arrays.h:116
void multiply_E_by(std::complex< double > factor)
Multiplies the electric field components by factor.
Definition arrays.h:170
void multiply_H_by(std::complex< double > factor)
Multiplies the magnetic field components by factor.
Definition arrays.h:179
std::complex< double > operator[](int index)
Return the component of the field corresponding to the index provided.
Definition arrays.h:139
Definition arrays.h:74
Definition arrays.h:19
void allocate(int I_total, int J_total, int K_total)
Allocates x, y, and z as (K_total+1) * (J_total+1) * (I_total+1) arrays.
Definition arrays.h:56
Type definitions and global constants.
Definition arrays.h:97
Useful miscellaneous utility functions.