/** * @file comm_serial.cpp * @author Julian Iseringhausen * @date Mon Apr 18 12:28:12 2011 * * @brief VMG::CommSerial * */ #ifdef HAVE_CONFIG_H #include #endif #ifdef HAVE_BOOST_FILESYSTEM #include namespace fs = boost::filesystem; #endif #include #include #include #include #include "base/discretization.hpp" #include "base/stencil.hpp" #include "base/vector.hpp" #include "comm/comm_serial.hpp" #include "comm/comm.hpp" #include "grid/multigrid.hpp" #include "grid/tempgrid.hpp" #include "mg.hpp" using namespace VMG; inline vmg_float pow_3(vmg_float val) { return val*val*val; } void CommSerial::Init() { output_count = 0; error_norm_count = 0; residual_count = 0; /* Create directory to output defects */ #ifdef HAVE_BOOST_FILESYSTEM char buffer[129]; time_t rawtime; struct tm *timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); strftime(buffer, 128, "output/%Y_%m_%d_%H_%M_%S/", timeinfo); defects_path_str = buffer; #else defects_path_str = ""; #endif } void CommSerial::CreateOutputDirectory() { #ifdef HAVE_BOOST_FILESYSTEM if (!fs::exists("output")) fs::create_directory("output"); if (!fs::exists(defects_path_str.c_str())) fs::create_directory(defects_path_str.c_str()); #endif } Grid& CommSerial::GetFinerGrid(Multigrid& multigrid) { return multigrid(multigrid.Level()+1); } Grid& CommSerial::GetCoarserGrid(Multigrid& multigrid) { return multigrid(multigrid.Level()-1); } vmg_float CommSerial::ComputeResidualNorm(Multigrid& sol, Multigrid& rhs) { #ifdef DEBUG sol().IsCompatible(rhs()); sol().IsConsistent(); rhs().IsConsistent(); #endif Stencil::iterator iter; Index i; vmg_float val; vmg_float norm = 0.0; const vmg_float prefactor = MG::GetDiscretization()->OperatorPrefactor(sol()); const Stencil& A = MG::GetDiscretization()->GetStencil(); this->CommToGhosts(sol()); if (sol().Global().BoundaryType() == LocallyRefined) MG::GetDiscretization()->SetInnerBoundary(sol(), rhs(), sol(sol.Level()-1)); for (i.X()=rhs().Local().Begin().X(); i.X()val * sol().GetVal(i+iter); norm += val*val; } norm = sqrt( pow_3(sol().MeshWidth()) * norm ); #ifdef DEBUG std::ofstream out; char path_str[129]; CreateOutputDirectory(); sprintf(path_str, "%sresidual.txt", defects_path_str.c_str()); out.open(path_str, std::ios::app); assert(out.good()); out << residual_count++ << " " << norm << std::endl; out.close(); #endif return norm; } void CommSerial::CommFromGhosts(Grid& mesh) { if (this->BoundaryConditions() == Periodic) { // Copy values in x-direction for (int i=mesh.Local().HaloBegin1().X(); i= mesh.Local().Begin().X() && i+mesh.Local().Size().X() < mesh.Local().End().X()); mesh(i+mesh.Local().Size().X(),j,k) += mesh.GetVal(i,j,k); } for (int i=mesh.Local().HaloBegin2().X(); i= mesh.Local().Begin().X() && i-mesh.Local().Size().X() < mesh.Local().End().X()); mesh(i-mesh.Local().Size().X(),j,k) += mesh.GetVal(i,j,k); } // Copy values in y-direction for (int i=mesh.Local().Begin().X(); iBoundaryConditions() == Periodic) { // Copy values in z-direction for (i=mesh.Local().Begin().X(); iSetProperties(sol); temp->ImportFromResidual(sol, rhs); #ifdef DEBUG temp->IsConsistent(); #endif OpenFileAndPrintHeader(out, *temp, information, true); assert(out.good()); if (!out.good()) return; for (int k=temp->Local().Begin().Z(); kLocal().End().Z(); k++) for (int j=temp->Local().Begin().Y(); jLocal().End().Y(); j++) for (int i=temp->Local().Begin().X(); iLocal().End().X(); i++) out << std::scientific << temp->GetVal(i,j,k) << std::endl; out.close(); } void CommSerial::PrintGrid(const Grid& mesh, const char* information) { std::ofstream out; OpenFileAndPrintHeader(out, mesh, information, false); for (int k=0; k" << std::endl << " " << std::endl << " " << std::endl << " "; if (grid.Global().BoundaryType() == LocallyRefined) { for (int i=0; i" << std::endl << " " << std::endl << " " << std::endl << " " << std::endl << " "; if (grid.Global().BoundaryType() == LocallyRefined) { for (int i=0; i" << std::endl << " " << std::endl << " "; if (grid.Global().BoundaryType() == LocallyRefined) { for (int i=1; i<=grid.Local().Size().Product(); i++) out << i << " "; }else { for (int i=1; i<=grid.Local().SizeTotal().Product(); i++) out << i << " "; } out << std::endl << " " << std::endl << " " << std::endl << " " << std::endl << " " << std::endl << " "; if (grid.Global().BoundaryType() == LocallyRefined) { for (int i=0; i" << std::endl << " " << std::endl << " "; for (int i=1; i<=numLines; i++) out << 2*i << " "; out << std::endl << " " << std::endl << " " << std::endl << " " << std::endl; } void CommSerial::DebugPrintGridStructure(Multigrid& grid) { std::ofstream out; char path_str[129]; CreateOutputDirectory(); sprintf(path_str, "%sgrid.vtp", defects_path_str.c_str()); out.open(path_str, std::ios::trunc); if (!out.good()) { printf("Multigrid: File %s not accessible.\n", path_str); return; } out << "" << std::endl << "" << std::endl << " " << std::endl; for (int i=grid.MinLevel(); i<=grid.MaxLevel(); i++) PrintGridStructureLevel(grid(i), out); out << " " << std::endl << "" << std::endl; out.close(); }