-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThermalForceLocalized.cpp
executable file
·197 lines (172 loc) · 7.26 KB
/
ThermalForceLocalized.cpp
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
/**
* @file ThermalForceLocalized.cpp
* @class ThermalForceLocalized ThermalForceLocalized.h
*
* @brief Computes a random force to model thermal effects
* where the force changes at a given radius
*
* @license This file is distributed under the BSD Open Source License.
* See LICENSE.TXT for details.
**/
#include "ThermalForceLocalized.h"
#include <cmath>
ThermalForceLocalized::ThermalForceLocalized(Cloud * const C, const double thermRed1,
const double thermRed2, const double specifiedRadius)
: Force(C), heatingRadius(specifiedRadius), heatVal1(thermRed1), heatVal2(thermRed2),
evenRandCache(new RandCache[C->n/DOUBLE_STRIDE]), oddRandCache(new RandCache[C->n/DOUBLE_STRIDE])
#ifdef DISPATCH_QUEUES
, evenRandGroup(dispatch_group_create()), oddRandGroup(dispatch_group_create()),
randQueue(dispatch_queue_create("com.DEMON.ThermalForceLocalized", NULL))
#endif
{
for (cloud_index i = 0, e = cloud->n/DOUBLE_STRIDE; i < e; i++)
oddRandCache[i] = RandCache(cloud->rands);
}
ThermalForceLocalized::~ThermalForceLocalized() {
delete[] evenRandCache;
delete[] oddRandCache;
#ifdef DISPATCH_QUEUES
dispatch_release(evenRandGroup);
dispatch_release(oddRandGroup);
dispatch_release(randQueue);
#endif
}
void ThermalForceLocalized::force1(const double currentTime) {
(void)currentTime;
#ifdef DISPATCH_QUEUES
dispatch_group_async(evenRandGroup, randQueue, ^{
#endif
for (cloud_index i = 0, e = cloud->n/DOUBLE_STRIDE; i < e; i++)
evenRandCache[i] = RandCache(cloud->rands);
#ifdef DISPATCH_QUEUES
});
dispatch_group_wait(oddRandGroup, DISPATCH_TIME_FOREVER);
#endif
BEGIN_PARALLEL_FOR(currentParticle, numParticles, cloud->n, DOUBLE_STRIDE, static)
force(currentParticle, cloud->getx1_pd(currentParticle), cloud->gety1_pd(currentParticle),
oddRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForceLocalized::force2(const double currentTime) {
(void)currentTime;
#ifdef DISPATCH_QUEUES
dispatch_group_async(oddRandGroup, randQueue, ^{
#endif
for (cloud_index i = 0, e = cloud->n/DOUBLE_STRIDE; i < e; i++)
oddRandCache[i] = RandCache(cloud->rands);
#ifdef DISPATCH_QUEUES
});
dispatch_group_wait(evenRandGroup, DISPATCH_TIME_FOREVER);
#endif
BEGIN_PARALLEL_FOR(currentParticle, numParticles, cloud->n, DOUBLE_STRIDE, static)
force(currentParticle, cloud->getx2_pd(currentParticle), cloud->gety2_pd(currentParticle),
evenRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForceLocalized::force3(const double currentTime) {
(void)currentTime;
#ifdef DISPATCH_QUEUES
dispatch_group_async(evenRandGroup, randQueue, ^{
#endif
for (cloud_index i = 0, e = cloud->n/DOUBLE_STRIDE; i < e; i++)
evenRandCache[i] = RandCache(cloud->rands);
#ifdef DISPATCH_QUEUES
});
dispatch_group_wait(oddRandGroup, DISPATCH_TIME_FOREVER);
#endif
BEGIN_PARALLEL_FOR(currentParticle, numParticles, cloud->n, DOUBLE_STRIDE, static)
force(currentParticle, cloud->getx3_pd(currentParticle), cloud->gety3_pd(currentParticle),
oddRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForceLocalized::force4(const double currentTime) {
(void)currentTime;
#ifdef DISPATCH_QUEUES
dispatch_group_async(oddRandGroup, randQueue, ^{
#endif
for (cloud_index i = 0, e = cloud->n/DOUBLE_STRIDE; i < e; i++)
oddRandCache[i] = RandCache(cloud->rands);
#ifdef DISPATCH_QUEUES
});
dispatch_group_wait(evenRandGroup, DISPATCH_TIME_FOREVER);
#endif
BEGIN_PARALLEL_FOR(currentParticle, numParticles, cloud->n, DOUBLE_STRIDE, static)
force(currentParticle, cloud->getx4_pd(currentParticle), cloud->gety4_pd(currentParticle),
evenRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
// F = c1*L : if r > h_r
// F = c2*L : if r < h_r
// L is a uniformly distributed random number between 0 - 1 in a random
// direction.
/**
* @brief Computes a thermal force with form F = c1*L : if r > h_r
* and F = c2*L : if r < h_r
* where L is a uniformly distributed random number between 0 - 1 in a
* random direction.
*
* @param[in] currentParticle The particle whose force is being computed
* @param[in] RC RandCache struct from RandomNumbers.h
**/
inline void ThermalForceLocalized::force(const cloud_index currentParticle, const doubleV displacementX,
const doubleV displacementY, const RandCache &RC) {
const doubleV radiusV = _mm_sqrt_pd(displacementX*displacementX + displacementY*displacementY);
const int mask = movemask_pd(cmplt_pd(radiusV, heatingRadius));
const doubleV thermV = mul_pd(select_pd(mask, heatVal1, heatVal2), RC.r);
plusEqual_pd(cloud->forceX + currentParticle, thermV*randomCos(RC)); // _mm_set_pd() is backwards
plusEqual_pd(cloud->forceY + currentParticle, thermV*randomSin(RC));
}
inline const doubleV ThermalForceLocalized::randomCos(const RandCache &RC) {
#ifdef __AVX__
return _mm256_set_pd(cos(RC.r4), cos(RC.r3), cos(RC.r2), cos(RC.r1));
#else
return _mm_set_pd(cos(RC.r2), cos(RC.r1));
#endif
}
inline const doubleV ThermalForceLocalized::randomSin(const RandCache &RC) {
#ifdef __AVX__
return _mm256_set_pd(sin(RC.r4), sin(RC.r3), sin(RC.r2), sin(RC.r1));
#else
return _mm_set_pd(sin(RC.r2), sin(RC.r1));
#endif
}
void ThermalForceLocalized::writeForce(fitsfile * const file, int * const error) const {
// move to primary HDU:
if (!*error)
// file, # indicating primary HDU, HDU type, error
fits_movabs_hdu(file, 1, IMAGE_HDU, error);
// add flag indicating that the localized thermal force is used:
if (!*error) {
long forceFlags = 0;
fits_read_key_lng(file, const_cast<char *> ("FORCES"), &forceFlags, NULL, error);
// add ThermalForce bit:
forceFlags |= ThermalForceLocalizedFlag;
if (*error == KEY_NO_EXIST || *error == VALUE_UNDEFINED)
*error = 0; // clear above error.
// add or update keyword:
if (!*error)
fits_update_key(file, TLONG, const_cast<char *> ("FORCES"), &forceFlags,
const_cast<char *> ("Force configuration."), error);
}
if (!*error) {
// file, key name, value, precision (scientific format), comment
fits_write_key_dbl(file, const_cast<char *> ("heatingValue1"), heatVal1,
6, const_cast<char *> ("[N] (ThermalForceLocalized)"), error);
fits_write_key_dbl(file, const_cast<char *> ("heatingValue2"), heatVal2,
6, const_cast<char *> ("[N] (ThermalForceLocalized)"), error);
fits_write_key_dbl(file, const_cast<char *> ("heatingRadius"), heatingRadius,
6, const_cast<char *> ("[m] (ThermalForceLocalized)"), error);
}
}
void ThermalForceLocalized::readForce(fitsfile * const file, int * const error) {
// move to primary HDU:
if (!*error)
// file, # indicating primary HDU, HDU type, error
fits_movabs_hdu(file, 1, IMAGE_HDU, error);
if (!*error) {
// file, key name, value, don't read comment, error
fits_read_key_dbl(file, const_cast<char *> ("heatingValue1"), &heatVal1, NULL, error);
fits_read_key_dbl(file, const_cast<char *> ("heatingValue2"), &heatVal2, NULL, error);
fits_read_key_dbl(file, const_cast<char *> ("heatingRadius"), &heatingRadius, NULL, error);
}
}