-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThermalForce.cpp
executable file
·180 lines (155 loc) · 5.67 KB
/
ThermalForce.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
/**
* @file ThermalForce.cpp
* @class ThermalForce ThermalForce.h
*
* @brief Computes a random force to model thermal effects
*
* @license This file is distributed under the BSD Open Source License.
* See LICENSE.TXT for details.
**/
#include "ThermalForce.h"
#include <cmath>
ThermalForce::ThermalForce(Cloud * const C, const double redFactor)
: Force(C), 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.ThermalForce", NULL)),
#endif
heatVal(redFactor) {
#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
});
#endif
}
ThermalForce::~ThermalForce() {
delete[] evenRandCache;
delete[] oddRandCache;
#ifdef DISPATCH_QUEUES
dispatch_release(evenRandGroup);
dispatch_release(oddRandGroup);
dispatch_release(randQueue);
#endif
}
void ThermalForce::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, oddRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForce::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, evenRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForce::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, oddRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
void ThermalForce::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, evenRandCache[currentParticle/DOUBLE_STRIDE]);
END_PARALLEL_FOR
}
/**
* @brief Computes a thermal force with form F = c*L 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 ThermalForce::force(const cloud_index currentParticle, const RandCache &RC) {
const doubleV thermV = mul_pd(RC.r, heatVal);
plusEqual_pd(cloud->forceX + currentParticle, thermV*randomCos(RC));
plusEqual_pd(cloud->forceY + currentParticle, thermV*randomSin(RC));
}
inline const doubleV ThermalForce::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 ThermalForce::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 ThermalForce::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 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 |= ThermalForceFlag;
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 *> ("heatingValue"), heatVal,
6, const_cast<char *> ("[N] (ThermalForce)"), error);
}
void ThermalForce::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 *> ("heatingValue"), &heatVal, NULL, error);
}