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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
// (c) 2019 Deepak kunhappan : deepak.kunhappan@3sr-grenoble.fr; deepak.kn1990@gmail.com
#ifdef YADE_MPI
#include "FoamCoupling.hpp"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wsuggest-override"
#pragma GCC diagnostic ignored "-Wcast-function-type"
#include <cstdlib>
#include <mpi.h>
#pragma GCC diagnostic pop
#include <pkg/common/Box.hpp>
#include <pkg/common/Facet.hpp>
#include <pkg/common/Grid.hpp>
#include <pkg/common/InsertionSortCollider.hpp>
#include <pkg/common/Sphere.hpp>
namespace yade { // Cannot have #include directive inside.
CREATE_LOGGER(FoamCoupling);
YADE_PLUGIN((FoamCoupling));
YADE_PLUGIN((FluidDomainBbox));
CREATE_LOGGER(FluidDomainBbox);
YADE_PLUGIN((Bo1_FluidDomainBbox_Aabb));
void Bo1_FluidDomainBbox_Aabb::go(const shared_ptr<Shape>& cm, shared_ptr<Bound>& bv, const Se3r&, const Body*)
{
FluidDomainBbox* domain = static_cast<FluidDomainBbox*>(cm.get());
if (!bv) { bv = shared_ptr<Bound>(new Aabb); }
Aabb* aabb = static_cast<Aabb*>(bv.get());
aabb->min = scene->isPeriodic ? scene->cell->wrapPt(domain->minBound) : domain->minBound;
aabb->max = scene->isPeriodic ? scene->cell->wrapPt(domain->maxBound) : domain->maxBound;
return;
}
void FoamCoupling::SetFoamSolver(const std::string& solverName, unsigned numProcs)
{
foamSolverName = solverName;
numFoamProcs = numProcs;
}
void FoamCoupling::StartFoamSolver()
{
assert(initDone == false && "Coupling has been initialized!");
assert(!foamSolverName.empty() && "OpenFOAM solver has not been set.");
if (foamSolverName.empty()) LOG_ERROR("OpenFoam environment not found. Is environment variable WM_PROJECT_DIR defined?")
scene = Omega::instance().getScene().get();
// local rank and sizes
MPI_Comm_rank(selfComm(), &localRank);
MPI_Comm_size(selfComm(), &localCommSize);
// spawn the openfoam process with the requested solver.
// char *solverName = const_cast<char*>(foamSolverName.data());
if (localRank == 0) std::cout << "Starting OpenFOAM solver --> " << foamSolverName << " with number of processes = " << numFoamProcs << "\n";
std::string par = "-parallel";
char** argVs = new char*[2];
argVs[0] = const_cast<char*>(par.c_str());
argVs[1] = NULL;
MPI_Comm_spawn(foamSolverName.data(), argVs, numFoamProcs, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &FOAMCOMM, MPI_ERRCODES_IGNORE);
if (localRank == 0)
std::cout << "OpenFOAM sovler " << foamSolverName << " started"
<< "\n";
// merge the openfoam child communicator for communications.
MPI_Intercomm_merge(FOAMCOMM, 0, &INTRACOMM); // all openfoam communications are now done on INTRACOMM.
std::cout << "comms merged" << std::endl;
MPI_Comm_size(INTRACOMM, &worldCommSize);
MPI_Comm_remote_size(FOAMCOMM, &foamCommSize);
// sanity check of the FOAMCOMM and numFoamProcs
if (localRank == 0) {
assert(foamCommSize == numFoamProcs && "possible MPI error, difference in FoamCommunicator size and number of OpenFOAM processes.");
assert(worldCommSize == foamCommSize + localCommSize && "invalid intracommunicator size");
}
//
if (localCommSize == 1) serialYade = true;
stride = localCommSize;
// initiate the communication with OF, get the mesh bounding boxes from openfoam.
getFluidDomainBbox();
// FIXME!
if (serialYade) {
// use the collider once to get interactions between the bodies in coupling and the fluid domain boxes.
// This is only needed in the serial case. In the parallel case, the collider is expliclty called
// to create interactions between the SubDomains, bodies, and Fluid domains...
// Better way to do this? or is there a way to 'set' interactions between specified pair of bodies?
// Is this the right way to do?
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &commSize);
// const auto &collider = YADE_PTR_CAST<InsertionSortCollider>(scene->engineByName("InsertionSortCollider"));
// collider->action();
}
std::cout << "got the fluid boxes and init done" << std::endl;
initDone = true;
}
void FoamCoupling::setNumParticles(int np) { numParticles = np; }
void FoamCoupling::setIdList(const std::vector<int>& alist)
{
bodyList.clear();
bodyList.resize(alist.size());
for (unsigned int i = 0; i != bodyList.size(); ++i) {
bodyList[i] = alist[i];
}
bodyListModified = true;
}
void FoamCoupling::insertBodyId(int bId)
{
const auto& iter = std::find(bodyList.begin(), bodyList.end(), bId);
if (iter != bodyList.end()) {
LOG_WARN("Body Id " << bId << " already exists in coupling. ")
} else {
bodyList.push_back(bId);
}
bodyListModified = true;
}
bool FoamCoupling::eraseId(int bId)
{
auto it = std::find(bodyList.begin(), bodyList.end(), bId);
if (it != bodyList.end()) {
bodyList.erase(it);
return true;
} else {
LOG_ERROR("Id not found in list of ids in coupling");
return false;
}
bodyListModified = true;
}
int FoamCoupling::getNumBodies() { return bodyList.size(); }
std::vector<int> FoamCoupling::getIdList() { return bodyList; }
void FoamCoupling::exchangeDeltaT()
{
// Recv foamdt first and broadcast;
MPI_Recv(&foamDeltaT, 1, MPI_DOUBLE, 1, TAG_FLUID_DT, MPI_COMM_WORLD, &status);
//bcast yadedt to others.
Real yadeDt = scene->dt;
MPI_Bcast(&yadeDt, 1, MPI_DOUBLE, rank, INTRACOMM);
// calculate the interval . TODO: to include hydrodynamic time scale if inertial in openfoam
// here -> hDeltaT = getViscousTimeScale();
dataExchangeInterval = (long int)((yadeDt < foamDeltaT) ? foamDeltaT / yadeDt : 1);
}
Real FoamCoupling::getViscousTimeScale()
{
// Real hDeltaT = 0.0;
// Real dummy = 1e9;
//
// MPI_Allreduce(&dummy, &hDeltaT, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
//
return 0;
}
void FoamCoupling::getFluidDomainBbox()
{
/* get the bounding box of the grid from each fluid solver processes, this gird minmax is used to set the min/max of the body of shape FluidDomainBbox.
All Yade processes have ranks from 0 to yadeCommSize - 1 in the INTRACOMM communicator, the fluid Ranks are then from yadeCommSize to size(M
PI_COMM_WORLD) -1, all yade ranks receive the min max of the fluid domains, and insert it to their body containers. The fluid subdomain bodies have subdomain=0, they are actually owned
by the master process (rank=0) in the yade communicator. */
std::vector<std::vector<double>> minMaxBuff;
//alloc memory
for (int i = 0; i != foamCommSize; ++i) {
std::vector<double> buff(6, 1e-50);
minMaxBuff.push_back(buff);
}
//recv the grid minmax from each rank from foam solver. The FOAM ranks are 'higher', i.e. in the INTRACOMM, they follow after the Yade ranks.
for (int rnk = 0; rnk != foamCommSize; ++rnk) {
// MPI_Status status;
std::vector<double>& buff = minMaxBuff[rnk];
MPI_Recv(&buff.front(), 6, MPI_DOUBLE, rnk + stride, TAG_GRID_BBOX, INTRACOMM, &status);
}
//
fluidDomains.resize(foamCommSize);
//create fluidDomainBbox bodies and get their ids.
for (int fd = 0; fd != foamCommSize; ++fd) {
shared_ptr<Body> flBody(shared_ptr<Body>(new Body()));
shared_ptr<FluidDomainBbox> flBodyshape(shared_ptr<FluidDomainBbox>(new FluidDomainBbox()));
flBodyshape->setMinMax(minMaxBuff[fd]);
flBodyshape->domainRank = stride + fd;
flBodyshape->hasIntersection = false;
flBody->shape = flBodyshape;
//if (!serialYade) {flBody->subdomain = 0; } else {flBody->subdomain=1;} // simply assigning a dummy so that it works in serial case ( single yade proc)
flBody->setIsFluidDomainBbox(true);
fluidDomains[fd] = scene->bodies->insert(flBody);
}
}
void FoamCoupling::buildSharedIdsMap()
{
/*Builds the list of ids interacting with a fluid subdomain and stores those body ids that has intersections with several fluid domains.
sharedIdsMapIndx = a vector of std::pair<Body::id_t, std::map<fluidDomainId, indexOfthebodyinthefluidDomain aka index in flbdy-> bIds> > */
inCommunicationProc.clear();
// const shared_ptr<Subdomain>& subd = YADE_PTR_CAST<Subdomain>((*scene->bodies)[scene->thisSubdomainId]->shape); //not needed as we have localIds list.
for (const auto& bodyId : localIds) {
std::map<int, int> testMap;
const auto& bIntrs = (*scene->bodies)[bodyId]->intrs;
for (const auto& itIntr : bIntrs) {
const shared_ptr<Interaction>& intr = itIntr.second;
Body::id_t otherId;
if (bodyId == intr->getId1()) {
otherId = intr->getId2();
} else {
otherId = intr->getId1();
}
const auto& otherBody = Body::byId(otherId, scene);
if (otherBody->getIsFluidDomainBbox()) {
const shared_ptr<FluidDomainBbox>& flbox = YADE_PTR_CAST<FluidDomainBbox>(otherBody->shape);
flbox->bIds.push_back(bodyId);
if (!flbox->hasIntersection) { flbox->hasIntersection = true; }
int indx = (flbox->bIds.size()) - 1;
testMap.insert(std::make_pair(
otherId,
indx)); // get the fluiddomainbbox body id and index in flbody->bIds, this will be used in the verifyTracking function
}
}
if (testMap.size() > 1) { // this body has intersections with more than one fluid domains, hence this is a shared id .
sharedIdsMapIndx.push_back(std::make_pair(bodyId, testMap));
}
}
//for quickly identifying fluid procs.
for (const auto& fluidId : fluidDomains) {
const shared_ptr<Body>& flb = (*scene->bodies)[fluidId];
if (flb) {
const shared_ptr<FluidDomainBbox>& flBox = YADE_PTR_CAST<FluidDomainBbox>(flb->shape);
if (flBox->bIds.size() > 0) { inCommunicationProc.push_back(std::make_pair(flBox->domainRank, flBox->bIds.size())); }
}
}
}
int FoamCoupling::ifSharedIdMap(const Body::id_t& testId)
{
int res = -1;
//std::vector<std::pair<int, std::map<int, int> > >::iterator
auto it = std::find_if(sharedIdsMapIndx.begin(), sharedIdsMapIndx.end(), [&testId](const std::pair<int, std::map<int, int>> elem) -> bool {
return testId == elem.first;
});
if (it != sharedIdsMapIndx.end()) { res = it - sharedIdsMapIndx.begin(); }
return res;
}
void FoamCoupling::buildLocalIds()
{
//if (localRank==yadeMaster and not serialYade) { return; } // master has no bodies.
if (bodyList.size() == 0) {
LOG_ERROR("Ids for coupling has no been set, FAIL!");
return;
}
if (!serialYade) {
if (localRank > yadeMaster) {
const shared_ptr<Subdomain>& subD = YADE_PTR_CAST<Subdomain>(scene->subD);
if (!subD) {
LOG_ERROR("subdomain not found for local rank/ world rank = " << localRank << " " << worldRank);
return;
}
for (const auto& testId : bodyList) {
std::vector<Body::id_t>::iterator iter = std::find(subD->ids.begin(), subD->ids.end(), testId); // can subD have ids sorted?
if (iter != subD->ids.end()) { localIds.push_back(*iter); }
}
} else {
return;
}
} else {
localIds = bodyList;
}
}
bool FoamCoupling::ifDomainBodies(const shared_ptr<Body>& b)
{
// check if body is subdomain, wall, facet, or other fluidDomainBbox
shared_ptr<Box> boxShape = YADE_PTR_DYN_CAST<Box>(b->shape);
shared_ptr<FluidDomainBbox> fluidShape = YADE_PTR_DYN_CAST<FluidDomainBbox>(b->shape);
shared_ptr<Facet> facetShape = YADE_PTR_DYN_CAST<Facet>(b->shape);
if (b->getIsSubdomain()) {
return true;
} else if (boxShape) {
return true;
} else if (facetShape) {
return true;
} else {
return false;
}
}
void FoamCoupling::sendIntersectionToFluidProcs()
{
// notify the fluid procs about intersection based on number of intersecting bodies.
// vector of sendRecvRanks, with each vector element containing the number of bodies, if no bodies, send negative val.
std::vector<int> sendRecvRanks(fluidDomains.size(), -1);
for (unsigned f = 0; f != fluidDomains.size(); ++f) {
const shared_ptr<Body>& fdomain = (*scene->bodies)[fluidDomains[f]];
if (fdomain) {
const shared_ptr<FluidDomainBbox>& fluidBox = YADE_PTR_CAST<FluidDomainBbox>(fdomain->shape);
if (fluidBox->bIds.size() > 0) {
sendRecvRanks[f] = fluidBox->bIds.size();
} else {
sendRecvRanks[f] = -1;
}
} else {
sendRecvRanks[f] = -1;
}
}
//
int buffSz = fluidDomains.size();
for (int rnk = 0; rnk != foamCommSize; ++rnk) {
MPI_Send(&sendRecvRanks.front(), buffSz, MPI_INT, rnk + stride, TAG_SZ_BUFF, INTRACOMM);
}
}
void FoamCoupling::sendBodyData()
{
/* send the particle data to the associated fluid procs. prtData -> pos, vel, angvel, raidus (for sphere), if fiber -> ori */
bool isPeriodic = scene->isPeriodic;
for (int f = 0; f != static_cast<int>(fluidDomains.size()); ++f) {
const shared_ptr<Body>& flbody = (*scene->bodies)[fluidDomains[f]];
if (flbody) {
const shared_ptr<FluidDomainBbox> flbox = YADE_PTR_CAST<FluidDomainBbox>(flbody->shape);
if (flbox->hasIntersection) {
std::vector<double> prtData(10 * flbox->bIds.size(), 1e-50);
for (unsigned int i = 0; i != flbox->bIds.size(); ++i) {
const shared_ptr<Body>& b = (*scene->bodies)[flbox->bIds[i]];
if (isPeriodic) {
const Vector3r& pos = scene->cell->wrapPt(b->state->pos);
prtData[10 * i] = pos[0];
prtData[10 * i + 1] = pos[1];
prtData[10 * i + 2] = pos[2];
} else {
prtData[10 * i] = b->state->pos[0];
prtData[10 * i + 1] = b->state->pos[1];
prtData[10 * i + 2] = b->state->pos[2];
}
prtData[10 * i + 3] = b->state->vel[0];
prtData[10 * i + 4] = b->state->vel[1];
prtData[10 * i + 5] = b->state->vel[2];
prtData[10 * i + 6] = b->state->angVel[0];
prtData[10 * i + 7] = b->state->angVel[1];
prtData[10 * i + 8] = b->state->angVel[2];
const shared_ptr<Sphere>& sph = YADE_PTR_CAST<Sphere>(b->shape);
prtData[10 * i + 9] = sph->radius;
}
int sz = prtData.size();
MPI_Send(&prtData.front(), sz, MPI_DOUBLE, flbox->domainRank, TAG_PRT_DATA, INTRACOMM);
}
}
}
}
void FoamCoupling::verifyParticleDetection()
{
/* check if the sent particles are located on the fluid procs, verify all particles (in fluid coupling) owned by the yade process has been accounted for.
Some particles may intersect the fluid domains bounding box but may not be actually inside the fluid mesh.
Method : Everty fluid proc sents a vector of it's search result. if found res = 1, else res =0, for each particle.
each yade rank receives this vector from intersecting fluid ranks, looks through the vector to find the fails.
if fail is found : see if this id is a sharedid. if not this particle has been 'lost'. if shared id : check the vector of verifyTracking of the intersecting fluid domain till found in
at least one intersecting fluid box. if not particle has been lost. */
//std::map<int, std::vector<int> > verifyTracking; //vector containing domainRank, vector of "found/misses" for each body, miss = -1, found = 1.
std::vector<std::pair<int, std::vector<int>>> verifyTracking;
for (const auto& proc : inCommunicationProc) {
std::vector<int> vt(proc.second, -1);
verifyTracking.push_back(std::make_pair(proc.first, std::move(vt)));
}
// recv the vec.
for (auto& it : verifyTracking) {
std::vector<int>& vt = it.second;
int rnk = it.first;
// MPI_Status status;
int buffSz = vt.size();
MPI_Recv(&vt.front(), buffSz, MPI_INT, rnk, TAG_SEARCH_RES, INTRACOMM, &status);
}
//check for misses
std::vector<Body::id_t> unFoundSharedIds;
for (const auto& vt : verifyTracking) {
const int& flBdyIndx = abs(vt.first - stride);
const shared_ptr<FluidDomainBbox>& flbody = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[fluidDomains[flBdyIndx]]->shape);
int bIndx = 0;
for (const auto& val : vt.second) {
if (val < 0) {
// this body was not found in the fluid domain.
const Body::id_t& testId = (*scene->bodies)[flbody->bIds[bIndx]]->id;
// check if this body is a sharedId from sharedIdsMap.
int sharedIndx = ifSharedIdMap(testId);
if (sharedIndx < 0) {
const Vector3r& pos = (*scene->bodies)[testId]->state->pos;
LOG_ERROR(
"Particle ID = " << testId << " pos = " << pos[0] << " " << pos[1] << " " << pos[2]
<< " was not found in fluid domain"
<< "lost Particle in proc = " << localRank);
} else {
Body::id_t unfoundId = testId;
unFoundSharedIds.push_back(unfoundId);
}
}
++bIndx;
}
}
//check if the 'sharedIds' has been located in any of the fluid procs. (REWRITE FROM HERE)
if (unFoundSharedIds.size() > 0) {
for (const auto& idPair : sharedIdsMapIndx) {
const auto& bodyId = idPair.first;
int foundCount = 0;
//const int mpSz = idPair.second.size();
bool found = false;
for (const auto& fdIndx : idPair.second) {
const shared_ptr<FluidDomainBbox>& flbox = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[fdIndx.first]->shape);
for (const auto& vt : verifyTracking) {
if (vt.first == flbox->domainRank) {
if (vt.second[fdIndx.second] > 0) {
found = true;
++foundCount;
// uncomment me for debugging..
// std::cout << "foundcount for bId = " << idPair.first << " " << foundCount << std::endl;
// std::cout << "totsize for bId = " << idPair.first << " " << idPair.second.size() << std::endl;
}
}
}
}
if (!found) {
const Vector3r& pos = (*scene->bodies)[bodyId]->state->pos;
LOG_ERROR(
"Particle ID (SHARED ID ) = " << bodyId << " pos = " << pos[0] << " " << pos[1] << " " << pos[2]
<< " was not found in fluid domain"
<< " lost particle in proc " << localRank);
}
}
}
// for (auto& idPair : sharedIdsMapIndx){
// for (auto iter = idPair.second.cbegin(); iter != idPair.second.cend();){
// auto fdIndx = *iter;
// const shared_ptr<FluidDomainBbox>& flbox = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[fdIndx.first]->shape);
// for (const auto& vt : verifyTracking){
// if (vt.first == flbox->domainRank){
// if (vt.second[fdIndx.second] > 0) {
// ; ++iter;}
// } else {
// idPair.second.erase(iter++); // remove proc and indx from sharedIdsMap
// }
// }
// }
// }
// for (const auto& idPair : sharedIdsMapIndx){
// if (!idPair.second.size()) {
// const auto& bodyId = idPair.first;
// const Vector3r& pos = (*scene->bodies)[bodyId]->state->pos;
// LOG_ERROR("Particle ID (SHARED ID ) = " << bodyId << " pos = " << pos[0] << " " << pos[1] << " " << pos[2] << " was not found in fluid domain" << " lost particle in proc " << localRank);
// }
// }
std::vector<int> sharedIdsCount(inCommunicationProc.size(), 0);
std::vector<std::vector<std::vector<int>>> sharedBuff;
sharedBuff.resize(inCommunicationProc.size());
// //prepare to send shared ids
for (int ii = 0; ii != (int)inCommunicationProc.size(); ++ii) {
for (const auto& idPair : sharedIdsMapIndx) {
const auto& indxMap = idPair.second;
// if and only if the size of the map is > 1, the particle has been found in more than one foam procs.
if (indxMap.size() > 1) {
const auto& indx = findRankSharedIndxMap(indxMap, inCommunicationProc[ii].first);
if (indx >= 0) {
std::vector<int> buff;
buff.push_back(indx); // index of the id in the fluid proc's lst of particles
for (const auto& val : indxMap) {
const auto& flbx = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[val.first]->shape);
if (inCommunicationProc[ii].first == flbx->domainRank) continue;
buff.push_back(val.first);
}
sharedBuff[ii].push_back(buff);
}
}
}
}
// send info on number of 'shared' ids
std::vector<MPI_Request> mpiReqs;
for (unsigned i = 0; i != sharedBuff.size(); ++i) {
int buffSz = (int)sharedBuff[i].size();
MPI_Request req;
MPI_Isend(&buffSz, 1, MPI_INT, inCommunicationProc[i].first, TAG_SHARED_ID, INTRACOMM, &req);
mpiReqs.push_back(req);
}
for (auto& req : mpiReqs) {
// MPI_Status status;
MPI_Wait(&req, &status);
}
mpiReqs.clear();
for (unsigned i = 0; i != sharedBuff.size(); ++i) {
if (!sharedBuff[i].size()) continue;
for (unsigned j = 0; j != sharedBuff[i].size(); ++j) {
MPI_Request req;
std::vector<int>& buff = sharedBuff[i][j];
int sz = (int)buff.size();
MPI_Isend(&buff.front(), sz, MPI_INT, inCommunicationProc[i].first, TAG_SHARED_ID, INTRACOMM, &req);
mpiReqs.push_back(req);
}
}
if (mpiReqs.size()) {
for (auto& rq : mpiReqs) {
// MPI_Status status;
MPI_Wait(&rq, &status);
}
mpiReqs.clear();
}
}
int FoamCoupling::findRankSharedIndxMap(const std::map<int, int>& tMap, const int& rnk)
{
// if found, return the index of the particle
for (const auto& prcIndx : tMap) {
const auto& flbx = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[prcIndx.first]->shape);
if (rnk == flbx->domainRank) return prcIndx.second; // return the particle index
}
return -1;
}
void FoamCoupling::getParticleForce()
{
hForce.clear();
for (const auto& proc : inCommunicationProc) {
std::vector<double> forceVec(6 * proc.second, 0.0);
hForce.push_back(std::make_pair(proc.first, std::move(forceVec)));
}
for (auto& recvForce : hForce) {
std::vector<double>& tmpForce = recvForce.second;
int recvRank = recvForce.first;
int buffSz = tmpForce.size();
/* fluid procs having no particles (those in inCommunicationProc) will send 0 force, torque */
MPI_Recv(&tmpForce.front(), buffSz, MPI_DOUBLE, recvRank, TAG_FORCE, INTRACOMM, &status);
}
}
void FoamCoupling::resetFluidDomains()
{
// clear the vector ids held fluidDomainBbox->bIds
if (localRank == yadeMaster and not serialYade) { return; }
for (unsigned f = 0; f != fluidDomains.size(); ++f) {
const shared_ptr<Body>& fdomain = (*scene->bodies)[fluidDomains[f]];
if (fdomain) {
const shared_ptr<FluidDomainBbox>& fluidBox = YADE_PTR_CAST<FluidDomainBbox>(fdomain->shape);
fluidBox->bIds.clear();
}
}
sharedIdsMapIndx.clear();
localIds.clear();
}
void FoamCoupling::setHydroForce()
{
// add the force
if (localRank == yadeMaster and not serialYade) { return; }
for (const auto& rf : hForce) {
int indx = abs(rf.first - localCommSize);
const std::vector<double>& forceVec = rf.second;
const shared_ptr<FluidDomainBbox>& flbox = YADE_PTR_CAST<FluidDomainBbox>((*scene->bodies)[fluidDomains[indx]]->shape);
for (unsigned int i = 0; i != flbox->bIds.size(); ++i) {
Vector3r fx;
fx[0] = forceVec[6 * i];
fx[1] = forceVec[6 * i + 1];
fx[2] = forceVec[6 * i + 2];
Vector3r tx;
tx[0] = forceVec[6 * i + 3];
tx[1] = forceVec[6 * i + 4];
tx[2] = forceVec[6 * i + 5];
scene->forces.addForce(flbox->bIds[i], fx);
scene->forces.addTorque(flbox->bIds[i], tx);
}
}
}
void FoamCoupling::exchangeDeltaTParallel()
{
// Recv foamdt first and broadcast;
if (!serialYade) {
if (localRank == yadeMaster) {
// MPI_Status status;
int fluidMaster = stride;
MPI_Recv(&foamDeltaT, 1, MPI_DOUBLE, fluidMaster, TAG_FLUID_DT, INTRACOMM, &status);
}
//bcast the fluidDt to all yade_procs.
// Real yadeDt = scene-> dt;
MPI_Bcast(&foamDeltaT, 1, MPI_DOUBLE, yadeMaster, selfComm());
//do a MPI_Allreduce (min) and get the minDt of all the yade procs..
Real myDt = scene->dt;
Real yadeDt;
MPI_Allreduce(&myDt, &yadeDt, 1, MPI_DOUBLE, MPI_MIN, selfComm());
// send the minDt to fluid proc (master .. )
if (localRank == yadeMaster) {
int fluidMaster = stride;
MPI_Send(&yadeDt, 1, MPI_DOUBLE, fluidMaster, TAG_YADE_DT, INTRACOMM);
}
// calculate the interval . TODO: to include hydrodynamic time scale if inertial in openfoam
// here -> hDeltaT = getViscousTimeScale();
dataExchangeInterval = (long int)((yadeDt < foamDeltaT) ? foamDeltaT / yadeDt : 1);
} else {
exchangeDeltaT();
}
}
void FoamCoupling::runCoupling()
{
if (localRank > yadeMaster or serialYade) {
buildLocalIds();
buildSharedIdsMap();
sendIntersectionToFluidProcs();
sendBodyData();
verifyParticleDetection();
getParticleForce();
}
}
void FoamCoupling::action()
{
if (!initDone) { StartFoamSolver(); }
if (exchangeData()) {
resetFluidDomains();
runCoupling();
exchangeDeltaTParallel();
}
setHydroForce();
}
bool FoamCoupling::exchangeData() { return scene->iter % dataExchangeInterval == 0; }
void FoamCoupling::killMPI()
{
// int value = 1; // Use the same value as in OpenFOAM
// MPI_Bcast(&value, 1, MPI_INT, 0, INTRACOMM); // Broadcast from rank 0 in Yade
// Now sync all processes
//MPI_Barrier(INTRACOMM);
// MPI_Finalize(); // Finalize MPI
MPI_Abort(INTRACOMM, -100);
}
void FoamCoupling::checkFoamVersion()
{
const char* wmProjectDir = std::getenv("WM_PROJECT_DIR");
if (wmProjectDir != nullptr) {
foamPath = std::string(wmProjectDir);
string version(std::getenv("WM_PROJECT_VERSION"));
if (!version.empty() && version.front() == 'v') //remove the leading 'v'
version.erase(0, 1);
foamVersion = std::stoi(version);
}
}
} // namespace yade
#endif
|