|
| 1 | +#ifndef BSSN_HPP_INCLUDED |
| 2 | +#define BSSN_HPP_INCLUDED |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#include <array> |
| 6 | +#include "../common/value2.hpp" |
| 7 | +#include "../common/single_source.hpp" |
| 8 | +#include <toolkit/opencl.hpp> |
| 9 | +#include "value_alias.hpp" |
| 10 | + |
| 11 | +#define MOMENTUM_CONSTRAINT_DAMPING |
| 12 | +///the number of cells we simply don't damp |
| 13 | +constexpr int constraint_deadzone = 10; |
| 14 | + |
| 15 | +template<typename T> |
| 16 | +struct bssn_args_mem : value_impl::single_source::argument_pack |
| 17 | +{ |
| 18 | + std::array<T, 6> cY; |
| 19 | + std::array<T, 6> cA; |
| 20 | + T K; |
| 21 | + T W; |
| 22 | + std::array<T, 3> cG; |
| 23 | + |
| 24 | + T gA; |
| 25 | + std::array<T, 3> gB; |
| 26 | + |
| 27 | + void build(auto& in) |
| 28 | + { |
| 29 | + using namespace value_impl::builder; |
| 30 | + |
| 31 | + add(cY, in); |
| 32 | + add(cA, in); |
| 33 | + add(K, in); |
| 34 | + add(W, in); |
| 35 | + add(cG, in); |
| 36 | + |
| 37 | + add(gA, in); |
| 38 | + add(gB, in); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +template<typename T> |
| 43 | +struct bssn_derivatives_mem : value_impl::single_source::argument_pack |
| 44 | +{ |
| 45 | + std::array<std::array<T, 3>, 6> dcY; |
| 46 | + std::array<T, 3> dgA; |
| 47 | + std::array<std::array<T, 3>, 3> dgB; |
| 48 | + std::array<T, 3> dW; |
| 49 | + |
| 50 | + void build(auto& in) |
| 51 | + { |
| 52 | + using namespace value_impl::builder; |
| 53 | + |
| 54 | + add(dcY, in); |
| 55 | + add(dgA, in); |
| 56 | + add(dgB, in); |
| 57 | + add(dW, in); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +struct bssn_derivatives; |
| 62 | + |
| 63 | +struct bssn_args |
| 64 | +{ |
| 65 | + unit_metric<valuef, 3, 3> cY; |
| 66 | + tensor<valuef, 3, 3> cA; |
| 67 | + valuef K; |
| 68 | + valuef W; |
| 69 | + tensor<valuef, 3> cG; |
| 70 | + |
| 71 | + valuef gA; |
| 72 | + tensor<valuef, 3> gB; |
| 73 | + |
| 74 | + bssn_args(v3i pos, v3i dim, |
| 75 | + bssn_args_mem<buffer<value<float>>>& in) |
| 76 | + { |
| 77 | + int index_table[3][3] = {{0, 1, 2}, |
| 78 | + {1, 3, 4}, |
| 79 | + {2, 4, 5}}; |
| 80 | + |
| 81 | + for(int i=0; i < 3; i++) |
| 82 | + { |
| 83 | + for(int j=0; j < 3; j++) |
| 84 | + { |
| 85 | + cY[i, j] = in.cY[index_table[i][j]][pos, dim]; |
| 86 | + cA[i, j] = in.cA[index_table[i][j]][pos, dim]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + K = in.K[pos, dim]; |
| 91 | + W = max(in.W[pos, dim], valuef(1e-4f)); |
| 92 | + |
| 93 | + for(int i=0; i < 3; i++) |
| 94 | + cG[i] = in.cG[i][pos, dim]; |
| 95 | + |
| 96 | + gA = max(in.gA[pos, dim], valuef(1e-4f)); |
| 97 | + |
| 98 | + for(int i=0; i < 3; i++) |
| 99 | + gB[i] = in.gB[i][pos, dim]; |
| 100 | + } |
| 101 | + |
| 102 | + tensor<valuef, 3> cG_undiff(const bssn_derivatives& derivs); |
| 103 | +}; |
| 104 | + |
| 105 | +struct bssn_derivatives |
| 106 | +{ |
| 107 | + ///diYjk |
| 108 | + tensor<valuef, 3, 3, 3> dcY; |
| 109 | + tensor<valuef, 3> dgA; |
| 110 | + ///digBj |
| 111 | + tensor<valuef, 3, 3> dgB; |
| 112 | + tensor<valuef, 3> dW; |
| 113 | + |
| 114 | + bssn_derivatives(v3i pos, v3i dim, bssn_derivatives_mem<buffer<derivative_t>>& derivatives) |
| 115 | + { |
| 116 | + int index_table[3][3] = {{0, 1, 2}, |
| 117 | + {1, 3, 4}, |
| 118 | + {2, 4, 5}}; |
| 119 | + |
| 120 | + for(int k=0; k < 3; k++) |
| 121 | + { |
| 122 | + for(int i=0; i < 3; i++) |
| 123 | + { |
| 124 | + for(int j=0; j < 3; j++) |
| 125 | + { |
| 126 | + int index = index_table[i][j]; |
| 127 | + |
| 128 | + dcY[k, i, j] = (valuef)derivatives.dcY[index][k][pos, dim]; |
| 129 | + } |
| 130 | + |
| 131 | + dgB[k, i] = (valuef)derivatives.dgB[i][k][pos, dim]; |
| 132 | + } |
| 133 | + |
| 134 | + dgA[k] = (valuef)derivatives.dgA[k][pos, dim]; |
| 135 | + dW[k] = (valuef)derivatives.dW[k][pos, dim]; |
| 136 | + } |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +struct bssn_buffer_pack |
| 141 | +{ |
| 142 | + std::array<cl::buffer, 6> cY; |
| 143 | + std::array<cl::buffer, 6> cA; |
| 144 | + cl::buffer K; |
| 145 | + cl::buffer W; |
| 146 | + std::array<cl::buffer, 3> cG; |
| 147 | + |
| 148 | + cl::buffer gA; |
| 149 | + std::array<cl::buffer, 3> gB; |
| 150 | + |
| 151 | + //lovely |
| 152 | + bssn_buffer_pack(cl::context& ctx) : |
| 153 | + cY{ctx, ctx, ctx, ctx, ctx, ctx}, |
| 154 | + cA{ctx, ctx, ctx, ctx, ctx, ctx}, |
| 155 | + K{ctx}, |
| 156 | + W{ctx}, |
| 157 | + cG{ctx, ctx, ctx}, |
| 158 | + gA{ctx}, |
| 159 | + gB{ctx, ctx, ctx} |
| 160 | + { |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + void allocate(t3i size) |
| 165 | + { |
| 166 | + int64_t linear_size = int64_t{size.x()} * size.y() * size.z(); |
| 167 | + |
| 168 | + for(auto& i : cY) |
| 169 | + i.alloc(sizeof(cl_float) * linear_size); |
| 170 | + for(auto& i : cA) |
| 171 | + i.alloc(sizeof(cl_float) * linear_size); |
| 172 | + for(auto& i : cG) |
| 173 | + i.alloc(sizeof(cl_float) * linear_size); |
| 174 | + for(auto& i : gB) |
| 175 | + i.alloc(sizeof(cl_float) * linear_size); |
| 176 | + |
| 177 | + K.alloc(sizeof(cl_float) * linear_size); |
| 178 | + W.alloc(sizeof(cl_float) * linear_size); |
| 179 | + gA.alloc(sizeof(cl_float) * linear_size); |
| 180 | + } |
| 181 | + |
| 182 | + template<typename T> |
| 183 | + void for_each(T&& func) |
| 184 | + { |
| 185 | + for(auto& i : cY) |
| 186 | + func(i); |
| 187 | + |
| 188 | + for(auto& i : cA) |
| 189 | + func(i); |
| 190 | + |
| 191 | + func(K); |
| 192 | + func(W); |
| 193 | + |
| 194 | + for(auto& i : cG) |
| 195 | + func(i); |
| 196 | + |
| 197 | + func(gA); |
| 198 | + |
| 199 | + for(auto& i : gB) |
| 200 | + func(i); |
| 201 | + } |
| 202 | + |
| 203 | + void append_to(cl::args& args) |
| 204 | + { |
| 205 | + for(auto& i : cY) |
| 206 | + args.push_back(i); |
| 207 | + |
| 208 | + for(auto& i : cA) |
| 209 | + args.push_back(i); |
| 210 | + |
| 211 | + args.push_back(K); |
| 212 | + args.push_back(W); |
| 213 | + |
| 214 | + for(auto& i : cG) |
| 215 | + args.push_back(i); |
| 216 | + |
| 217 | + args.push_back(gA); |
| 218 | + |
| 219 | + for(auto& i : gB) |
| 220 | + args.push_back(i); |
| 221 | + } |
| 222 | +}; |
| 223 | + |
| 224 | +struct derivative_data; |
| 225 | + |
| 226 | +valuef calculate_hamiltonian_constraint_adm(bssn_args& args, bssn_derivatives& derivs, const derivative_data& d, valuef rho_s); |
| 227 | +valuef calculate_hamiltonian_constraint(bssn_args& args, bssn_derivatives& derivs, const derivative_data& d, valuef rho_s); |
| 228 | +tensor<valuef, 3> calculate_momentum_constraint(bssn_args& args, const derivative_data& d, v3f Si_lower); |
| 229 | + |
| 230 | +struct plugin; |
| 231 | +struct initial_params; |
| 232 | + |
| 233 | +void make_derivatives(cl::context ctx); |
| 234 | +void make_bssn(cl::context ctx, const std::vector<plugin*>& plugins, const initial_params& cfg); |
| 235 | +void enforce_algebraic_constraints(cl::context ctx); |
| 236 | +void init_debugging(cl::context ctx, const std::vector<plugin*>& plugins); |
| 237 | +void make_momentum_constraint(cl::context ctx, const std::vector<plugin*>& plugins); |
| 238 | +void make_sommerfeld(cl::context ctx); |
| 239 | + |
| 240 | +v3i get_coordinate(valuei id, v3i dim); |
| 241 | +//promotes a smaller cube to a bigger cube |
| 242 | +v3i get_coordinate_including_boundary(valuei id, v3i dim); |
| 243 | +v3i get_coordinate_including_boundary(valuei id, v3i dim, int boundary_size); |
| 244 | + |
| 245 | +template<typename T, typename U, typename V> |
| 246 | +inline |
| 247 | +tensor<T, 3> grid_to_world(const tensor<T, 3>& pos, const tensor<V, 3>& dim, const U& scale) |
| 248 | +{ |
| 249 | + tensor<T, 3> centre = (tensor<T, 3>{(T)dim.x(), (T)dim.y(), (T)dim.z()} - 1) / 2; |
| 250 | + |
| 251 | + tensor<T, 3> diff = pos - centre; |
| 252 | + |
| 253 | + return diff * scale; |
| 254 | +} |
| 255 | + |
| 256 | +template<typename T, typename U, typename V> |
| 257 | +inline |
| 258 | +tensor<T, 3> world_to_grid(const tensor<T, 3>& pos, const tensor<V, 3>& dim, const U& scale) |
| 259 | +{ |
| 260 | + tensor<T, 3> centre = (tensor<T, 3>{(T)dim.x(), (T)dim.y(), (T)dim.z()} - 1) / 2; |
| 261 | + |
| 262 | + return (pos / scale) + centre; |
| 263 | +} |
| 264 | + |
| 265 | +template<typename T> |
| 266 | +inline |
| 267 | +std::array<T, 6> extract_symmetry(const tensor<T, 3, 3>& in) |
| 268 | +{ |
| 269 | + return {in[0, 0], in[1, 0], in[2, 0], in[1, 1], in[2, 1], in[2, 2]}; |
| 270 | +} |
| 271 | + |
| 272 | +template<typename T> |
| 273 | +inline |
| 274 | +tensor<T, 3, 3> make_symmetry(const std::array<T, 6>& in) |
| 275 | +{ |
| 276 | + tensor<T, 3, 3> ret; |
| 277 | + ret[0, 0] = in[0]; |
| 278 | + ret[1, 0] = in[1]; |
| 279 | + ret[2, 0] = in[2]; |
| 280 | + |
| 281 | + ret[1, 1] = in[3]; |
| 282 | + ret[2, 1] = in[4]; |
| 283 | + ret[2, 2] = in[5]; |
| 284 | + |
| 285 | + ret[0, 1] = in[1]; |
| 286 | + ret[0, 2] = in[2]; |
| 287 | + ret[1, 2] = in[4]; |
| 288 | + |
| 289 | + return ret; |
| 290 | +} |
| 291 | + |
| 292 | +#endif // BSSN_HPP_INCLUDED |
0 commit comments