-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFramework.cu
executable file
·368 lines (348 loc) · 12.8 KB
/
Framework.cu
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "homogenization/Framework.cuh"
using namespace homo;
using namespace culib;
template<typename CH>
void logIter(int iter, cfg::HomoConfig config, TensorVar<>& rho, CH& Ch, double obj) {
/// fixed log
if (iter % 5 == 0) {
rho.value().toVdb(getPath("rho"));
//rho.diff().toVdb(getPath("sens"));
Ch.writeTo(getPath("C"));
}
Ch.domain_.logger() << "finished iteration " << iter << std::endl;
/// optional log
char namebuf[100];
if (config.logrho != 0 && iter % config.logrho == 0) {
sprintf_s(namebuf, "rho_%04d", iter);
rho.value().toVdb(getPath(namebuf));
}
if (config.logc != 0 && iter % config.logc == 0) {
sprintf_s(namebuf, "Clog");
//Ch.writeTo(getPath(namebuf));
auto ch = Ch.data();
std::ofstream ofs;
if (iter == 0) {
ofs.open(getPath(namebuf));
} else {
ofs.open(getPath(namebuf), std::ios::app);
}
ofs << "iter " << iter << " ";
for (int i = 0; i < 36; i++) { ofs << ch[i] << " "; }
ofs << std::endl;
ofs.close();
}
if (config.logsens != 0 && iter % config.logsens == 0) {
sprintf_s(namebuf, "sens_%04d", iter);
//rho.diff().graft(sens.data());
rho.diff().toVdb(getPath(namebuf));
}
if (config.logobj != 0 && iter % config.logobj == 0) {
sprintf_s(namebuf, "objlog");
std::ofstream ofs;
if (iter == 0) {
ofs.open(getPath(namebuf));
}
else {
ofs.open(getPath(namebuf), std::ios::app);
}
ofs << "iter " << iter << " ";
ofs << "obj = " << obj << std::endl;
ofs.close();
}
}
void initDensity(var_tsexp_t<>& rho, cfg::HomoConfig config) {
int resox = rho.value().length(0);
int resoy = rho.value().length(1);
int resoz = rho.value().length(2);
constexpr float pi = 3.1415926;
if (config.winit == cfg::InitWay::random || config.winit == cfg::InitWay::randcenter) {
randTri(rho.value(), config);
} else if (config.winit == cfg::InitWay::manual) {
rho.value().fromVdb(config.inputrho, false);
} else if (config.winit == cfg::InitWay::interp) {
rho.value().fromVdb(config.inputrho, true);
} else if (config.winit == cfg::InitWay::rep_randcenter) {
randTri(rho.value(), config);
} else if (config.winit == cfg::InitWay::noise) {
rho.value().rand(0.f, 1.f);
symmetrizeField(rho.value(), config.sym);
rho.value().proj(20.f, 0.5f);
auto view = rho.value().view();
auto ker = [=] __device__(int id) { return view(id); };
float s = config.volRatio / (sequence_sum(ker, view.size(), 0.f) / view.size());
rho.value().mapInplace([=] __device__(int x, int y, int z, float val) {
float newval = val * s;
if (newval < 0.001f) newval = 0.001;
if (newval >= 1.f) newval = 1.f;
return newval;
});
} else if (config.winit == cfg::InitWay::P) {
rho.rvalue().setValue([=]__device__(int i, int j, int k) {
float p[3] = { float(i) / resox, float(j) / resoy , float(k) / resoz };
float val = cosf(2 * pi * p[0]) + cosf(2 * pi * p[1]) + cosf(2 * pi * p[2]);
auto newval = tanproj(-val, 20);
newval = max(min(newval, 1.f), 0.001f);
return newval;
});
} else if (config.winit == cfg::InitWay::G) {
rho.rvalue().setValue([=]__device__(int i, int j, int k) {
float p[3] = { float(i) / resox, float(j) / resoy, float(k) / resoz };
float s[3], c[3];
for (int i = 0; i < 3; i++) {
s[i] = sin(2 * pi * p[i]);
c[i] = cos(2 * pi * p[i]);
}
float val = s[0] * c[1] + s[2] * c[0] + s[1] * c[2];
auto newval = tanproj(val, 20);
newval = max(min(newval, 1.f), 0.001f);
return newval;
});
} else if (config.winit == cfg::InitWay::D) {
rho.rvalue().setValue([=] __device__(int i, int j, int k) {
float p[3] = { float(i) / resox, float(j) / resoy, float(k) / resoz };
float x = p[0], y = p[1], z = p[2];
float val = cos(2 * pi * x) * cos(2 * pi * y) * cos(2 * pi * z) - sin(2 * pi * x) * sin(2 * pi * y) * sin(2 * pi * z);
float newval = tanproj(val, 20);
newval = max(min(newval, 1.f), 0.001f);
return newval;
});
} else if (config.winit == cfg::InitWay::IWP) {
rho.rvalue().setValue([=] __device__(int i, int j, int k) {
float p[3] = { float(i) / resox, float(j) / resoy, float(k) / resoz };
float x = p[0], y = p[1], z = p[2];
float val = 2 * (cos(2 * pi * x) * cos(2 * pi * y) + cos(2 * pi * y) * cos(2 * pi * z) + cos(2 * pi * z) * cos(2 * pi * x)) -
(cos(2 * 2 * pi * x) + cos(2 * 2 * pi * y) + cos(2 * 2 * pi * z));
float newval = tanproj(val, 20);
newval = max(min(newval, 1.f), 0.001f);
return newval;
});
}
// symmetrize density field
symmetrizeField(rho.value(), config.sym);
// clamp density value to [rho_min, 1]
rho.value().clamp(0.001, 1);
}
void example_opti_bulk(cfg::HomoConfig config) {
// set output prefix
setPathPrefix(config.outprefix);
// create homogenization domain
Homogenization hom(config);
// update config resolution
for (int i = 0; i < 3; i++) config.reso[i] = hom.getGrid()->cellReso[i];
// define density expression
TensorVar<float> rho(config.reso[0], config.reso[1], config.reso[2]);
// initialize density
initDensity(rho, config);
// output initial density
rho.value().toVdb(getPath("initRho"));
// define material interpolation term
#if 1
auto rhop = rho.pow(3);
#else
auto rhop = rho.conv(radial_convker_t<float, Spline4>(config.filterRadius)).pow(3);
#endif
// create elastic tensor expression
//auto Ch = genCH(hom, rhop);
elastic_tensor_t<float, decltype(rhop)> Ch(hom, rhop);
AbortErr();
// create a oc optimizer
OCOptimizer oc(0.001, config.designStep, config.dampRatio);
// define objective expression
#if 1
auto objective = -(Ch(0, 0) + Ch(1, 1) + Ch(2, 2) +
(Ch(0, 1) + Ch(0, 2) + Ch(1, 2)) * 2) / 9.f; // bulk modulus
#else
auto objective = -(Ch(0, 0) + Ch(1, 1) + Ch(2, 2) +
(Ch(0, 1) + Ch(0, 2) + Ch(1, 2)) * 2) / 9.f; // shear modulus
#endif
// record objective value
std::vector<double> objlist;
// convergence criteria
ConvergeChecker criteria(config.finthres);
// main loop of optimization
for (int iter = 0; iter < config.max_iter; iter++) {
// abort when cuda error occurs
AbortErr();
float val = objective.eval();
// record objective value
objlist.emplace_back(val);
// compute derivative
objective.backward(1);
// output to screen
printf("\033[32m\n * Iter %d obj = %.4e\033[0m\n", iter, val);
// check convergence
if (criteria.is_converge(iter, val)) { printf("= converged\n"); break; }
// make sensitivity symmetry
symmetrizeField(rho.diff(), config.sym);
#if 1
// filtering the sensitivity
oc.filterSens(rho.diff(), rho.value(), config.filterRadius);
#endif
//rho.diff().toMatlab("senscustom");
// update density
oc.update(rho.diff(), rho.value(), config.volRatio);
// make density symmetry
symmetrizeField(rho.value(), config.sym);
// output temp results
logIter(iter, config, rho, Ch, val);
}
//rhop.value().toMatlab("rhofinal");
hom.grid->writeDensity(getPath("density"), VoxelIOFormat::openVDB);
hom.grid->array2matlab("objlist", objlist.data(), objlist.size());
rho.value().toVdb(getPath("rho"));
Ch.writeTo(getPath("C"));
}
void example_opti_npr(cfg::HomoConfig config) {
// set output prefix
setPathPrefix(config.outprefix);
// create homogenization domain
Homogenization hom(config);
// update config resolution
for (int i = 0; i < 3; i++) config.reso[i] = hom.getGrid()->cellReso[i];
// define density expression
TensorVar<float> rho(config.reso[0], config.reso[1], config.reso[2]);
// initialize density
initDensity(rho, config);
// output initial density
rho.value().toVdb(getPath("initRho"));
// define material interpolation term
#if 1
auto rhop = rho.pow(3);
#else
auto rhop = rho.conv(radial_convker_t<float, Spline4>(config.filterRadius)).pow(3);
#endif
// create elastic tensor expression
auto Ch = genCH(hom, rhop);
AbortErr();
// create a oc optimizer
OCOptimizer oc(0.001, config.designStep, config.dampRatio);
// define objective expression
// record objective value
std::vector<double> objlist;
// convergence criteria
ConvergeChecker criteria(config.finthres);
// main loop of optimization
for (int iter = 0; iter < config.max_iter; iter++) {
// abort when cuda error occurs
AbortErr();
float beta = 0.8f;
auto objective = Ch(0, 1) + Ch(0, 2) + Ch(1, 2) -
(Ch(0, 0) + Ch(1, 1) + Ch(2, 2)) * powf(beta, iter);
float val = objective.eval();
// record objective value
objlist.emplace_back(val);
// compute derivative
objective.backward(1);
// output to screen
printf("\033[32m\n * Iter %d obj = %.4e\033[0m\n", iter, val);
// check convergence
if (criteria.is_converge(iter, val)) { printf("= converged\n"); break; }
// make sensitivity symmetry
symmetrizeField(rho.diff(), config.sym);
#if 1
// filtering the sensitivity
oc.filterSens(rho.diff(), rho.value(), config.filterRadius);
#endif
// update density
oc.update(rho.diff(), rho.value(), config.volRatio);
// make density symmetry
symmetrizeField(rho.value(), config.sym);
// output temp results
logIter(iter, config, rho, Ch, val);
}
//rhop.value().toMatlab("rhofinal");
hom.grid->writeDensity(getPath("density"), VoxelIOFormat::openVDB);
hom.grid->array2matlab("objlist", objlist.data(), objlist.size());
rho.value().toVdb(getPath("rho"));
Ch.writeTo(getPath("C"));
}
// usage of MMA optimizer
void example_opti_shear_isotropy(cfg::HomoConfig config) {
// set output prefix
setPathPrefix(config.outprefix);
// create homogenization domain
Homogenization hom(config);
// update config resolution
for (int i = 0; i < 3; i++) config.reso[i] = hom.getGrid()->cellReso[i];
// define density expression
TensorVar<float> rho(config.reso[0], config.reso[1], config.reso[2]);
// initialize density
initDensity(rho, config);
// output initial density
rho.value().toVdb(getPath("initRho"));
// define material interpolation term
auto rhop = rho.conv(radial_convker_t<float, Spline4>(config.filterRadius)).sgm().pow(3);
// create elastic tensor expression
auto Ch = genCH(hom, rhop);
AbortErr();
// create a oc optimizer
int ne = config.reso[0] * config.reso[1] * config.reso[2];
MMAOptimizer mma(2, ne, 1, 0, 1000, 1);
mma.setBound(0.001, 1);
// record objective value
std::vector<double> objlist;
// convergence criteria
ConvergeChecker criteria(config.finthres);
// main loop of optimization
for (int iter = 0; iter < config.max_iter; iter++) {
// define objective expression
auto objective = -(Ch(3, 3) + Ch(4, 4) + Ch(5, 5)) / 3.f; // shear modulus
// abort when cuda error occurs
AbortErr();
float val = objective.eval();
// record objective value
objlist.emplace_back(val);
// compute derivative
objective.backward(1);
// output to screen
printf("\033[32m\n * Iter %d obj = %.4e\033[0m\n", iter, val);
// check convergence
if (criteria.is_converge(iter, val)) { printf("= converged\n"); break; }
// make sensitivity symmetry
symmetrizeField(rho.diff(), config.sym);
// objective derivative
auto objGrad = rho.diff().flatten();
float aniScale = 1000.f;
auto constrain = ((Ch(3, 3) + Ch(4, 4) + Ch(5, 5)) * 2.f /
(Ch(0, 0) + Ch(1, 1) + Ch(2, 2) - Ch(0, 1) - Ch(0, 2) - Ch(1, 2)) - 1.f).pow(2) * aniScale;
float anistroy_constrain = constrain.eval();
constrain.backward(1);
float zener_ratio = sqrt(anistroy_constrain / aniScale) + 1;
symmetrizeField(rho.diff(), config.sym);
auto gGrad = rho.diff().flatten();
// constrain value
auto gval = getTempPool().getUnifiedBlock<float>();
float vol_scale = 1000.f;
auto vol_ratio = rho.conv(radial_convker_t<float, Spline4>(config.filterRadius)).sgm().sum() / ne;
gval.proxy<float>()[0] = (vol_ratio.eval() - config.volRatio) * vol_scale;
gval.proxy<float>()[1] = anistroy_constrain * aniScale - 0.1f;
vol_ratio.backward(1);
// constrain derivative
auto vol_grad = rho.diff().flatten();
float* dgdx[2] = { vol_grad.data(), gGrad.data() };
// design variables
auto rhoArray = rho.value().flatten();
printf("zener ratio = %4.2e ; obj = %4.2e ; vol = %4.2e\n", zener_ratio, val, vol_ratio);
printf("constrain = %4.2e ; %4.2e\n", float(gval.proxy<float>()[0]), float(gval.proxy<float>()[1]));
// mma update
mma.update(iter, rhoArray.data(), objGrad.data(), gval.data<float>(), dgdx);
//update variable
rho.value().graft(rhoArray.data());
// output temp results
logIter(iter, config, rho, Ch, val);
}
//rhop.value().toMatlab("rhofinal");
hom.grid->writeDensity(getPath("density"), VoxelIOFormat::openVDB);
hom.grid->array2matlab("objlist", objlist.data(), objlist.size());
rho.value().toVdb(getPath("rho"));
Ch.writeTo(getPath("C"));
}
void example_yours(cfg::HomoConfig config) {
// add your routines here ...
}
void runCustom(cfg::HomoConfig config) {
//example_opti_bulk(config);
//example_opti_npr(config);
//example_opti_shear_isotropy(config);
example_yours(config);
}