1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
#pragma once
#include <forstio/codec/math.hpp>
#include <forstio/codec/data_math.hpp>
#include <forstio/codec/data.hpp>
#include "../iterator.hpp"
namespace kel {
namespace lbm {
namespace coll {
struct Spheroid{};
}
namespace sch {
using namespace saw::schema;
namespace impl {
template<typename T,uint64_t D>
struct rotation_type_helper;
template<typename T>
struct rotation_type_helper<T,2u> {
using Schema = Scalar<T>;
};
template<typename T>
struct rotation_type_helper<T,3u> {
using Schema = Vector<T,3u>;
};
}
template<typename T, uint64_t D>
using ParticleRigidBody = Struct<
Member<Vector<T,D>, "position">,
Member<Vector<T,D>, "position_old">,
Member<typename impl::rotation_type_helper<T,D>::Schema, "rotation">,
Member<typename impl::rotation_type_helper<T,D>::Schema, "rotation_old">,
Member<Vector<T,D>, "acceleration">,
Member<typename impl::rotation_type_helper<T,D>::Schema, "angular_acceleration">
>;
template<typename T>
using ParticleCollisionSpheroid = Struct<
Member<Scalar<T>, "radius">
>;
template<typename T, uint64_t D>
using Particle = Struct<
Member<ParticleRigidBody<T,D>, "rigid_body">
// Problem is that dynamic data would two layered
// Member<Array<Float64,D>, "mask">,
>;
template<typename T, uint64_t D, typename CollisionType = ParticleCollisionSpheroid<T>>
using ParticleGroup = Struct<
Member<Array<T,D>, "mask">,
Member<CollisionType, "collision">,
Member<Scalar<T>, "mass">,
Member<Array<Particle<T,D>>, "particles">
>;
}
template<typename T, uint64_t D>
saw::data<sch::ParticleGroup<T,D, sch::ParticleCollisionSpheroid<T>>> create_spheroid_particle_group(
saw::data<sch::Scalar<T>> rad_p,
saw::data<sch::Scalar<T>> density_p,
const saw::data<sch::UInt64>& mask_resolution
){
saw::data<sch::ParticleGroup<T,D,sch::ParticleCollisionSpheroid<T>>> part;
auto& mask = part.template get<"mask">();
auto& collision = part.template get<"collision">();
auto& mass = part.template get<"mass">();
if constexpr ( D == 1u ){
saw::data<sch::Scalar<T>> c;
c.at({}).set(2.0);
mass = rad_p * c * density_p;
} else if constexpr ( D == 2u){
saw::data<sch::Scalar<T>> pi;
pi.at({}).set(3.14159);
mass = rad_p * rad_p * pi * density_p;
} else if constexpr ( D == 3u ){
saw::data<sch::Scalar<T>> c;
c.at({}).set(3.14159 * 4.0 / 3.0);
mass = rad_p * rad_p * rad_p * c * density_p;
} else {
static_assert(D == 0u or D > 3u, "Dimensions only supported for Dim 1,2 & 3.");
}
saw::data<sch::FixedArray<sch::UInt64,D>> mask_dims;
for(uint64_t i = 0u; i < D; ++i){
mask_dims.at({i}) = mask_resolution;
}
mask = {mask_dims};
iterator<D>::apply([&](const auto& index){
},{},mask_dims);
return part;
}
/*
template<typename T, uint64_t D>
saw::data<sch::Particle<T,D, sch::ParticleCollisionSpheroid<T>>> create_spheroid_particle(
saw::data<sch::Vector<T,D>> pos_p,
saw::data<sch::Vector<T,D>> vec_p,
saw::data<sch::Vector<T,D>> acc_p,
saw::data<sch::Vector<T,D>> rot_pos_p,
saw::data<sch::Vector<T,D>> rot_vel_p,
saw::data<sch::Vector<T,D>> rot_acc_p,
saw::data<sch::Scalar<T>> rad_p,
saw::data<sch::Scalar<T>> density_p,
saw::data<sch::Scalar<T>> dt
){
saw::data<sch::Particle<T,D>> part;
auto& body = part.template get<"rigid_body">();
auto& mass = part.template get<"mass">();
auto& pos = body.template get<"position">();
auto& pos_old = body.template get<"position_old">();
auto& acc = body.template get<"acceleration">();
auto& rot = body.template get<"rotation">();
auto& rot_old = body.template get<"rotation_old">();
auto& coll = part.template get<"collision">();
auto& rad = coll.template get<"radius">();
pos = pos_p;
pos_old = pos - vec_p * dt;
acc = acc_p;
rad = rad_p;
if constexpr ( D == 1u){
saw::data<sch::Scalar<T>> c;
c.at({}).set(2.0);
mass = rad_p * c * density_p;
} else if constexpr ( D == 2u){
saw::data<sch::Scalar<T>> pi;
pi.at({}).set(3.14159);
mass = rad_p * rad_p * pi * density_p;
} else if constexpr ( D == 3u ){
saw::data<sch::Scalar<T>> c;
c.at({}).set(3.14159 * 4.0 / 3.0);
mass = rad_p * rad_p * rad_p * c * density_p;
} else {
static_assert(D == 0u or D > 3u, "Dimensions only supported for Dim 1,2 & 3.");
}
return part;
}
*/
template<typename T,uint64_t D>
constexpr auto verlet_step_lambda = [](saw::data<sch::Particle<T,D>>& particle, saw::data<sch::Scalar<T>> time_step_delta){
auto& body = particle.template get<"rigid_body">();
auto& pos = body.template get<"position">();
auto& pos_old = body.template get<"position_old">();
auto& pos_acc = body.template get<"acceleration">();
auto& rot = body.template get<"rotation">();
auto& rot_old = body.template get<"rotation_old">();
auto& rot_acc = body.template get<"angular_acceleration">();
auto tsd_squared = time_step_delta * time_step_delta;
saw::data<sch::Vector<T,D>> pos_new;
// Actual step
saw::data<sch::Scalar<T>> two;
two.at({}).set(2.0);
pos_new = pos * two - pos_old + pos_acc * tsd_squared;
// Angular
saw::data<typename sch::impl::rotation_type_helper<T,D>::Schema> rot_new;
rot_new = rot * two - rot_old + rot_acc * tsd_squared;
// Swap - Could be std::swap?
pos_old = pos;
pos = pos_new;
rot_old = rot;
rot = rot_new;
};
template<typename T, uint64_t D>
constexpr auto handle_collision = [](
saw::data<sch::Particle<T,D>>& left, const saw::data<sch::Scalar<T>>& mass_l,
saw::data<sch::Particle<T,D>>& right, const saw::data<sch::Scalar<T>>& mass_r,
saw::data<sch::Vector<T,D>> unit_pos_rel, saw::data<sch::Vector<T,D>> vel_rel,
saw::data<sch::Scalar<T>> d_t
){
auto& rb_l = left.template get<"rigid_body">();
auto& pos_l = rb_l.template get<"position">();
auto& pos_old_l = rb_l.template get<"position_old">();
auto vel_l = (pos_l-pos_old_l)/d_t;
auto& rb_r = right.template get<"rigid_body">();
auto& pos_r = rb_r.template get<"position">();
auto& pos_old_r = rb_r.template get<"position_old">();
auto vel_r = (pos_r-pos_old_r)/d_t;
auto vel_pos_rel_dot = saw::math::dot(unit_pos_rel,vel_rel);
if( vel_pos_rel_dot.at({{0u}}).get() < 0.0 ){
pos_l = pos_l + vel_rel * unit_pos_rel * d_t;
pos_r = pos_r - vel_rel * unit_pos_rel * d_t;
}
};
template<typename T, uint64_t D, typename Collision>
constexpr auto broadphase_collision_distance_squared = [](
saw::data<sch::Particle<T,D>>& left,
const saw::data<Collision>& coll_l,
saw::data<sch::Particle<T,D>>& right,
const saw::data<Collision>& coll_r
) -> std::pair<bool,saw::data<sch::Scalar<T>>>{
auto rad_l = coll_l.template get<"radius">();
auto rad_r = coll_r.template get<"radius">();
auto& rb_l = left.template get<"rigid_body">();
auto& rb_r = right.template get<"rigid_body">();
auto& pos_l = rb_l.template get<"position">();
auto& pos_r = rb_r.template get<"position">();
auto pos_dist = pos_l - pos_r;
auto norm_2 = saw::math::dot(pos_dist,pos_dist);
saw::data<sch::Scalar<T>> two;
two.at({}) = 2.0;
auto rad_ab_2 = rad_l * rad_l + rad_r * rad_r + rad_r * rad_l * two;
return std::make_pair((norm_2.at({}).get() < rad_ab_2.at({}).get()), norm_2);
};
/**
*
*
*/
template<typename T, uint64_t D, typename Collision>
constexpr auto broadphase_collision_check = [](
saw::data<sch::Particle<T,D>>& left,
const saw::data<Collision>& coll_l,
saw::data<sch::Particle<T,D>>& right,
const saw::data<Collision>& coll_r
) -> bool{
return broadphase_collision_distance_squared<T,D,Collision>(left,coll_l,right,coll_r).first;
};
}
}
|