From fc2824c1d76246f28b1f3a055587946ed4c56559 Mon Sep 17 00:00:00 2001 From: Klaus <klaus.boehnlein@tu-dresden.de> Date: Mon, 9 Aug 2021 13:00:21 +0200 Subject: [PATCH] Added Output as Table --- inputs/cellsolver.parset | 3 +- src/dune-microstructure.cc | 147 ++++++++++++++++++++++++++++++++----- 2 files changed, 131 insertions(+), 19 deletions(-) diff --git a/inputs/cellsolver.parset b/inputs/cellsolver.parset index 16a7832c..cc567f41 100644 --- a/inputs/cellsolver.parset +++ b/inputs/cellsolver.parset @@ -14,7 +14,8 @@ cellDomain = 1 # Grid parameters ############################################# -levels = 2 +#levels = 2 +numLevels = {1,3} # computes all levels from first to second entry # #Elements_Cell = 20 20 20 # number elements in each direction (y1 y2 x3) diff --git a/src/dune-microstructure.cc b/src/dune-microstructure.cc index dc84fa5f..b6710dcd 100644 --- a/src/dune-microstructure.cc +++ b/src/dune-microstructure.cc @@ -54,10 +54,44 @@ #include <any> #include <variant> #include <string> +#include <iomanip> using namespace Dune; +/*! Center-aligns string within a field of width w. Pads with blank spaces + to enforce alignment. */ +std::string center(const std::string s, const int w) { + std::stringstream ss, spaces; + int padding = w - s.size(); // count excess room to pad + for(int i=0; i<padding/2; ++i) + spaces << " "; + ss << spaces.str() << s << spaces.str(); // format with padding + if(padding>0 && padding%2!=0) // if odd #, add 1 space + ss << " "; + return ss.str(); +} + +/* Convert double to string with specified number of places after the decimal + and left padding. */ +template<class type> +std::string prd(const type x, const int decDigits, const int width) { + std::stringstream ss; + ss << std::fixed << std::right; + ss.fill(' '); // fill space around displayed # + ss.width(width); // set width around displayed # + ss.precision(decDigits); // set # places after decimal + ss << x; + return ss.str(); +} + + + + + + + + template<class Basis> @@ -2208,7 +2242,7 @@ int main(int argc, char *argv[]) ///// LEVEL - APPROACH // TODO // int numLevels = parameterSet.get<int>("numLevels", 1); - std::array<int,2> numLevels = parameterSet.get<std::array<int,2>>("numLevels", {1,2}); // Alternativ : Bereich z.B. [2,4] + std::array<int,2> numLevels = parameterSet.get<std::array<int,2>>("numLevels", {1,3}); // Alternativ : Bereich z.B. [2,4] int levelCounter = 0; @@ -2275,15 +2309,6 @@ int main(int argc, char *argv[]) - // NICE TABLE - char buf[256]; - char pattern[] = "%10s %10s %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"; - sprintf(buf, pattern, "Bob", "Doe", 10.96, 7.61, 14.39, 2.11, 47.30, 14.21, 44.58, 5.00, 60.23); - std::cout << buf << std::endl; - sprintf(buf, pattern, "Helen", "City", 10.44, 7.78, 16.27, 1.99, 48.92, 13.93, 53.79, 5.00, 70.97); - std::cout << buf << std::endl; - sprintf(buf, pattern, "Joe", "Green", 10.90, 7.33, 14.49, 2.05, 47.91, 14.15, 44.45, 4.70, 73.98); - std::cout << buf << std::endl; @@ -3016,22 +3041,108 @@ int main(int argc, char *argv[]) ///////////////////////////////////////// // Print Storage ///////////////////////////////////////// - std::cout << "PRINT STORAGE: " << std::endl; +// std::cout << "PRINT STORAGE: " << std::endl; +// int StorageCount = 0; +// for(auto& v: Storage) { +// +// if(StorageCount % 6 == 0 ) +// std::cout<< "Level: "; +// +// +// std::visit([](auto&& arg){std::cout << arg;}, v); +// std::cout << "-----" << std::endl; +// StorageCount++; +// } + + + ///////////////////////////////////////// + // Output Table + ///////////////////////////////////////// + + // NICE TABLE +// char buf[256]; +// char pattern[] = "%10s %10s %7s %10s %10s %10s "; +// char pattern2[] = "%10s %10f %7.0f %10f %10f %10f "; +// sprintf(buf, pattern, "Level", "L2SymError", "Order", "L2SymNorm", "L2SNorm_ana", "L2Norm"); +// std::cout << buf << std::endl; + +// std::cout << std::setw(10) << " " << std::setw(10) << "Level " << "L2SymError " << std::setw(10) << "Order " << std::setw(10) << "L2SymNorm " << std::setw(10) << "L2SNorm_ana " << std::setw(10) << "L2Norm " << std::endl; + +// std::cout << std::left << std::setw(10) << std::setfill(' ') << "Level" << "L2SymError " << std::setw(10) << "Order " << std::setw(10) << "L2SymNorm " << std::setw(10) << "L2SNorm_ana " << std::setw(10) << "L2Norm " << std::endl; + + + //WORKS: +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "Level "; +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "L2SymError "; +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "Order "; +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "L2SymNorm "; +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "L2SNorm_ana "; +// std::cout << std::left << std::setw(12) << std::setfill(' ') << "L2Norm "; +// std::cout << std::endl; +// std::cout << "-----------------------------------------------------------------------------------------"<< std::endl; +// + // WORKS + int tableWidth = 12; + std::cout << center("Levels",tableWidth) << " | " + << center("L2SymError",tableWidth) << " | " + << center("Order",tableWidth) << " | " + << center("L2SymNorm",tableWidth) << " | " + << center("L2SNorm_ana",tableWidth) << " | " + << center("L2Norm",tableWidth) << " | " << "\n"; + std::cout << std::string(tableWidth*6 + 3*6, '-') << "\n"; + + +// std::vector<double> tmpVec; // oder std::variant + std::vector<std::variant<const size_t , double>> tmpVec; int StorageCount = 0; + for(auto& v: Storage) { - if(StorageCount % 6 == 0 ) - std::cout<< "Level: "; +// if(StorageCount % 6 == 0 ) +// std::cout<< "Level: "; - std::visit([](auto&& arg){std::cout << arg;}, v); - std::cout << "-----" << std::endl; +// std::visit([](auto&& arg){std::cout << std::left << std::setw(12) << std::setfill(' ') << arg;}, v); + +// std::visit([tableWidth](auto&& arg){std::cout << center(prd(arg,5,12),tableWidth) << " | ";}, v); + std::visit([tableWidth](auto&& arg){std::cout << center(prd(arg,5,1),tableWidth) << " | ";}, v); + + + + +// std::visit([tmpVec](auto&& arg){tmpVec.push_back(arg);}, v); + StorageCount++; + if(StorageCount % 6 == 0 ) + { +// sprintf(buf, pattern2, "2x2x2", std::get<1>(tmpVec[1]), std::get<1>(tmpVec[2]), std::get<1>(tmpVec[2]), std::get<1>(tmpVec[2]), std::get<1>(tmpVec[5])); + + std::cout << std::endl; + } + } + + + + +// for(double x=1.5; x<200; x +=x*2) { +// std::cout << prd(x,1,10) << " | " +// << prd(x*x,2,10) << " | " +// << prd(x*x/8.0,4,10) << "\n"; +// } + + + + // NICE TABLE +// char buf[256]; +// char pattern[] = "%10s %10s %7s %10s %10s %10s "; +// char pattern2[] = "%10s %10f %7.0f %10f %10f %10f "; +// sprintf(buf, pattern, "Level", "L2SymError", "Order", "L2SymNorm", "L2SNorm_ana", "L2Norm"); +// std::cout << buf << std::endl; +// sprintf(buf, pattern2, "2x2x2", 10.44, 7.78, 16.27, 1.99, 48.92); +// std::cout << buf << std::endl; + - ///////////////////////////////////////// - // Output Table - ///////////////////////////////////////// } -- GitLab