Debian Codegraph @

src/num/aslBasicBC.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 #include "aslBasicBC.h"
25 #include "acl/aclMath/aclVectorOfElements.h"
26 #include "acl/acl.h"
27 #include "acl/aclHardware.h"
28 #include "acl/aclGenerators.h"
29 #include <acl/Kernels/aclKernel.h>
30 #include "acl/Kernels/aclKernelConfigurationTemplates.h"
31 #include <aslGenerators.h>
32 #include <math/aslTemplateVE.h>
33 #include "acl/aclMath/aclMathAlg.h"
34 #include <math/aslIndex2Position.h>
35 #include <math/aslPositionFunction.h>
36 #include <utilities/aslUValue.h>
37 #include "aslBasicBC2.h"
38
39
40 namespace asl
41 {
42 BCConstantValue::BCConstantValue(Data d, const acl::VectorOfElements & v):
43 BCond(d->getBlock()),
44 kernel(new acl::Kernel()),
45 data(d),
46 value(v)
47 {
48 }
49
50
51 void BCConstantValue::init()
52 {
53 loadIndicesToACL();
54 kernel->clear();
55 (*kernel)<<(acl::excerpt(data->getEContainer(), *indicesACL) = value);
56 kernel->setup();
57 }
58
59
60 void BCConstantValue::execute()
61 {
62 kernel->compute();
63 }
64
65
66 void BCConstantValue::setValue(const acl::VectorOfElements & v)
67 {
68 acl::copy(v,value);
69 }
70
71 BCConstantValueMap::BCConstantValueMap(Data d,
72 const acl::VectorOfElements & v,
73 Data map):
74 BCondWithMap(map, nearestNeigboursVT(nD(d->getBlock()))),
75 kernel(new acl::Kernel(acl::KERNEL_SIMDUA)),
76 data(d),
77 value(v)
78 {
79 }
80
81 BCConstantValueMap::~BCConstantValueMap()
82 {
83 }
84
85
86 void BCConstantValueMap::init()
87 {
88 acl::ExpressionContainer kk;
89
90 acl::TypeID type(getElementType(data->getEContainer()));
91
92 auto vGhost(data->getEContainer());
93 kk << (acl::assignmentSafe(vGhost,
94 select(vGhost, value, isGhostNode(), type)));
95
96 kernel->addExpression(acl::elementOperators::
97 ifElse(acl::elementOperators::any((map->getEContainer() <= 0 &&
98 map->getEContainer() > -.9999)[0]),
99 kk.expression,
100 {} ));
101
102 kernel->setup();
103 }
104
105 void BCConstantValueMap::execute()
106 {
107 kernel->compute();
108 }
109
110 BCValuePFMap::BCValuePFMap(Data d,
111 SPPositionFunction v,
112 Data map):
113 BCondWithMap(map, nearestNeigboursVT(nD(d->getBlock()))),
114 kernel(new acl::Kernel(acl::KERNEL_SIMDUA)),
115 data(d),
116 value(v)
117 {
118 }
119
120 BCValuePFMap::~BCValuePFMap()
121 {
122 }
123
124 void BCValuePFMap::init()
125 {
126 acl::ExpressionContainer kk;
127
128 acl::TypeID type(getElementType(data->getEContainer()));
129
130 auto vGhost(data->getEContainer());
131 asl::Index2PositionACL i2p(data->getBlock(),type);
132
133 kk << i2p.initPosition;
134 auto val(value->value(i2p.position));
135 kk << (acl::assignmentSafe(vGhost,
136 select(vGhost, val, isGhostNode(), type)));
137
138 kernel->addExpression(acl::elementOperators::
139 ifElse(acl::elementOperators::any((map->getEContainer() <= 0 &&
140 map->getEContainer() > -.9999)[0]),
141 kk.expression,
142 {} ));
143
144 kernel->setup();
145 }
146
147 void BCValuePFMap::execute()
148 {
149 kernel->compute();
150 }
151
152
153 BCConstantValueMiddlePointMap::
154 BCConstantValueMiddlePointMap(Data d,
155 const acl::VectorOfElements & v,
156 Data map,
157 const VectorTemplate *const t):
158 BCondWithMap(map, t),
159 kernel(new acl::Kernel(acl::KERNEL_BASIC)),
160 // kernel(new acl::Kernel(acl::KERNEL_SIMDUA)),
161 data(d),
162 value(v)
163 {
164 }
165
166 BCConstantValueMiddlePointMap::~BCConstantValueMiddlePointMap()
167 {
168 }
169
170
171 void BCConstantValueMiddlePointMap::init()
172 {
173 acl::ExpressionContainer kk;
174
175 unsigned int nc(data->getEContainer().size());
176
177 auto dataX(generateDCFullSafe(data));
178
179 acl::TypeID type(getElementType(data->getEContainer()));
180 acl::TypeID typeI(acl::TYPE_SELECT[type]);
181
182 initMapInfrastructure(kk);
183
184 vector<TemplateVE> dataTVE(nc);
185 for(unsigned int ii(0); ii < nc; ++ii)
186 {
187 dataTVE[ii].init(*dataX, *templ, ii);
188 kk << dataTVE[ii].initValues;
189 }
190
191 auto vLocal(generateVEPrivateVariable(nc,type));
192 auto isBoundary(generateVEPrivateVariable(1,typeI));
193 auto counter(generateVEPrivateVariable(1,type));
194
195 unsigned int nDir(mapTVE->vectorTemplate->vectors.size());
196
197 kk << (vLocal = acl::generateVEConstantN(nc,0.));
198 kk << (counter = acl::generateVEConstant(0.));
199
200 for(unsigned int i(1); i < nDir; ++i)
201 {
202 kk << (isBoundary = isComputationNode(i));
203
204 for(unsigned int ii(0); ii < nc; ++ii)
205 {
206 auto vBulk(subVE(dataTVE[ii].values,i));
207 kk << (subVE(vLocal,ii) += select(2.*subVE(value, ii) - vBulk,
208 isBoundary,
209 type) * templ->laplasCoefs[i]);
210 }
211 kk << (counter+= select(acl::generateVEConstant( templ->laplasCoefs[i] ),
212 isBoundary,
213 type));
214 }
215
216 kk << (vLocal /= max(counter,acl::generateVEConstant(.01),type));
217 for(unsigned int ii(0); ii < nc; ++ii)
218 {
219 kk << (acl::assignmentSafe(subVE(data->getEContainer(),ii),
220 select(subVE(dataTVE[ii].values,0),
221 subVE(vLocal,ii),
222 isGhostNode(0),
223 type)));
224 }
225 kernel->addExpression(acl::elementOperators::
226 ifElse(acl::elementOperators::any((isGhostNode() &&
227 map->getEContainer() > -.9999)[0]),
228 kk.expression,
229 {} ));
230
231 kernel->setup();
232 }
233
234 void BCConstantValueMiddlePointMap::execute()
235 {
236 kernel->compute();
237 }
238
239
240 BCConstantGradient::BCConstantGradient(Data d,
241 const acl::VectorOfElements & v,
242 const VectorTemplate *const t):
243 BCond(d->getBlock(), t),
244 kernel(new acl::Kernel()),
245 data(d),
246 value(v * block.dx)
247 {
248 }
249
250 void BCConstantGradient::init()
251 {
252 loadIndicesToACL();
253 loadNeighbourIndicesToACL();
254 kernel->clear();
255 (*kernel) << (acl::excerpt(data->getEContainer(), *indicesACL) =
256 acl::excerpt(data->getEContainer(), *neighbourIndicesACL) -
257 value);
258 kernel->setup();
259 }
260
261 void BCConstantGradient::execute()
262 {
263 kernel->compute();
264 }
265
266 void BCConstantGradient::setValue(const acl::VectorOfElements & v)
267 {
268 acl::copy(v * block.dx, value);
269 }
270
271 BCConstantGradientMap::BCConstantGradientMap(Data d,
272 const acl::VectorOfElements & v,
273 Data map,
274 const VectorTemplate *const t):
275 BCondWithMap(map, t),
276 // kernel(new acl::Kernel(acl::KERNEL_SIMDUA)),
277 kernel(new acl::Kernel(acl::KERNEL_BASIC)),
278 data(d),
279 value(v)
280 {
281 }
282
283 BCConstantGradientMap::BCConstantGradientMap(Data d,
284 const acl::VectorOfElements & v,
285 Data map,
286 Data computationalDomain,
287 const VectorTemplate *const t):
288 BCondWithMap(map, computationalDomain, t),
289 kernel(new acl::Kernel(acl::KERNEL_SIMDUA)),
290 data(d),
291 value(v)
292 {
293 }
294
295 BCConstantGradientMap::~BCConstantGradientMap()
296 {
297 }
298
299
300 void BCConstantGradientMap::init()
301 {
302 acl::ExpressionContainer kk;
303
304 unsigned int nc(data->getEContainer().size());
305 unsigned int nDir(templ->vectors.size());
306 unsigned int nd(nD(data->getBlock()));
307
308 auto dataX(generateDCFullSafe(data));
309
310 initMapInfrastructure(kk);
311
312 acl::TypeID type(getElementType(data->getEContainer()));
313
314 auto normal(generateVEPrivateVariable(nd,type));
315 auto vLocal(generateVEPrivateVariable(1,type));
316 auto counter(generateVEPrivateVariable(1,type));
317 auto dirCoef(generateVEPrivateVariable(1,type));
318
319 kk << (counter = acl::generateVEConstant(0.));
320 kk << (normal = gradient(*mapTVE));
321 kk << (gcNormalize(normal));
322 for(unsigned int i(1); i < nDir; ++i)
323 {
324 kk << (dirCoef = (normal * normalize(templ->vectors[i])) + 1.);
325 kk << (dirCoef *= dirCoef );
326 kk << (counter+= select(dirCoef, isComputationNode(i), type));
327 }
328 kk << (counter = max(counter,acl::generateVEConstant(.001), type));
329
330
331 TemplateVE dataTVE;
332 for(unsigned int ii(0); ii < nc; ++ii)
333 {
334 dataTVE.init(*dataX, *templ, ii,false);
335 kk << (vLocal = acl::generateVEConstant(0.));
336 for(unsigned int i(1); i < nDir; ++i)
337 {
338 kk << (dirCoef = (normal * normalize(templ->vectors[i])) + 1.);
339 kk << (dirCoef *= dirCoef );
340 kk << (vLocal += select((dataTVE.getValue(i) -
341 (normal*normalize(templ->vectors[i]))*subVE(value, ii)) *
342 dirCoef,
343 isComputationNode(i),
344 type));
345 }
346 kk << (acl::assignmentSafe(subVE(data->getEContainer(),ii),
347 select(subVE(dataTVE.values,0),
348 vLocal/counter,
349 isGhostNode(0) && counter>0.1,
350 type)));
351 }
352 kernel->addExpression(acl::elementOperators::
353 ifElse(acl::elementOperators::any((map->getEContainer() <= 0 &&
354 map->getEContainer() > -.9999)[0]),
355 kk.expression,
356 {} ));
357
358 kernel->setup();
359 }
360
361 /* void BCConstantGradientMap::init()
362 {
363 acl::ExpressionContainer kk;
364
365 unsigned int nc(data->getEContainer().size());
366 unsigned int nd(nD(data->getBlock()));
367
368 auto dataX(generateDCFullSafe(data));
369
370 initMapInfrastructure(kk);
371
372 acl::TypeID type(getElementType(data->getEContainer()));
373 acl::TypeID typeI(acl::TYPE_SELECT[type]);
374
375 vector<TemplateVE> dataTVE(nc);
376 for(unsigned int ii(0); ii < nc; ++ii)
377 {
378 dataTVE[ii].init(*dataX, *templ, ii);
379 kk << dataTVE[ii].initValues;
380 }
381
382 auto normal(generateVEPrivateVariable(nd,type));
383 auto vLocal(generateVEPrivateVariable(nc,type));
384 auto isBoundary(generateVEPrivateVariable(1,typeI));
385 auto counter(generateVEPrivateVariable(1,type));
386 auto dirCoef(generateVEPrivateVariable(1,type));
387
388 unsigned int nDir(templ->vectors.size());
389
390 kk << (vLocal = acl::generateVEConstantN(nc,0.));
391 kk << (counter = acl::generateVEConstant(0.));
392 kk << (normal = gradient(*mapTVE));
393 kk << (gcNormalize(normal));
394 for(unsigned int i(1); i < nDir; ++i)
395 {
396 kk << (isBoundary = isComputationNode(i));
397 kk << (dirCoef = (normal * normalize(templ->vectors[i])) + 1.);
398 kk << (dirCoef *= dirCoef );
399 for(unsigned int ii(0); ii < nc; ++ii)
400 {
401 auto vBulk(subVE(dataTVE[ii].values,i));
402 kk << (subVE(vLocal,ii) += select(acl::generateVEConstant(0.),
403 vBulk - subVE(value, ii) *
404 (normal * templ->vectors[i]),
405 isBoundary,
406 type) * dirCoef);
407 }
408 kk << (counter+= select(acl::generateVEConstant( 0.),
409 dirCoef,
410 isBoundary,
411 type));
412 }
413 kk << (vLocal /= max(counter,acl::generateVEConstant(.01)));
414 for(unsigned int ii(0); ii < nc; ++ii)
415 {
416 kk << (acl::assignmentSafe(subVE(data->getEContainer(),ii),
417 select(subVE(dataTVE[ii].values,0),
418 subVE(vLocal,ii),
419 isGhostNode(0),
420 type)));
421 }
422 kernel->addExpression(acl::elementOperators::
423 ifElse(acl::elementOperators::any((map->getEContainer() <= 0 &&
424 map->getEContainer() > -.9999)[0]),
425 kk.expression,
426 {} ));
427
428 kernel->setup();
429 }
430 */
431 void BCConstantGradientMap::execute()
432 {
433 kernel->compute();
434 }
435
436
437 BCConstantSource::BCConstantSource(Data d, cl_double v):
438 BCond(d->getBlock()),
439 kernel(new acl::Kernel()),
440 data(d),
441 value(v)
442 {
443 }
444
445 void BCConstantSource::init()
446 {
447 loadIndicesToACL();
448 kernel->clear();
449 (*kernel)<<(acl::excerpt(data->getEContainer(),*indicesACL)+=
450 acl::generateVEVariableR(value));
451 kernel->setup();
452 }
453
454
455 void BCConstantSource::execute()
456 {
457 kernel->compute();
458 }
459
460
461 void BCConstantSource::setValue(cl_double v)
462 {
463 value = v;
464 }
465
466
467 BCDirectCopier::BCDirectCopier(Data dSource, Data dDestination):
468 BCondConnector(dSource->getBlock(), dDestination->getBlock()),
469 kernel(new acl::Kernel()),
470 source(dSource),
471 destination(dDestination)
472 {
473 }
474
475
476 void BCDirectCopier::init()
477 {
478 loadIndicesToACL();
479 kernel->clear();
480 (*kernel)<<(acl::excerpt(destination->getEContainer(),*indices2ACL)=
481 acl::excerpt(source->getEContainer(),*indices1ACL));
482 // (*kernel)<<(*indices2ACL=*indices1ACL);
483 kernel->setup();
484 }
485
486
487 void BCDirectCopier::execute()
488 {
489 kernel->compute();
490 }
491
492 SPBCond generateBCConstantValue(SPAbstractDataWithGhostNodes d,
493 double v,
494 const std::vector<SlicesNames> & sl)
495 {
496 auto bc(make_shared<BCConstantValue>(d, acl::generateVEConstant(v)));
497 addSlices(*bc,sl);
498 return bc;
499 }
500
501 SPBCond generateBCConstantValue(SPAbstractDataWithGhostNodes d,
502 UValue<double> & v,
503 const std::vector<SlicesNames> & sl)
504 {
505 auto bc(make_shared<BCConstantValue>(d, acl::generateVEVariableSP(v.p)));
506 addSlices(*bc,sl);
507 return bc;
508 }
509
510 SPBCond generateBCConstantValue(SPAbstractDataWithGhostNodes d,
511 UValue<AVec<float>> & v,
512 const std::vector<SlicesNames> & sl)
513 {
514 auto bc(make_shared<BCConstantValue>(d, acl::generateVEVariableSP(v.p)));
515 addSlices(*bc,sl);
516 return bc;
517 }
518
519 SPBCond generateBCConstantValue(SPAbstractDataWithGhostNodes d,
520 AVec<> v,
521 const std::vector<SlicesNames> & sl)
522 {
523 auto bc(make_shared<BCConstantValue>(d, acl::generateVEConstant(v)));
524 addSlices(*bc,sl);
525 return bc;
526 }
527
528 SPNumMethod generateBCConstantValue(SPAbstractDataWithGhostNodes d,
529 double v,
530 SPAbstractDataWithGhostNodes map)
531 {
532 return make_shared<BCConstantValueMap>(d, acl::generateVEConstant(v), map);
533 }
534
535 SPNumMethod generateBCConstantValue(SPAbstractDataWithGhostNodes d,
536 AVec<> v,
537 SPAbstractDataWithGhostNodes map)
538 {
539 return make_shared<BCConstantValueMap>(d, acl::generateVEConstant(v), map);
540 }
541
542 SPNumMethod generateBCConstantValue(SPAbstractDataWithGhostNodes d,
543 SPPositionFunction v,
544 SPAbstractDataWithGhostNodes map)
545 {
546 return make_shared<BCValuePFMap>(d, v, map);
547 }
548
549 SPNumMethod generateBCConstantValueMiddlePoint(SPAbstractDataWithGhostNodes d,
550 double v,
551 SPAbstractDataWithGhostNodes map,
552 const VectorTemplate *const t)
553 {
554 return make_shared<BCConstantValueMiddlePointMap>
555 (d, acl::generateVEConstant(v), map, t);
556 }
557
558 SPNumMethod generateBCConstantValueMiddlePoint(SPAbstractDataWithGhostNodes d,
559 AVec<> v,
560 SPAbstractDataWithGhostNodes map,
561 const VectorTemplate *const t)
562 {
563 return make_shared<BCConstantValueMiddlePointMap>
564 (d, acl::generateVEConstant(v), map, t);
565 }
566
567 SPBCond generateBCConstantGradient(SPAbstractDataWithGhostNodes d,
568 double v,
569 const VectorTemplate *const t,
570 const std::vector<SlicesNames> & sl)
571 {
572 auto bc(make_shared<BCConstantGradient>(d, acl::generateVEConstant(v),t));
573 addSlices(*bc,sl);
574 return bc;
575 }
576
577 SPNumMethod generateBCConstantGradient(SPAbstractDataWithGhostNodes d,
578 double v,
579 SPAbstractDataWithGhostNodes map,
580 const VectorTemplate *const t)
581 {
582 return make_shared<BCConstantGradientMap>
583 (d, acl::generateVEConstant(v), map, t);
584 }
585
586 SPNumMethod generateBCConstantGradient(SPAbstractDataWithGhostNodes d,
587 AVec<> v,
588 SPAbstractDataWithGhostNodes map,
589 const VectorTemplate *const t)
590 {
591 return make_shared<BCConstantGradientMap>
592 (d, acl::generateVEConstant(v), map, t);
593 }
594
595 SPNumMethod generateBCConstantGradient(SPAbstractDataWithGhostNodes d,
596 double v,
597 SPAbstractDataWithGhostNodes map,
598 SPAbstractDataWithGhostNodes computationalDomain,
599 const VectorTemplate *const t)
600 {
601 return make_shared<BCConstantGradientMap>
602 (d, acl::generateVEConstant(v), map, computationalDomain, t);
603 }
604
605
606 SPNumMethod generateBCConstantGradient2(SPAbstractDataWithGhostNodes d,
607 double v,
608 SPAbstractDataWithGhostNodes map,
609 const VectorTemplate *const t)
610 {
611 return make_shared<BCConstantGradientMap2>
612 (d, acl::generateVEConstant(v), map, t);
613 }
614
615 SPNumMethod generateBCConstantGradient2(SPAbstractDataWithGhostNodes d,
616 AVec<> v,
617 SPAbstractDataWithGhostNodes map,
618 const VectorTemplate *const t)
619 {
620 return make_shared<BCConstantGradientMap2>
621 (d, acl::generateVEConstant(v), map, t);
622 }
623
624 SPNumMethod generateBCConstantGradient2(SPAbstractDataWithGhostNodes d,
625 double v,
626 SPAbstractDataWithGhostNodes map,
627 SPAbstractDataWithGhostNodes computationalDomain,
628 const VectorTemplate *const t)
629 {
630 return make_shared<BCConstantGradientMap2>
631 (d, acl::generateVEConstant(v), map, computationalDomain, t);
632 }
633
634 } // asl
635