Skip to content

Commit 9c1f97d

Browse files
author
Avirup Sircar
committed
modified generateMesh which seemed ok might delete later
1 parent 2ea14cc commit 9c1f97d

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

analysis/classicalEnrichmentComparison/PSP/KSDFTClassicalUniformQuad/TestKohnShamDft.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,19 @@ int main(int argc, char** argv)
556556

557557
adaptiveMesh.createMesh(*triangulationBase);
558558

559+
// std::fstream out;
560+
// out.open("grid.vtk", std::fstream::out);
561+
// if (out.is_open()) {
562+
// utils::mpi::MPIBarrier(comm);
563+
// triangulationBase->writeToVtkFile(out);
564+
// utils::mpi::MPIBarrier(comm);
565+
// out.close();
566+
// std::cout << "Data was written to grid.vtk\n";
567+
// }
568+
// else {
569+
// std::cout << "Error opening file\n";
570+
// }
571+
559572
std::shared_ptr<basis::ParentToChildCellsManagerBase> parentToChildCellsManager = std::make_shared<basis::ParentToChildCellsManagerDealii<dim>>();
560573

561574
std::vector<double> smearedChargeRadiusVec(atomCoordinatesVec.size(),rc);

src/basis/GenerateMesh.cpp

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <basis/Defaults.h>
2929
#include <basis/GenerateMesh.h>
30+
#include <cmath>
3031

3132
namespace dftefe
3233
{
@@ -204,7 +205,7 @@ namespace dftefe
204205
i =
205206
std::pow(2,
206207
round(log2(
207-
(std::max(4.0, largestMeshSizeAroundAtom) /*4.0*/) /
208+
(std::max(8.0, largestMeshSizeAroundAtom) /*4.0*/) /
208209
largestMeshSizeAroundAtom))) *
209210
largestMeshSizeAroundAtom;
210211
}
@@ -384,21 +385,129 @@ namespace dftefe
384385
return isAnyCellRefined;
385386
}
386387

388+
bool
389+
GenerateMesh::refineInsideSystemNonPeriodicAlgorithm(
390+
TriangulationBase & triangulation,
391+
std::vector<double> &maxAtomCoordinates,
392+
std::vector<double> &minAtomCoordinates)
393+
{
394+
auto cell = triangulation.beginLocal();
395+
auto endc = triangulation.endLocal();
396+
397+
double currentMeshSize = (*cell)->minimumVertexDistance();
398+
bool isAnyCellRefined = false;
399+
size_type cellIndex = 0;
400+
for (; cell != endc; ++cell)
401+
{
402+
utils::Point center(d_dim, 0.0);
403+
(*cell)->center(center);
404+
405+
bool cellRefineFlag = false;
406+
407+
double boundingBoxDom = 0;
408+
for (int i = 0; i < d_dim; i++)
409+
{
410+
double axesLen = std::max(std::abs(maxAtomCoordinates[i]),
411+
std::abs(minAtomCoordinates[i])) +
412+
std::max(15.0, d_radiusAroundAtom);
413+
if (boundingBoxDom < axesLen)
414+
boundingBoxDom = axesLen;
415+
}
416+
bool val = true;
417+
for (int i = 0; i < d_dim; i++)
418+
{
419+
if (std::abs(center[i]) > boundingBoxDom)
420+
{
421+
val = false;
422+
break;
423+
}
424+
// val += std::pow((center[i]/ axesLen),2);
425+
}
426+
if (val && currentMeshSize > 4)
427+
{
428+
cellRefineFlag = true;
429+
}
430+
431+
size_type cellRefineFlagSizeType = (size_type)cellRefineFlag;
432+
433+
utils::mpi::MPIAllreduce<utils::MemorySpace::HOST>(
434+
utils::mpi::MPIInPlace,
435+
&cellRefineFlagSizeType,
436+
1,
437+
utils::mpi::Types<size_type>::getMPIDatatype(),
438+
utils::mpi::MPIMax,
439+
d_mpiInterpoolCommunicator);
440+
441+
cellRefineFlag = cellRefineFlagSizeType;
442+
443+
//
444+
// set coarsen flags
445+
if (cellRefineFlag)
446+
{
447+
(*cell)->setRefineFlag();
448+
isAnyCellRefined = true;
449+
}
450+
cellIndex += 1;
451+
}
452+
return isAnyCellRefined;
453+
}
454+
387455
void
388456
GenerateMesh::createMesh(TriangulationBase &triangulation)
389457
{
390458
triangulation.initializeTriangulationConstruction();
391459
generateCoarseMesh(triangulation, d_isPeriodicFlags);
392460

461+
bool refineFlag = true;
462+
// all directions non periodic, note, can be extended to some dicrecs.
463+
// non-periodic
464+
if (!(std::any_of(d_isPeriodicFlags.begin(),
465+
d_isPeriodicFlags.end(),
466+
[](bool v) { return v; })))
467+
{
468+
std::vector<double> maxAtomCoordinates(d_dim, 0);
469+
std::vector<double> minAtomCoordinates(d_dim, 0);
470+
471+
for (int j = 0; j < d_dim; j++)
472+
{
473+
for (int i = 0; i < d_atomCoordinates.size(); i++)
474+
{
475+
if (maxAtomCoordinates[j] < d_atomCoordinates[i][j])
476+
maxAtomCoordinates[j] = d_atomCoordinates[i][j];
477+
if (minAtomCoordinates[j] > d_atomCoordinates[i][j])
478+
minAtomCoordinates[j] = d_atomCoordinates[i][j];
479+
}
480+
}
481+
refineFlag =
482+
refineInsideSystemNonPeriodicAlgorithm(triangulation,
483+
maxAtomCoordinates,
484+
minAtomCoordinates);
485+
486+
// This sets the global refinement sweep flag
487+
size_type refineFlagSizeType = (size_type)refineFlag;
488+
489+
utils::mpi::MPIAllreduce<utils::MemorySpace::HOST>(
490+
utils::mpi::MPIInPlace,
491+
&refineFlagSizeType,
492+
1,
493+
utils::mpi::Types<size_type>::getMPIDatatype(),
494+
utils::mpi::MPIMax,
495+
d_mpiDomainCommunicator);
496+
497+
refineFlag = refineFlagSizeType;
498+
}
499+
triangulation.executeCoarseningAndRefinement();
500+
triangulation.finalizeTriangulationConstruction();
501+
393502
d_triaCurrentRefinement.clear();
394503

395504
//
396505
// Call only refinementAlgorithm. Multilevel refinement is
397506
// performed until refinementAlgorithm does not set refinement flags on
398507
// any cell.
399508
//
400-
size_type numLevels = 0;
401-
bool refineFlag = true;
509+
size_type numLevels = 0;
510+
refineFlag = true;
402511
while (refineFlag)
403512
{
404513
refineFlag = false;

src/basis/GenerateMesh.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ namespace dftefe
106106
std::map<size_type, size_type> &cellIdToCellRefineFlagMapLocal,
107107
const basis::CellMappingBase & cellMapping);
108108

109+
bool
110+
refineInsideSystemNonPeriodicAlgorithm(
111+
TriangulationBase & triangulation,
112+
std::vector<double> &maxAtomCoordinates,
113+
std::vector<double> &minAtomCoordinates);
114+
109115
bool d_adaptiveWithFineMesh;
110116
size_type d_dim;
111117
double d_radiusAtAtom;

0 commit comments

Comments
 (0)