Commit 5569e972 authored by Benoît LÉ's avatar Benoît LÉ

[xExport] Added xPostProcessingManager class

Added new class xPostProcessingManager, which was previously in the XTLS/TLSElementbased
appli (under the name PostProcessing).

The main goal of this class is to encapsulate three xexport concepts:
- The xexport::Export function
- The xexport::xExportManager class
- The xexport::xExportSensors class

through the use of two main functions:

- exportOnSpace : export a field/mesh/a level set with the Export function,
using the frequency defined by the xExportManager. Note that if the field is a
vector or tensor field, a given component can be exported by adding an extension to the
name of the field in the parsed file. For instance, if a vector field named "displacement"
is exported in the code, it can be exported by adding the name "displacement" in the
export_manager field of the parsed file, but the first (second/third) component can also be exported
b adding "displacement_1" ("displacement_2", "displacement_3"). Same for tensor fields (_11, _13, _23, etc)

- exportOnTime : write a scalar quantity in a text file, using the xExportSensors class

The saveMesh, saveField and saveDomain function from the former class PostProcessing were kept, and could
be used in the future to implement restart.
parent 9ca299e0
/*
This file is a part of eXlibris C++ Library
under the GNU General Public License:
See the LICENSE.md files for terms and
conditions.
*/
#include "xPostProcessingManager.h"
#include "mEntity.h"
#include "xFiniteElement.h"
using namespace std;
using namespace xfem;
using namespace xexport;
xPostProcessingManager::xPostProcessingManager(const xParseData& parse_data_, xexport::xExport& pexport_, xEntityFilter filter)
: parse_data(parse_data_), export_manager(GetNbDigits(parse_data.getInt("nb_step_max"))), filter(filter), pexport(pexport_)
{
export_manager.init(parse_data, "export_manager");
}
void xPostProcessingManager::init(xMesh& mesh)
{
sensor_manager.init(parse_data, &mesh, 2, "export_sensors_label", "export_sensors_point");
}
void xPostProcessingManager::reinit(xMesh& mesh) { sensor_manager.reinit(&mesh); }
bool xPostProcessingManager::toExport(const std::string export_name) { return sensor_manager.toExport(export_name); }
bool xPostProcessingManager::toExport(const std::string export_name, const int step, const std::string& extension_name)
{
return export_manager.toExport(export_name, step, extension_name);
}
void xPostProcessingManager::exportOnSpace(const std::string export_name, const int step, const xLevelSet& ls)
{
if (export_manager.toExport(export_name, step, ""))
{
pexport.openFile(export_manager.getFileName());
Export(ls, pexport, export_name);
pexport.closeFile();
}
}
void xPostProcessingManager::exportOnSpace(const std::string export_name, const int step, const xMesh& mesh)
{
if (export_manager.toExport(export_name, step, ""))
{
Export(mesh, pexport, export_manager.getFileName());
}
}
void xPostProcessingManager::exportOnTime(const std::string name, const double time, const double val)
{
sensor_manager.measure(val, time, name);
}
void xPostProcessingManager::exportOnTime(const std::string name, const double time, const xEval<double>& eval)
{
sensor_manager.measureAll(eval, time, name);
}
void xPostProcessingManager::exportOnTime(const std::string name, const double time, const xEval<xtensor::xVector<>>& eval)
{
sensor_manager.measureAll(eval, time, name);
}
void xPostProcessingManager::exportOnTime(const std::string name, const double time, const xEval<xtensor::xTensor2<>>& eval)
{
sensor_manager.measureAll(eval, time, name);
}
void xPostProcessingManager::saveMesh(std::string export_name, int step, xMesh& mesh)
{
if (step > 0 && export_manager.toExport("save", step, ""))
{
Export(mesh, pexport, export_name + "_save");
}
}
void xPostProcessingManager::saveField(std::string export_name, int step, const xField<>& field, xRegion reg)
{
if (step > 0 && export_manager.toExport("save", step, ""))
{
xValueManagerDist<double>* double_manager = field.getValueManager();
std::ofstream oss(export_name + "_save.bin", ios::binary);
if (oss.is_open())
{
const int info_size = 2 * reg.dim() * sizeof(int);
int nb = reg.size(reg.dim());
oss.write((char*)&nb, sizeof(int));
for (auto e : reg)
{
xFiniteElement FEM;
FEM.setKeys(e, field.begin(), field.end());
const int size = FEM.sizeKey();
std::vector<xValue<double>*> vals;
vals.reserve(size);
double_manager->getValPtr(FEM.beginKey(), FEM.endKey(), vals);
assert(size < 7);
int info[6];
info[0] = size;
for (int i = 0; i < e->size(0); ++i)
{
auto n = e->get(0, i);
info[i + 1] = n->getId();
}
double data[16];
for (int i = 0; i < size; ++i)
{
data[i] = vals[i]->getVal();
}
oss.write((char*)info, info_size);
oss.write((char*)data, size * sizeof(double));
}
oss.close();
}
}
}
/*
This file is a part of eXlibris C++ Library
under the GNU General Public License:
See the LICENSE.md files for terms and
conditions.
*/
#ifndef _xPOSTPROCESSINGMANAGER_H_
#define _xPOSTPROCESSINGMANAGER_H_
#include "xExportAlgorithm.h"
#include "xExportGmsh.h"
#include "xExportManager.h"
#include "xField.h"
#include "xSensors.h"
inline int GetNbDigits(int nb)
{
if (nb < 0)
{
return 1;
}
int digits = 0;
while (nb)
{
nb /= 10;
++digits;
}
return digits;
}
namespace xexport
{
class xPostProcessingManager
{
public:
xPostProcessingManager(const xParseData &, xexport::xExport &, xfem::xEntityFilter = xfem::xAcceptAll());
// init and reinit xfem::xGeomElem used by xexport::xSensors to measure.
void init(xfem::xMesh &);
void reinit(xfem::xMesh &);
void createCurvesPlotFiles(std::string);
bool toExport(const std::string export_name);
bool toExport(const std::string export_name, const int step, const std::string &extension_name = "");
void exportOnSpace(const std::string export_name, const int step, const xfem::xLevelSet &ls);
template <typename ITER>
void exportOnSpace(const std::string export_name, const int step, const xfem::xEval<double> &eval,
const xfem::xIntegrationRule &integration_rule, ITER begin, ITER end,
xfem::xEntityToEntity integ2appro = xtool::xIdentity<AOMD::mEntity *>())
{
if (export_manager.toExport(export_name, step, ""))
{
exportOnSpace(export_name, eval, integration_rule, begin, end, integ2appro);
}
}
template <typename ITER>
void exportOnSpace(const std::string export_name, const int step, const xfem::xEval<xtensor::xVector<>> &eval,
const xfem::xIntegrationRule &integration_rule, ITER begin, ITER end,
xfem::xEntityToEntity integ2appro = xtool::xIdentity<AOMD::mEntity *>())
{
if (export_manager.toExport(export_name, step, ""))
{
exportOnSpace(export_name, eval, integration_rule, begin, end, integ2appro);
}
for (int i = 0; i < 3; ++i)
{
if (export_manager.toExport(export_name + "_" + std::to_string(i + 1), step, ""))
{
xtensor::xExtractCompVector<> extract(i);
xfem::xEvalUnary<xtensor::xExtractCompVector<>> evalex(extract, eval);
exportOnSpace(export_name, evalex, integration_rule, begin, end, integ2appro);
}
}
}
template <typename ITER>
void exportOnSpace(const std::string export_name, const int step, const xfem::xEval<xtensor::xTensor2<>> &eval,
const xfem::xIntegrationRule &integration_rule, ITER begin, ITER end,
xfem::xEntityToEntity integ2appro = xtool::xIdentity<AOMD::mEntity *>())
{
if (export_manager.toExport(export_name, step, ""))
{
exportOnSpace(export_name, eval, integration_rule, begin, end, integ2appro);
}
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (export_manager.toExport(export_name + "_" + std::to_string(i + 1) + std::to_string(j + 1), step, ""))
{
xtensor::xExtractCompTensor<> extract(i, j);
xfem::xEvalUnary<xtensor::xExtractCompTensor<>> evalex(extract, eval);
exportOnSpace(export_name, evalex, integration_rule, begin, end, integ2appro);
}
}
}
if (export_manager.toExport(export_name + "_tr", step, ""))
{
xfem::xEvalUnary<xtensor::xTrace<>> evaltr(eval);
exportOnSpace(export_name, evaltr, integration_rule, begin, end, integ2appro);
}
if (export_manager.toExport(export_name + "_dev", step, ""))
{
xfem::xEvalUnary<xtensor::xDeviatoric<>> evaldev(eval);
exportOnSpace(export_name, evaldev, integration_rule, begin, end, integ2appro);
}
}
template <typename ITER>
void exportOnTime(const std::string name, const double time, const xfem::xEval<double> &eval,
const xfem::xIntegrationRule &integration_rule, ITER begin, ITER end, xfem::xEntityToEntity entity_to_entity)
{
if (sensor_manager.toExport(name))
{
double global = 0.;
xfem::xIntegrateEvalCommand<xfem::xEval<double>> command(eval, global);
xfem::ApplyCommandOnIntegrationRule(command, integration_rule, begin, end, entity_to_entity);
sensor_manager.measure(global, time, name);
}
}
void exportOnSpace(const std::string export_name, const int step, const xfem::xMesh &mesh);
void exportOnTime(const std::string name, const double time, const double val);
void exportOnTime(const std::string name, const double time, const xfem::xEval<double> &eval);
void exportOnTime(const std::string name, const double time, const xfem::xEval<xtensor::xVector<>> &eval);
void exportOnTime(const std::string name, const double time, const xfem::xEval<xtensor::xTensor2<>> &eval);
void saveMesh(std::string export_name, int step, xfem::xMesh &mesh);
void saveField(std::string export_name, int step, const xfem::xField<> &field, xfem::xRegion reg);
template <typename ITER>
void saveDomain(std::string export_name, int step, ITER it, ITER end)
{
if (step > 0 && export_manager.toExport("save", step, ""))
{
std::ofstream oss(export_name + "_save.bin", std::ios::binary);
if (oss.is_open())
{
int nb = std::distance(it, end);
oss.write((char *)&nb, sizeof(int));
for (; it != end; ++it)
{
auto e = *it;
int data[16];
int size = e->size(0);
for (int i = 0; i < size; ++i)
{
data[i] = e->get(0, i)->getId();
}
oss.write((char *)&size, sizeof(int));
oss.write((char *)data, size * sizeof(int));
}
oss.close();
}
}
}
protected:
template <typename EVAL, typename ITER>
void exportOnSpace(const std::string &export_name, const EVAL &eval, const xfem::xIntegrationRule &integration_rule,
ITER begin, ITER end, xfem::xEntityToEntity integ2appro = xtool::xIdentity<AOMD::mEntity *>())
{
pexport.openFile(export_manager.getFileName());
xfem::xFilteredRegion<ITER, xfem::xEntityFilter> fr(begin, end, filter);
Export(eval, pexport, export_name, integration_rule, fr.begin(), fr.end(), integ2appro);
pexport.closeFile();
}
const xParseData &parse_data;
xexport::xExportManager export_manager;
xexport::xExportSensors sensor_manager;
xexport::xExport &pexport;
xfem::xEntityFilter filter;
};
} // namespace xexport
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment