Export.cc 6.12 KB
Newer Older
1 2 3 4 5
/* 
    This file is a part of eXlibris C++ Library
    under the GNU General Public License:
    See the LICENSE.md files for terms and 
    conditions.
6
*/
7

8
#include "Export.h"
9
#include "Observer.h"
Kevin Moreau's avatar
Kevin Moreau committed
10
#include "mEntity.h"
11
#include <string>
Kevin Moreau's avatar
Kevin Moreau committed
12
#include <fstream>
13

14
using namespace std;
15
using namespace xfem;
16

17 18
stack<pair<string, chrono::time_point<std::chrono::system_clock>>> Display::str_container
= stack<pair<string, chrono::time_point<std::chrono::system_clock>>>();
19

20 21 22
string Display::prefix = string("\033[0;36m");
string Display::suffix = string("\033[0;37m");

23
void Display::txtColorOff() {
24 25 26 27
  prefix = "";
  suffix = "";
}

28
void Display::setPrefixAndSuffix(string prefix_, string suffix_) {
29 30 31 32 33
  prefix = prefix_;
  suffix = suffix_;
}


34
void Display::init() {                // To be suppressed soon
35 36 37 38 39 40 41
  int i;
  for (i=0; i<=10; i=i+1) {
    std::cout << " "<< endl;
  }
}

void Display::emph(string str) {
42
  cout << prefix << str << suffix << endl;
43
}
44

45
void Display::dispIn(string& str, unsigned int n) {
46
  cout << string(2*n, ' ') << prefix << "(" << n << ") Enter " << str << suffix << endl; // see http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
47
}
48

49
void Display::dispOut(string& str, unsigned int n, double time) {
50
  cout << string(2*n, ' ') << prefix << "("  << n << ") Quit " << str << string(70-2*n-str.size(), ' ') << time << suffix << endl;
51 52
}

53
void Display::stepIn(string str) {
54
  return;
55 56 57
  str_container.push(make_pair(str, schrono()));
  dispIn(str, str_container.size());
  str_container.top().second=chrono::system_clock::now();
58 59
}

60
void Display::stepOut() {
61
  return;
62 63 64 65 66 67
  schrono stop=chrono::system_clock::now();
  pair<string, schrono>& pair=str_container.top();
  string& str=pair.first;
  std::chrono::duration<double> dur=stop-pair.second;
  dispOut(str, str_container.size(), dur.count());
  str_container.pop();
68 69
}

70 71
void Display::stepOut(string str) {
  return;
72
  string& str_verif = str_container.top().first;
73
  if (str == str_verif) {
74
    stepOut();
75 76 77
  } else {
    std::cout << "In Export.cc, " << str << " different from " << str_verif << endl;
  }
78 79 80 81 82
}




83
PostProcessing::PostProcessing(const xParseData& parse_data_, xexport::xExport &pexport_, xEntityFilter filter) :
Kevin Moreau's avatar
Kevin Moreau committed
84
  parse_data(parse_data_),
85
  export_manager(GetNbDigits(parse_data.getInt("nb_step_max"))),
86 87
  filter(filter) ,
  pexport(pexport_) {
Kevin Moreau's avatar
Kevin Moreau committed
88
  export_manager.init(parse_data, "export_manager");
89
}
90

Kevin Moreau's avatar
Kevin Moreau committed
91 92 93
void PostProcessing::init(xMesh& mesh) {
  sensor_manager.init(parse_data, &mesh, 2, "export_sensors_label", "export_sensors_point");
}
94 95 96
void PostProcessing::reinit(xMesh& mesh) {
  sensor_manager.reinit(&mesh);
}
97

98 99 100 101 102 103 104 105
bool PostProcessing::toExport(const std::string export_name) {
  return sensor_manager.toExport(export_name);
}

bool PostProcessing::toExport(const std::string export_name, const int step) {
  return export_manager.toExport(export_name, step, "");
}

106 107 108 109 110 111 112
void PostProcessing::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();
  }
}
113

114 115 116 117 118 119
void PostProcessing::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());
  }
}

120 121 122
void PostProcessing::exportOnTime(const std::string name, const double time, const double val) {
  sensor_manager.measure(val, time, name);
}
123

124 125 126
void PostProcessing::exportOnTime(const std::string name, const double time, const xEval<double>& eval) {
  sensor_manager.measureAll(eval, time, name);
}
127

128
void PostProcessing::exportOnTime(const std::string name, const double time, const xEval<xtensor::xVector<>>& eval) {
129 130
  sensor_manager.measureAll(eval, time, name);
}
131

132
void PostProcessing::exportOnTime(const std::string name, const double time, const xEval<xtensor::xTensor2<>>& eval) {
133 134
  sensor_manager.measureAll(eval, time, name);
}
135

136
void PostProcessing::saveMesh(std::string export_name, int step, xMesh& mesh) {
137
  if(step>0 && export_manager.toExport("save", step, "")) {
138 139 140 141
    Export(mesh, pexport, export_name+"_save");
  }
}

142
void PostProcessing::saveField(std::string export_name, int step, const xField<>& field, xRegion reg) {
143
  if(step>0 && export_manager.toExport("save", step, "")) {
144
    xValueManagerDist<double>* double_manager=field.getDoubleManager();
Kevin Moreau's avatar
Kevin Moreau committed
145 146
    std::ofstream oss(export_name+"_save.bin", ios::binary);
    if(oss.is_open()) {
Kevin Moreau's avatar
Kevin Moreau committed
147 148
      const int info_size=2*reg.dim()*sizeof(int);
      int nb=reg.size(reg.dim());
Kevin Moreau's avatar
Kevin Moreau committed
149
      oss.write((char*)&nb, sizeof(int));
Kevin Moreau's avatar
Kevin Moreau committed
150
      for(auto e: reg) {
Kevin Moreau's avatar
Kevin Moreau committed
151 152 153 154 155 156 157 158 159 160 161
        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) {
Kevin Moreau's avatar
Kevin Moreau committed
162
          auto n=e->get(0, i);
Kevin Moreau's avatar
Kevin Moreau committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
          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();
    }
  }
}

void PostProcessing::archiveSave(int step) {
180
  if(step>0 && export_manager.toExport("save", step, ".tar")) {
Kevin Moreau's avatar
Kevin Moreau committed
181 182 183 184 185 186
    if(system(NULL)==0) {
      std::cout<<"Error: system not callable"<<std::endl;
      std::abort();
    }
    std::ofstream oss("step_save.bin", ios::binary);
    if(oss.is_open()) {
187 188 189 190
      double load_factor, old_load_factor, load_factor_variation;
      Observer::tell("old_load_factor", old_load_factor);
      Observer::tell("load_factor", load_factor);
      Observer::tell("load_factor_variation", load_factor_variation);
Kevin Moreau's avatar
Kevin Moreau committed
191
      oss.write((char*)&step, sizeof(int));
192 193 194
      oss.write((char*)&old_load_factor, sizeof(double));
      oss.write((char*)&load_factor, sizeof(double));
      oss.write((char*)&load_factor_variation, sizeof(double));
Kevin Moreau's avatar
Kevin Moreau committed
195 196
      oss.close();
    }
197
    std::string command="tar cf "+export_manager.getFileName()+" *_save.*";
Kevin Moreau's avatar
Kevin Moreau committed
198
    system(command.c_str());
199
    system("rm -f *_save.*");
Kevin Moreau's avatar
Kevin Moreau committed
200 201
  }
}