Loading...
Searching...
No Matches
POD_application_example_00001.cpp

POD application. This application computes the POD basis using the setup specified through an xml dictionary. To print the usage: ./POD_application_example_00001 –help
.

POD application. This application computes the POD basis using the setup specified through an xml dictionary. To print the usage: ./POD_application_example_00001 –help

/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/
#if BITPIT_ENABLE_MPI
#include <mpi.h>
#endif
#include "pod.hpp"
using namespace bitpit;
//=================================================================================== //
enum class Verbose{
QUIET=0 ,
NORMAL=1 ,
FULL=2
};
//=================================================================================== //
struct InfoBitpodPP{
std::string dictName;
Verbose vconsole;
Verbose vlog;
dictName = "pod.xml";
vlog = Verbose::FULL;
vconsole = Verbose::NORMAL;
}
InfoBitpodPP(const InfoBitpodPP & other){
*this = other;
}
dictName = other.dictName;
vlog = other.vlog;
vconsole = other.vconsole;
return *this;
}
};
//=================================================================================== //
InfoBitpodPP readArguments(int argc, char*argv[] ){
//reading arguments
if(argc <2) {
std::cout<<"Error. Not enough arguments found launching the application"<<std::endl;
std::cout<<"Please run application_example_00001 --help for a brief guide on how to use it"<<std::endl;
exit(1);
}
std::unordered_set<std::string> input;
for(int i=1; i<argc; ++i){
std::string temp = argv[i];
input.insert(bitpit::utils::string::trim(temp));
}
if(input.count("--help")>0){
std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
std::cout<<""<<std::endl;
std::cout<<" Brief application_example_00001 helper, version 1.0"<<std::endl;
std::cout<<""<<std::endl;
std::cout<<""<<std::endl;
std::cout<<" This is the executable command for running POD instructions from XML Control Dictionaries"<<std::endl;
std::cout<<" The command needs mandatorily a XML dictionary to run. It can return execution info on "<<std::endl;
std::cout<<" both console(screen) and external log file. As further debug info, it can plot optional "<<std::endl;
std::cout<<" results of its execution. "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" The full list of options for running the application are: "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" --help : print this helper "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" --dict=<dictionary name> : full path to the target xml dictionary. Mandatory. "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" --vconsole=<quiet/normal/full> : print info on the execution on console(screen). "<<std::endl;
std::cout<<" full is meant for full debug message verbosity, normal for "<<std::endl;
std::cout<<" a medium verbosity, quiet shut off messaging on console. "<<std::endl;
std::cout<<" Default verbosity is medium. "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" --vlog=<quiet/normal/full> : print info on the execution external file bitpit.log. "<<std::endl;
std::cout<<" full is meant for full debug message verbosity, normal for "<<std::endl;
std::cout<<" a medium verbosity, quiet shut off messaging on file. "<<std::endl;
std::cout<<" Default verbosity is full. "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" "<<std::endl;
std::cout<<" For any problem, bug and malfunction please contact bitpit developers. "<<std::endl;
std::cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
exit(1);
}
std::unordered_map<int, std::string> keymap;
keymap[0] = "dict=";
keymap[1] = "vlog=";
keymap[2] = "vconsole=";
std::map<int, std::string> final_map;
for(const auto &val: input){
std::size_t pos = std::string::npos;
int counter=0;
while (pos == std::string::npos && counter <3){
pos = val.find(keymap[counter]);
++counter;
}
if(pos != std::string::npos) final_map[counter-1] =val.substr(pos+keymap[counter-1].size());
}
if(final_map.count(0) < 1) {
std::cout<<"Error. Not valid xml dictionary found launching the application"<<std::endl;
std::cout<<"Please run application_example_00001 --help for a brief guide on how to use it"<<std::endl;
exit(1);
}
InfoBitpodPP result;
result.dictName = final_map[0];
if(final_map.count(1)){
int check = -1 + int(final_map[1]=="quiet") + 2*int(final_map[1]=="normal") + 3*int(final_map[1]=="full");
if(check == -1) check = 2;
result.vlog = static_cast<Verbose>(check);
}
if(final_map.count(2)){
int check = -1 + int(final_map[2]=="quiet") + 2*int(final_map[2]=="normal") + 3*int(final_map[2]=="full");
if(check == -1) check = 1;
result.vconsole = static_cast<Verbose>(check);
}
return result;
}
//=================================================================================== //
void read_podXML(const bitpit::Config::Section & slotXML, POD & podInst){
if(slotXML.hasOption("Directory")){
std::string input = slotXML.get("Directory");
std::string temp = ".";
if(!input.empty())
podInst.setDirectory(input);
else
podInst.setDirectory(temp);
}
if(slotXML.hasOption("Name")){
std::string input = slotXML.get("Name");
std::string temp = "pod";
if(!input.empty())
podInst.setName(input);
else
podInst.setName(temp);
}
if(slotXML.hasOption("MemoryMode")){
std::string input = slotXML.get("MemoryMode");
if(input =="MEMORY_LIGHT")
podInst.setMemoryMode(POD::MemoryMode::MEMORY_NORMAL);
else
podInst.setMemoryMode(POD::MemoryMode::MEMORY_NORMAL);
}
if(slotXML.hasOption("RunMode")){
std::string input = slotXML.get("RunMode");
if(input =="RESTORE")
podInst.setRunMode(POD::RunMode::RESTORE);
else
podInst.setRunMode(POD::RunMode::COMPUTE);
}
if(slotXML.hasOption("WriteMode")){
std::string input = slotXML.get("WriteMode");
if(input =="NONE")
podInst.setWriteMode(POD::WriteMode::NONE);
else if(input =="DEBUG")
podInst.setWriteMode(POD::WriteMode::DEBUG);
else
podInst.setWriteMode(POD::WriteMode::DUMP);
}
if(slotXML.hasOption("ReconstructionMode")){
std::string input = slotXML.get("ReconstructionMode");
if(input =="MINIMIZATION")
podInst.setReconstructionMode(POD::ReconstructionMode::MINIMIZATION);
else
podInst.setReconstructionMode(POD::ReconstructionMode::PROJECTION);
}
if(slotXML.hasOption("ErrorMode")){
std::string input = slotXML.get("ErrorMode");
if(input =="SINGLE")
podInst.setErrorMode(POD::ErrorMode::SINGLE);
else if(input =="COMBINED")
podInst.setErrorMode(POD::ErrorMode::COMBINED);
else
podInst.setErrorMode(POD::ErrorMode::NONE);
}
if(slotXML.hasOption("StaticMesh")){
std::string input = slotXML.get("StaticMesh");
bool value = false;
if(!input.empty()){
std::stringstream ss(input);
ss >> value;
}
podInst.setStaticMesh(value);
}
if(slotXML.hasOption("MeshType")){
std::string input = slotXML.get("MeshType");
if(input =="VOLOCTREE")
podInst.setMeshType(POD::MeshType::VOLOCTREE);
else
podInst.setMeshType(POD::MeshType::UNDEFINED);
}
else{
podInst.setMeshType(POD::MeshType::VOLOCTREE);
}
if(slotXML.hasOption("MeshDirectory")){
std::string meshDir = slotXML.get("MeshDirectory");
meshDir = bitpit::utils::string::trim(meshDir);
if(!meshDir.empty()){
if(slotXML.hasOption("MeshName")){
std::string meshName = slotXML.get("MeshName");
meshName = bitpit::utils::string::trim(meshName);
if(!meshName.empty()){
podInst.setMesh(meshDir, meshName);
}
}
}
}
if(slotXML.hasOption("Mean")){
std::string input = slotXML.get("Mean");
bool value = false;
if(!input.empty()){
std::stringstream ss(input);
ss >> value;
}
podInst.setUseMean(value);
}
if(slotXML.hasOption("Modes")){
std::string input = slotXML.get("Modes");
std::size_t temp = std::numeric_limits<std::size_t>::max();
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
podInst.setModeCount(temp);
}
if(slotXML.hasOption("Energy")){
std::string input = slotXML.get("Energy");
double temp = 100;
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
podInst.setEnergyLevel(temp);
}
if(slotXML.hasOption("ErrorThreshold")){
std::string input = slotXML.get("ErrorThreshold");
double temp = 0;
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
podInst.setErrorThreshold(temp);
}
std::vector<std::string> tempsf;
std::vector<std::array<std::string,3>> tempvf;
if(slotXML.hasOption("targetErrorScalarFields")){
std::string input = slotXML.get("targetErrorScalarFields");
if(!input.empty()){
std::stringstream ss(input);
ss>>tempsf;
}
}
if(slotXML.hasOption("targetErrorVectorFields")){
std::string input = slotXML.get("targetErrorVectorFields");
if(!input.empty()){
std::stringstream ss(input);
ss>>tempvf;
}
}
podInst.setTargetErrorFields(tempsf,tempvf);
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<< "Finished reading XML dictionary"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
{
std::vector<std::string> emode(2, "restore");
emode[1] = "compute";
std::vector<std::string> mmode(2, "normal");
emode[1] = "light";
std::vector<std::string> wmode(3, "dump");
wmode[1] = "debug";
wmode[2] = "none";
std::vector<std::string> rmode(2, "projection");
wmode[1] = "minimization";
std::vector<std::string> errmode(3, "combined");
wmode[1] = "single";
wmode[2] = "none";
log::cout()<< "Resume of arguments in "<< podInst.getName() << " : " << std::endl;
log::cout()<< "execution mode: "<<emode[static_cast<int>(podInst.getRunMode())]<<std::endl;
log::cout()<< "memory mode: "<<mmode[static_cast<int>(podInst.getMemoryMode())]<<std::endl;
log::cout()<< "write mode: "<<wmode[static_cast<int>(podInst.getWriteMode())]<<std::endl;
log::cout()<< "reconstruction mode: "<<rmode[static_cast<int>(podInst.getReconstructionMode())]<<std::endl;
log::cout()<< "error mode: "<<errmode[static_cast<int>(podInst.getErrorMode())]<<std::endl;
log::cout()<< "number of modes: "<<podInst.getModeCount()<<std::endl;
log::cout()<< "energy level: "<<podInst.getEnergyLevel()<<std::endl;
log::cout()<< "error threshold: "<<podInst.getErrorThreshold()<<std::endl;
log::cout()<< " "<<std::endl;
log::cout()<< " "<<std::endl;
}
}
//=================================================================================== //
std::vector<bool> read_jobControlsXML(const bitpit::Config::Section & slotXML, POD & podInst){
BITPIT_UNUSED(podInst);
std::vector<bool> controls;
controls.resize(3,false);
if(slotXML.hasOption("doPODbasis")){
std::string input = slotXML.get("doPODbasis");
bool temp=true;
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
controls[0]=temp;
}
else
controls[0]=true;
if(slotXML.hasOption("doLeave1out")){
std::string input = slotXML.get("doLeave1out");
bool temp=false;
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
controls[1]=temp;
}
if(slotXML.hasOption("doBoundingBox")){
std::string input = slotXML.get("doBoundingBox");
bool temp=false;
if(!input.empty()){
std::stringstream ss(input);
ss>>temp;
}
controls[2]=temp;
}
return controls;
}
//=================================================================================== //
std::vector<bool> read_Dictionary(POD & podInst) {
std::vector<bool> exeFlags;
exeFlags.resize(3,false);
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<< "Currently reading XML dictionary"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
if(config::root.hasSection("POD")){
bitpit::Config::Section & podXML = config::root.getSection("POD");
read_podXML(podXML, podInst);
log::cout() << "...Instantiated pod: "<< podInst.getName() << std::endl;
}else{
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<<"No POD section available in the XML dictionary"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
}
if(config::root.hasSection("Database")){
bitpit::Config::Section & blockXML = config::root.getSection("Database");
for(auto & sect : blockXML.getSections()){
std::string name = ".";
std::string path = ".";
std::size_t ns, first = 0;
std::size_t stride = 1;
name = sect.second->get("Case");
path = sect.second->get("Directory");
std::string nss = sect.second->get("NSnapshots");
std::stringstream ss(nss);
ss >> ns;
if(sect.second->hasOption("Stride")){
std::string sstride = sect.second->get("Stride");
std::stringstream ssstride(sstride);
ssstride >> stride;
}
if(sect.second->hasOption("FirstSnapshot")){
std::string sfirst = sect.second->get("FirstSnapshot");
std::stringstream ssfirst(sfirst);
ssfirst >> first;
}
for (std::size_t i = first; i < ns; i+=stride){
snap.directory = path;
snap.name = name + "." + std::to_string(i);
podInst.addSnapshot(snap);
}
}
}else{
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<<"No Database section available in the XML dictionary"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
}
if(config::root.hasSection("Reconstruction")){
bitpit::Config::Section & blockXML = config::root.getSection("Reconstruction");
for(auto & sect : blockXML.getSections()){
std::string name = ".";
std::string path = ".";
std::size_t ns, first = 0;
std::size_t stride = 1;
name = sect.second->get("Case");
path = sect.second->get("Directory");
std::string nss = sect.second->get("NSnapshots");
std::stringstream ss(nss);
ss >> ns;
if(sect.second->hasOption("Stride")){
std::string sstride = sect.second->get("Stride");
std::stringstream ssstride(sstride);
ssstride >> stride;
}
if(sect.second->hasOption("FirstSnapshot")){
std::string sfirst = sect.second->get("FirstSnapshot");
std::stringstream ssfirst(sfirst);
ssfirst >> first;
}
for (std::size_t i = first; i < ns; i+=stride){
snap.directory = path;
snap.name = name + "." + std::to_string(i);
}
}
}
if(config::root.hasSection("Leave1out")){
bitpit::Config::Section & blockXML = config::root.getSection("Leave1out");
for(auto & sect : blockXML.getSections()){
std::string name = ".";
std::string path = ".";
std::size_t ns, first = 0;
std::size_t stride = 1;
name = sect.second->get("Case");
path = sect.second->get("Directory");
std::string nss = sect.second->get("NSnapshots");
std::stringstream ss(nss);
ss >> ns;
if(sect.second->hasOption("Stride")){
std::string sstride = sect.second->get("Stride");
std::stringstream ssstride(sstride);
ssstride >> stride;
}
if(sect.second->hasOption("FirstSnapshot")){
std::string sfirst = sect.second->get("FirstSnapshot");
std::stringstream ssfirst(sfirst);
ssfirst >> first;
}
for (std::size_t i = first; i < ns; i+=stride){
snap.directory = path;
snap.name = name + "." + std::to_string(i);
podInst.removeLeave1outSnapshot(snap);
}
}
}
if(config::root.hasSection("JobControls")){
bitpit::Config::Section & jobXML = config::root.getSection("JobControls");
exeFlags=read_jobControlsXML(jobXML, podInst);
}else
exeFlags[0]=true;
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<< "Finished reading XML dictionary"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
return exeFlags;
}
// =================================================================================== //
void podcore(const InfoBitpodPP & info) {
std::string log = "bitpit";
switch(int(info.vconsole)){
case 1 :
bitpit::log::setConsoleVerbosity(log::cout(), bitpit::log::LEVEL_INFO);
break;
case 2 :
bitpit::log::setConsoleVerbosity(log::cout(), bitpit::log::LEVEL_DEBUG);
break;
case 0 :
bitpit::log::setConsoleVerbosity(log::cout(), bitpit::log::LEVEL_QUIET);
break;
default:
break;
}
switch(int(info.vlog)){
case 1 :
bitpit::log::setFileVerbosity(log::cout(), bitpit::log::LEVEL_INFO);
break;
case 2 :
bitpit::log::setFileVerbosity(log::cout(), bitpit::log::LEVEL_DEBUG);
break;
case 0 :
bitpit::log::setFileVerbosity(log::cout(), bitpit::log::LEVEL_QUIET);
break;
default:
break;
}
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
{
std::vector<std::string> verb(3, "quiet");
verb[1] = "normal";
verb[2] = "full";
log::cout()<< "Resume of arguments:"<<std::endl;
log::cout()<< "dictionary: "<<info.dictName<<std::endl;
log::cout()<< "console verbosity: "<<verb[static_cast<int>(info.vconsole)]<<std::endl;
log::cout()<< "log file verbosity: "<<verb[static_cast<int>(info.vlog)]<<std::endl;
log::cout()<< " "<<std::endl;
log::cout()<< " "<<std::endl;
}
bitpit::config::reset("podXML", 1);
POD podInst;
std::vector<bool> exes;
exes=read_Dictionary(podInst);
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<<"Execution of pod... ";
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
if (exes[1])
podInst.leave1out();
if (exes[2])
if (exes[0])
podInst.run();
log::cout().setDefaultSeverity(bitpit::log::LEVEL_INFO);
log::cout()<<"...execution of pod done"<<std::endl;
log::cout().setDefaultSeverity(bitpit::log::DEBUG);
}
//=================================================================================== //
int main( int argc, char *argv[] ) {
#if BITPIT_ENABLE_MPI
MPI_Init(&argc, &argv);
{
#endif
try{
InfoBitpodPP info = readArguments(argc, argv);
podcore(info);
}
catch(std::exception & e){
std::cout<<"application_example_00001 exited with an error of type : "<<e.what()<<std::endl;
return 1;
}
#if BITPIT_ENABLE_MPI
}
MPI_Finalize();
#endif
return 0;
}
Configuration storage.
const std::string & get(const std::string &key) const
Section & getSection(const std::string &key)
bool hasOption(const std::string &key) const
void setDefaultSeverity(log::Level severity)
Definition logger.cpp:537
The POD (Proper Orthogonal Decomposition) class provides an interface for defining POD object.
Definition pod.hpp:41
void setErrorThreshold(double threshold)
Definition pod.cpp:346
std::size_t getModeCount()
Definition pod.cpp:315
void run()
Definition pod.cpp:836
void setRunMode(RunMode mode)
Definition pod.cpp:542
void setTargetErrorFields(std::vector< std::string > &namesf, std::vector< std::array< std::string, 3 > > &namevf)
Definition pod.cpp:367
void setMeshType(MeshType type)
Definition pod.cpp:390
void setStaticMesh(bool flag)
Definition pod.cpp:483
void setMemoryMode(MemoryMode mode)
Definition pod.cpp:505
double getErrorThreshold()
Definition pod.cpp:356
void setDirectory(const std::string &directory)
Definition pod.cpp:180
double getEnergyLevel()
Definition pod.cpp:336
const std::string & getName()
Definition pod.cpp:170
RunMode getRunMode()
Definition pod.cpp:566
void leave1out()
Definition pod.cpp:1121
ReconstructionMode getReconstructionMode()
Definition pod.cpp:623
void evalErrorBoundingBox()
Definition pod.cpp:1503
MemoryMode getMemoryMode()
Definition pod.cpp:530
void setUseMean(bool flag)
Definition pod.cpp:493
void addReconstructionSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:282
void removeLeave1outSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:243
void setWriteMode(WriteMode mode)
Definition pod.cpp:576
ErrorMode getErrorMode()
Definition pod.cpp:648
void setReconstructionMode(ReconstructionMode mode)
Definition pod.cpp:598
void setEnergyLevel(double energy)
Definition pod.cpp:326
void setModeCount(std::size_t nmodes)
Definition pod.cpp:304
void setName(const std::string &name)
Definition pod.cpp:160
WriteMode getWriteMode()
Definition pod.cpp:587
void setErrorMode(ErrorMode mode)
Definition pod.cpp:635
void setMesh(const std::string &directory, const std::string &name)
Definition pod.cpp:443
void addSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:208
#define BITPIT_UNUSED(variable)
Definition compiler.hpp:63
std::string & trim(std::string &s)
void reset(const std::string &rootName)
void read(const std::string &filename, bool append)
unsigned int check(std::ifstream &, std::vector< std::vector< int > > &)
Definition DGF.cpp:1169
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
Logger & setFileVerbosity(Logger &logger, const log::Level &threshold)
Definition logger.cpp:2105
Logger & info(log::Visibility defaultVisibility)
Definition logger.cpp:1847
Logger & setConsoleVerbosity(Logger &logger, const log::Level &threshold)
Definition logger.cpp:2078
database of essential information absorbed from the custom arguments
InfoBitpodPP & operator=(const InfoBitpodPP &other)
The SnapFile structure is used to store the file names inside POD classes.
--- layout: doxygen_footer ---