void DgSqrD4Grid2DS::setAddBoundaryChildren (const DgResAdd<DgIVec2D>& add, DgLocVector& vec) const { if (isCongruent() || radix() == 3) { // no boundary children in this topology; leave vec empty } else // must be aligned aperture 4 { DgLocation* tmpLoc = makeLocation(add); // D8 neighbors is what we want DgSqrD8Grid2D d8(network(), grids()[add.res() + 1]->backFrame(), "dummyD8"); d8.convert(tmpLoc); d8.setNeighbors(*tmpLoc, vec); grids()[add.res() + 1]->convert(vec); convert(vec); delete tmpLoc; } } // void DgSqrD4Grid2DS::setAddBoundaryChildren
void DgSqrD4Grid2DS::setAddParents (const DgResAdd<DgIVec2D>& add, DgLocVector& vec) const { //cout << " setAddParents: " << add << endl; if (isCongruent() || radix() == 3) { DgLocation* tmpLoc = makeLocation(add); grids()[add.res() - 1]->convert(tmpLoc); convert(tmpLoc); vec.push_back(*tmpLoc); delete tmpLoc; } else // must be aligned aperture 4 { // vertices lie in parents DgLocation* tmpLoc = makeLocation(add); DgPolygon* verts = makeVertices(*tmpLoc); delete tmpLoc; //cout << " verts 1: " << *verts << endl; grids()[add.res() - 1]->convert(*verts); //cout << " verts 2: " << *verts << endl; convert(*verts); //cout << " verts 3: " << *verts << endl; for (int i = 0; i < verts->size(); i++) { bool found = false; for (int j = 0; j < vec.size(); j++) { //cout << " " << i << " " << j << " " << (*verts)[i] << " " << vec[j]; if ((*verts)[i] == vec[j]) { //cout << " YES" << endl; found = true; break; } //cout << " NO" << endl; } if (!found) vec.push_back((*verts)[i]); } //cout << " parents: " << vec << endl; delete verts; } } // void DgSqrD4Grid2DS::setAddParents
DgSqrD4Grid2DS::~DgSqrD4Grid2DS (void) { for (unsigned long i = 0; i < grids().size(); i++) delete (*grids_)[i]; delete grids_; } // DgSqrD4Grid2DS::~DgSqrD4Grid2DS
void DgSqrD4Grid2DS::setAddInteriorChildren (const DgResAdd<DgIVec2D>& add, DgLocVector& vec) const { if (isCongruent() || radix() == 3) { const DgIVec2D& lowerLeft = add.address() * radix(); vector<DgAddressBase*>& v = vec.addressVec(); for (int i = 0; i < radix(); i++) { for (int j = 0; j < radix(); j++) { v.push_back(new DgAddress< DgResAdd<DgIVec2D> >( DgResAdd<DgIVec2D>(DgIVec2D(lowerLeft.i() + i, lowerLeft.j() + j), add.res() + 1))); } } } else // must be aligned aperture 4 { // only center square is interior DgLocation* tmpLoc = makeLocation(add); grids()[add.res() + 1]->convert(tmpLoc); vec.push_back(*tmpLoc); delete tmpLoc; } } // void DgSqrD4Grid2DS::setAddInteriorChildren
void BrlcadReader::findBoundaryCodes() { int num_grids = m_Grids.size(); QVector<vtkUnstructuredGrid*> grids(num_grids); qCopy(m_Grids.begin(), m_Grids.end(), grids.begin()); QVector<FaceFinder> finders(num_grids); for (int i_grid = 0; i_grid < num_grids; ++i_grid) { finders[i_grid].setGrid(grids[i_grid]); } EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code"); bool has_errors = false; QVector<bool> bc_exists(num_grids, false); for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) { int best_grid = -1; double L_min = 1e99; vec3_t x1 = cellCentre(m_Grid, id_cell); for (int i_grid = 0; i_grid < num_grids; ++i_grid) { double L = 1e99; vtkIdType id_closest = finders[i_grid].getClosestFace(x1, L); vec3_t x2(-999,-999,-999); if (id_closest != -1) x2 = cellCentre(m_Grids[i_grid], id_closest); if (id_closest != -1) { if (L < L_min) { best_grid = i_grid; L_min = L; } } } if (best_grid == -1) { has_errors = true; cell_code->SetValue(id_cell, 9999); } else { bc_exists[best_grid] = true; cell_code->SetValue(id_cell, best_grid + 1); } } GuiMainWindow::pointer()->clearBCs(); int bc_max = 1; QVector<int> bc_map(num_grids+1,9999); m_BC2GridIndex.clear(); for (int i_grid = 0; i_grid < num_grids; ++i_grid) { if (bc_exists[i_grid]) { bc_map[i_grid+1] = bc_max; GuiMainWindow::pointer()->addBC(bc_max, BoundaryCondition(m_BCNames[grids[i_grid]], "patch")); m_BC2GridIndex[bc_max] = i_grid; ++bc_max; } } for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) { if (cell_code->GetValue(id_cell) != 9999) { cell_code->SetValue(id_cell, bc_map[cell_code->GetValue(id_cell)]); } } if (has_errors) { GuiMainWindow::pointer()->addBC(9999, BoundaryCondition("error-faces", "patch")); } GuiMainWindow::pointer()->updateBoundaryCodes(true); }
DgSqrD4Grid2DS::DgSqrD4Grid2DS (DgRFNetwork& networkIn, const DgRF<DgDVec2D, long double>& backFrameIn, int nResIn, unsigned int apertureIn, bool isCongruentIn, bool isAlignedIn, const string& nameIn) : DgDiscRFS2D (networkIn, backFrameIn, nResIn, apertureIn, isCongruentIn, isAlignedIn, nameIn) { // determine the radix radix_ = static_cast<int>(sqrt(static_cast<float>(aperture()))); if (static_cast<unsigned int>(radix() * radix()) != aperture()) { report( "DgSqrD4Grid2DS::DgSqrD4Grid2DS() aperture must be a perfect square", DgBase::Fatal); } if (isAligned() && radix() != 2 && radix() != 3) { report("DgSqrD4Grid2DS::DgSqrD4Grid2DS() only aligned apertures 4 and 9 " " parent/children operators fully implemented", DgBase::Warning); } // do the grids long double fac = 1; DgDVec2D trans; if (isCongruent()) { trans = DgDVec2D(-0.5, -0.5); } else if (isAligned()) { trans = DgDVec2D(0.0, 0.0); } else { report("DgSqrD4Grid2DS::DgSqrD4Grid2DS() grid system must be either " "congruent, aligned, or both", DgBase::Fatal); } for (int i = 0; i < nRes(); i++) { string newName = name() + "_" + dgg::util::to_string(i); //cout << newName << " " << fac << ' ' << trans << endl; DgContCartRF* ccRF = new DgContCartRF(network(), newName + string("bf")); new Dg2WayContAffineConverter(backFrame(), *ccRF, (long double) fac, 0.0, trans); (*grids_)[i] = new DgSqrD4Grid2D(network(), *ccRF, newName); new Dg2WayResAddConverter<DgIVec2D, DgDVec2D, long double> (*this, *(grids()[i]), i); fac *= radix(); } } // DgSqrD4Grid2DS::DgSqrD4Grid2DS
bool Cube::save(QString const& filePath, Data::Bank& bank) { // Make sure we have the required data QList<Data::Geometry*> geometries(bank.findData<Data::Geometry>()); if (geometries.isEmpty()) { m_errors.append("No geometry information found"); return false; }else if (geometries.size() > 1) { QLOG_WARN() << "Ambiguous geometry information found in Cube parser"; } Data::Geometry* geometry(geometries.first()); QList<Data::GridData*> grids(bank.findData<Data::GridData>()); if (grids.isEmpty()) { m_errors.append("No grid data found"); return false; }else if (grids.size() > 1) { QLOG_WARN() << "More than one grid specified in Cube parser"; } Data::GridData* grid(grids.first()); QFile file(filePath); if (file.exists() || !file.open(QIODevice::WriteOnly)) { m_errors.append("Failed to open file for write"); return false; } QStringList header; header << "Cube file for " + grid->surfaceType().toString(); header << "Generated using IQmol"; QStringList coords(formatCoordinates(*geometry)); unsigned nx, ny, nz; grid->getNumberOfPoints(nx, ny, nz); qglviewer::Vec delta(grid->delta()); qglviewer::Vec origin(grid->origin()); origin *= Constants::AngstromToBohr; header << QString("%1 %2 %3 %4").arg(coords.size(), 5) .arg(origin.x, 13, 'f', 6) .arg(origin.y, 13, 'f', 6) .arg(origin.z, 13, 'f', 6); header << QString("%1 %2 %3 %4").arg(nx, 5).arg(delta.x, 13, 'f', 6) .arg(0.0, 13, 'f', 6) .arg(0.0, 13, 'f', 6); header << QString("%1 %2 %3 %4").arg(ny, 5).arg(0.0, 13, 'f', 6) .arg(delta.y, 13, 'f', 6) .arg(0.0, 13, 'f', 6); header << QString("%1 %2 %3 %4").arg(nz, 5).arg(0.0, 13, 'f', 6) .arg(0.0, 13, 'f', 6) .arg(delta.z, 13, 'f', 6); header << coords; QByteArray buffer; buffer.append(header.join("\n")); buffer.append("\n"); file.write(buffer); buffer.clear(); double w; unsigned col(0); for (unsigned i = 0; i < nx; ++i) { for (unsigned j = 0; j < ny; ++j) { for (unsigned k = 0; k < nz; ++k, ++col) { w = (*grid)(i, j, k); if (w >= 0.0) buffer += " "; buffer += QString::number(w, 'E', 5); if (col == 5) { col = -1; buffer += "\n"; }else { buffer += " "; } } file.write(buffer); buffer.clear(); } } buffer += "\n"; file.write(buffer); file.flush(); file.close(); return true; }
int main(int argc,char **argv) { #ifdef CH_MPI MPI_Init(&argc, &argv); #endif // registerDebugger(); // begin forever present scoping trick { pout()<<std::endl; Vector<std::string> names0(1, "phi"); Vector<int> refRatio(3,2); Vector<Real> coveredVal(1,3.0); const char* in_file = "sphere.inputs"; // read in an input file or use default file // if (argc > 1) // { // in_file = argv[1]; // } //parse input file ParmParse pp(0,NULL,NULL,in_file); RealVect center; Real radius; RealVect origin; RealVect dx; Box domain; ProblemDomain pDomain(domain); int eekflag = 0; LevelData<EBCellFAB> fine, med, coarse; LevelData<EBCellFAB> fineRHS, medRHS, coarseRHS; LevelData<EBCellFAB> fineResidual, mediumResidual, coarseResidual; Vector<LevelData<EBCellFAB>* > ebvector(3,NULL); Vector<LevelData<EBCellFAB>* > vresidual(3,NULL); Vector<LevelData<EBCellFAB>* > rhsvector(3,NULL); ebvector[0]=&coarse; ebvector[1]=&med; ebvector[2]=&fine; vresidual[0]=&coarseResidual; vresidual[1]=&mediumResidual; vresidual[2]=&fineResidual; rhsvector[0] = &coarseRHS; rhsvector[1] = &medRHS; rhsvector[2] = &fineRHS; readGeometryInfo(domain, dx, origin, center, radius); Box domainFine(domain), domainMedi, domainCoar; ProblemDomain pFine(domain); RealVect dxFine(dx), dxMedi, dxCoar; CH_assert(eekflag == 0); domainMedi = coarsen(domainFine, 2); domainCoar = coarsen(domainMedi, 2); dxMedi = 2.0*dxFine; dxCoar = 2.0*dxMedi; Vector<RealVect> xVec(3, IntVect::Unit); xVec[0]*= dxCoar; xVec[1]*= dxMedi; xVec[2]*= dxFine; Vector<DisjointBoxLayout> grids(3); ProblemDomain baseDomain(domainCoar); ProblemDomain pMed(domainMedi); Vector<ProblemDomain> pd(3); pd[0] = baseDomain; pd[1] = pMed; pd[2] = ProblemDomain(domainFine); RefCountedPtr<BaseBCValue>value(new DirichletBC()); DirichletPoissonDomainBC* domainBC = new DirichletPoissonDomainBC(); domainBC->setFunction(value); RefCountedPtr<BaseDomainBC> bc(domainBC); //make data holders Vector<int> comps(2,1); int steps= 5; int step = 0; while (step < steps) { eekflag = makeGeometry( domain, dx, origin, center, radius); //make grids //IntVectSet tags = mfIndexSpace->interfaceRegion(2); IntVectSet tags(domainCoar); tags.grow(1); makeHierarchy(grids, baseDomain, tags); const CH_XD::EBIndexSpace* ebisPtr = Chombo_EBIS::instance(); Vector<EBISLayout> layouts(3); EBISLayout& fineLayout = layouts[2]; ebisPtr->fillEBISLayout(fineLayout, grids[2], domainFine, 2); EBCellFactory fineFactory(fineLayout); ebvector[2]->define(grids[2], 1, IntVect::Unit, fineFactory); rhsvector[2]->define(grids[2], 1, IntVect::Zero, fineFactory); EBISLayout& medLayout = layouts[1]; ebisPtr->fillEBISLayout(medLayout, grids[1], domainMedi, 2); EBCellFactory medFactory(medLayout); ebvector[1]->define(grids[1], 1, IntVect::Unit, medFactory); rhsvector[1]->define(grids[1], 1, IntVect::Zero, medFactory); EBISLayout& coarseLayout = layouts[0]; ebisPtr->fillEBISLayout(coarseLayout, grids[0], domainCoar, 2); EBCellFactory coarseFactory(coarseLayout); ebvector[0]->define(grids[0], 1, IntVect::Unit, coarseFactory); rhsvector[0]->define(grids[0], 1, IntVect::Zero, coarseFactory); for (int lev=0; lev<3; lev++) { setValue(*rhsvector[lev], RHS(), pd[lev].domainBox(), xVec[lev], origin, true); } Vector<int> refRatio(3,2); int max_iter = 40; pp.get("max_iter", max_iter); Real eps = 1.e-6; pp.get("eps", eps); int relaxType; pp.get("relaxType",relaxType); DirichletPoissonDomainBCFactory* domDirBC = new DirichletPoissonDomainBCFactory(); domDirBC->setFunction(value); RefCountedPtr<BaseDomainBCFactory> domBC( domDirBC ); DirichletPoissonEBBCFactory* ebDirBC = new DirichletPoissonEBBCFactory(); ebDirBC->setFunction(value); RefCountedPtr<BaseEBBCFactory> ebBC( ebDirBC ); Vector<EBLevelGrid> eblgs(3); Vector<RefCountedPtr<EBQuadCFInterp> > quadCFI(3, RefCountedPtr<EBQuadCFInterp>()); for (int i=0; i<3; i++) { eblgs[i] = EBLevelGrid(grids[i], layouts[i], pd[i]); if (i > 0) { quadCFI[i] = RefCountedPtr<EBQuadCFInterp>( new EBQuadCFInterp(grids[i], grids[i-1], layouts[i], layouts[i-1], pd[i-1], refRatio[i-1], 1, *eblgs[i].getCFIVS())); } } EBAMRPoissonOpFactory opFact(eblgs, refRatio, quadCFI, xVec[0], RealVect::Zero, 4, relaxType, domBC, ebBC, 0.0, 1.0, 0.0, IntVect::Unit, IntVect::Zero); for (int i=0; i<3; i++) { LevelData<EBCellFAB> &phi=*ebvector[i], &rhs=*rhsvector[i], &residual=*vresidual[i]; LevelData<EBCellFAB> correction; DisjointBoxLayout dblMGCoar; EBISLayout ebislMGCoar; EBAMRPoissonOp* opPtr = opFact.AMRnewOp(pd[i]); EBAMRPoissonOp& op = *opPtr; RelaxSolver<LevelData<EBCellFAB> > solver; solver.define(&op, false); solver.m_imax = max_iter; solver.m_eps = eps; op.create(residual, rhs); op.create(correction, phi); op.setToZero(residual); op.setToZero(phi); op.residual(residual, phi, rhs); Real r2norm = op.norm(residual, 2); Real r0norm = op.norm(residual, 0); pout()<<indent<<"Residual L2 norm "<<r2norm<<"Residual max norm = " <<r0norm<<std::endl; solver.solve(phi, rhs); op.residual(residual, phi, rhs); r2norm = op.norm(residual, 2); r0norm = op.norm(residual, 0); pout()<<indent2<<"Residual L2 norm "<<r2norm<<" Residual max norm = " <<r0norm<<std::endl; delete opPtr; } #ifdef CH_USE_HDF5 sprintf(iter_str, "residual.%03d.%dd.hdf5",step, SpaceDim); Vector<std::string> names(1); names[0]="residual"; writeEBHDF5(iter_str, grids, vresidual, names, domainCoar, dxCoar[0], 1, step, refRatio, 3, true, coveredVal); sprintf(iter_str, "phi.%03d.%dd.hdf5",step, SpaceDim); names[0]="phi"; writeEBHDF5(iter_str, grids, ebvector ,names, domainCoar, dxCoar[0], 1, step, refRatio, 3, true, coveredVal); #endif step++; center[0]-= dx[0]/3.0; center[1]-= dx[1]/2.0; radius += dx[0]/6.0; Chombo_EBIS::instance()->clear(); pout()<<step<<std::endl; } pout() <<"\n "<<indent2<<pgmname<<" test passed " << endl; } // end scoping trick #ifdef CH_MPI MPI_Finalize(); #endif return 0; }
/** * Loops, producing a sequence of hdf5 files step0.hdf5, step1.hdf5, etc. * If handed first_step=n, n>0, on the command line, will load step<n>.hdf5 * and start from that step of the loop. * */ void EBRestart::mainFunc( const InputParams& a_inputs ) { EBRestart::GeomParams geometry( a_inputs ); int eekflag = 0; RefCountedPtr<EBIndexSpace> ebIndexSpace( new EBIndexSpace ); RefCountedPtr<EBIndexSpace> ebIndexSpace_old( new EBIndexSpace ); Vector<LevelData<EBCellFAB>* > ebvector(a_inputs.nlevs); Vector<LevelData<EBCellFAB>* > ebvector_old(a_inputs.nlevs); for ( int l=0;l<a_inputs.nlevs;++l ) { ebvector[l] = new LevelData<EBCellFAB>; ebvector_old[l] = new LevelData<EBCellFAB>; } CH_assert(eekflag == 0); Vector<DisjointBoxLayout> grids(a_inputs.nlevs); EBRestart::CheckSumMap checksums; // by step, by level checksums[a_inputs.first_step-1] = EBRestart::CheckSumVect(a_inputs.nlevs); if ( a_inputs.first_step > 0 ) // Restart. { #ifndef CH_USE_HDF5 MayDay::Error("inputs.first_step>0 but CH_USE_HDF undefined;" "can't restart without hdf5."); #endif for ( int s=0; s<a_inputs.first_step-1; ++s ) { EBRestart::evolveGeomParams( geometry ); } int eekflag = EBRestart::makeGeometry(*ebIndexSpace, geometry); CH_assert( eekflag==0 ); EBRestart::loadHDF( a_inputs.first_step-1, a_inputs.ghosts, ebIndexSpace, ebvector, grids, geometry, a_inputs ); EBRestart::updateChecksums( ebvector, a_inputs.nlevs, checksums[a_inputs.first_step-1] ); EBRestart::evolveGeomParams( geometry ); } for ( int step=a_inputs.first_step; step<a_inputs.nsteps; ++step ) { checksums[step] = std::vector<EBRestart::CheckSum>(a_inputs.nlevs); EBRestart::stepOnce( step, ebIndexSpace, ebIndexSpace_old, ebvector, ebvector_old, grids, a_inputs.ghosts, geometry, a_inputs, checksums[step-1], checksums[step] ); } // Memory cleanup for (int l=0; l<a_inputs.nlevs; l++) { delete ebvector[l]; delete ebvector_old[l]; } }