GenericDispls.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 #include "GenericDispls.hpp"
25 #include <bitpit_operators.hpp>
26 #include <fstream>
27 
28 namespace mimmo {
29 
35  m_name = "mimmo.GenericDispls";
36  m_read = readMode;
37  m_nDispl = 0;
38  m_template = false;
39  m_dir = ".";
40  m_filename = m_name+"_source.dat";
41 };
42 
47 GenericDispls::GenericDispls(const bitpit::Config::Section & rootXML){
48 
49  m_name = "mimmo.GenericDispls";
50  m_nDispl = 0;
51  m_template = false;
52  m_dir = ".";
53  m_filename = m_name+"_source.dat";
54  m_read = true;
55 
56  std::string fallback_name = "ClassNONE";
57  std::string input = rootXML.get("ClassName", fallback_name);
58  input = bitpit::utils::string::trim(input);
59 
60  std::string fallback_name2 = "1";
61  std::string input2 = rootXML.get("IOmode", fallback_name2);
62  input2 = bitpit::utils::string::trim(input2);
63 
64  m_read = bool(std::atoi(input2.c_str()));
65 
66  if(input == "mimmo.GenericDispls"){
67  absorbSectionXML(rootXML);
68  }else{
70  };
71 }
72 
77 
82  m_read = other.m_read;
83  m_dir = other.m_dir;
84  m_filename = other.m_filename;
85  m_nDispl = other.m_nDispl;
86  m_displ = other.m_displ;
87  m_labels = other.m_labels;
88  m_template = other.m_template;
89 };
90 
96 {
97  std::swap(m_read, x.m_read);
98  std::swap(m_dir, x.m_dir);
99  std::swap(m_filename, x.m_filename);
100  std::swap(m_nDispl, x.m_nDispl);
101  std::swap(m_displ, x.m_displ);
102  std::swap(m_labels, x.m_labels);
103  std::swap(m_template, x.m_template);
105 }
109 void
111 
112  bool built = true;
113 
114  built = (built && createPortIn<dvecarr3E, GenericDispls>(this, &mimmo::GenericDispls::setDispl, M_DISPLS, !m_read));
115  built = (built && createPortIn<livector1D, GenericDispls>(this, &mimmo::GenericDispls::setLabels, M_VECTORLI));
116  built = (built && createPortIn<int, GenericDispls>(this, &mimmo::GenericDispls::setNDispl, M_VALUEI));
117 
118  built = (built && createPortOut<dvecarr3E, GenericDispls>(this, &mimmo::GenericDispls::getDispl, M_DISPLS));
119  built = (built && createPortOut<livector1D, GenericDispls>(this, &mimmo::GenericDispls::getLabels, M_VECTORLI));
120  built = (built && createPortOut<int, GenericDispls>(this, &mimmo::GenericDispls::getNDispl, M_VALUEI));
121 
122  m_arePortsBuilt = built;
123 }
124 
129 int
131  return m_nDispl;
132 };
133 
138 dvecarr3E
140  return m_displ;
141 };
142 
150  return m_labels;
151 };
152 
157 bool
159  return m_template;
160 };
161 
162 
167 void
168 GenericDispls::setReadDir(std::string dir){
169  if(!m_read) return;
170  m_dir = dir;
171 };
172 
177 void
178 GenericDispls::setReadFilename(std::string filename){
179  if(!m_read) return;
180  m_filename = filename;
181 };
182 
187 void
188 GenericDispls::setWriteDir(std::string dir){
189  if(m_read) return;
190  m_dir = dir;
191 };
192 
197 void
198 GenericDispls::setWriteFilename(std::string filename){
199  if(m_read) return;
200  m_filename = filename;
201 };
202 
209 void
211  if(m_read) return;
212  m_nDispl = nD;
213 };
214 
220 void
222  if(m_read) return;
223  m_labels.clear();
224  m_labels = labels;
225  if(m_displ.empty()) setNDispl(int(labels.size()));
226 };
227 
233 void
235  if(m_read) return;
236  m_displ.clear();
237  m_displ = displs;
238  m_nDispl = m_displ.size();
239 };
240 
245 void
247  if(m_read) return;
248  m_template = flag;
249 };
250 
254 void
256  m_nDispl = 0;
257  m_displ.clear();
258  m_labels.clear();
259  m_template = false;
260  m_filename = m_name+"_source.dat";
261 }
262 
267 void
269 
270  if(m_read){
271  read();
272  }else{
273  write();
274  }
275 };
276 
282 void GenericDispls::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
283 
284  BITPIT_UNUSED(name);
285 
286  std::string input;
287 
288  //checking IOmode
289 
290  if(slotXML.hasOption("IOmode")){
291  input = slotXML.get("IOmode");
292  bool value = true;
293  if(!input.empty()){
294  std::stringstream ss(bitpit::utils::string::trim(input));
295  ss>>value;
296  }
297  if (m_read != value){
298  (*m_log)<< "warning in class "<<m_name<<": cannot absorb xml parameters for class IOmode mismatching"<<std::endl;
299  throw std::runtime_error (m_name + " : xml absorbing failed");
300  }
301  };
302 
303  BaseManipulation::absorbSectionXML(slotXML, name);
304 
305  if(m_read){
306  if(slotXML.hasOption("ReadDir")){
307  std::string input = slotXML.get("ReadDir");
308  input = bitpit::utils::string::trim(input);
309  setReadDir(input);
310  };
311 
312  if(slotXML.hasOption("ReadFilename")){
313  std::string input = slotXML.get("ReadFilename");
314  input = bitpit::utils::string::trim(input);
315  setReadFilename(input);
316  };
317  }else{
318  if(slotXML.hasOption("WriteDir")){
319  std::string input = slotXML.get("WriteDir");
320  input = bitpit::utils::string::trim(input);
321  setWriteDir(input);
322  };
323 
324  if(slotXML.hasOption("WriteFilename")){
325  std::string input = slotXML.get("WriteFilename");
326  input = bitpit::utils::string::trim(input);
327  setWriteFilename(input);
328  };
329  }
330 
331  if(slotXML.hasOption("NDispl")){
332  std::string input = slotXML.get("NDispl");
333  input = bitpit::utils::string::trim(input);
334  int temp = 0;
335  if(!input.empty()){
336  std::stringstream ss(input);
337  ss>>temp;
338  }
339  setNDispl(temp);
340  };
341 
342  if(slotXML.hasOption("Template")){
343  std::string input = slotXML.get("Template");
344  input = bitpit::utils::string::trim(input);
345  bool temp = false;
346  if(!input.empty()){
347  std::stringstream ss(input);
348  ss>>temp;
349  }
350  setTemplate(temp);
351  };
352 
353 }
354 
360 void GenericDispls::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
361 
362  BITPIT_UNUSED(name);
363 
364  BaseManipulation::flushSectionXML(slotXML, name);
365 
366  slotXML.set("IOmode", std::to_string(int(m_read)));
367  if(m_read){
368  slotXML.set("ReadDir", m_dir);
369  slotXML.set("ReadFilename", m_filename);
370  }else{
371  slotXML.set("WriteDir", m_dir);
372  slotXML.set("WriteFilename", m_filename);
373  }
374  slotXML.set("NDispl", std::to_string(m_nDispl));
375  slotXML.set("Template", std::to_string(int(m_template)));
376 
377 };
378 
379 
383 void GenericDispls::read(){
384 
385  std::ifstream reading;
386  std::string source = m_dir+"/"+ m_filename;
387  reading.open(source.c_str());
388 
389 #if MIMMO_ENABLE_MPI
390  // Leave the reading only to the master rank
391  if(getRank() == 0)
392 #endif
393  {
394 
395  if(reading.is_open()){
396 
397  m_displ.clear();
398  m_labels.clear();
399 
400  std::string keyword, line;
401  long label;
402  darray3E dtrial;
403 
404  do{
405  line.clear();
406  keyword.clear();
407  std::getline(reading,line);
408  line = bitpit::utils::string::trim(line);
409  std::stringstream ss(line);
410  ss>>keyword;
411  keyword = bitpit::utils::string::trim(keyword);
412  if(keyword == "$DISPL") {
413 
414  ss>>label;
415  m_labels.push_back(label);
416 
417  dtrial.fill(0.0);
418  ss>>dtrial[0]>>dtrial[1]>>dtrial[2];
419  m_displ.push_back(dtrial);
420  }
421 
422  }while(!reading.eof());
423 
424  m_nDispl = m_displ.size();
425 
426  }else{
427  (*m_log)<<"error of "<<m_name<<" : cannot open "<<m_filename<< " requested. Exiting... "<<std::endl;
428  throw std::runtime_error (m_name + " : cannot open " + m_filename + " requested");
429  }
430 
431  reading.close();
432 
433  }
434 
435 #if MIMMO_ENABLE_MPI
436  // Communicate the data read by master rank to other ranks
437  MPI_Bcast(&m_nDispl, 1, MPI_INT, 0, m_communicator);
438 
439  m_displ.resize(m_nDispl);
440  MPI_Bcast(m_displ.data(), 3*m_nDispl, MPI_DOUBLE, 0, m_communicator);
441 
442  m_labels.resize(m_nDispl);
443  MPI_Bcast(m_labels.data(), m_nDispl, MPI_LONG, 0, m_communicator);
444 #endif
445 
446 };
447 
451 void GenericDispls::write(){
452 
453 #if MIMMO_ENABLE_MPI
454  // Leave the writing only to the master rank
455  if(getRank() == 0)
456 #endif
457  {
458 
459  //assessing data;
460  int sizelabels = m_labels.size();
461  long maxlabel = 0;
462  for(const auto & val: m_labels){
463  maxlabel = std::max(maxlabel,val);
464  }
465 
466  int sizedispl = m_displ.size();
467 
468  m_labels.resize(m_displ.size(), -1);
469  if(sizelabels < sizedispl){
470  for(int i = sizelabels; i<sizedispl; ++i){
471  m_labels[i] = maxlabel+1;
472  ++maxlabel;
473  }
474  }
475 
476  std::string filename;
477  if(m_template){
478  filename = "TEMPLATE_"+m_filename;
479  }else{
480  filename = m_filename;
481  }
482 
483  std::ofstream writing;
484  std::string source = m_dir+"/"+ m_filename;
485  writing.open(source.c_str());
486  std::string keyT1 = "{", keyT2 = "}";
487  if(writing.is_open()){
488 
489  int counter = 0;
490  for(const auto & dd : m_displ){
491  if(m_template){
492  std::string str1 = keyT1+"x"+std::to_string(m_labels[counter])+keyT2;
493  std::string str2 = keyT1+"y"+std::to_string(m_labels[counter])+keyT2;
494  std::string str3 = keyT1+"z"+std::to_string(m_labels[counter])+keyT2;
495 
496  writing<<"$DISPL"<<'\t'<<m_labels[counter]<<'\t'<<str1<<'\t'<<str2<<'\t'<<str3<<std::endl;
497  }else{
498  writing<<"$DISPL"<<'\t'<<m_labels[counter]<<'\t'<<dd[0]<<'\t'<<dd[1]<<'\t'<<dd[2]<<std::endl;
499  }
500  ++counter;
501  }
502 
503  }else{
504  (*m_log)<<"error of "<<m_name<<" : cannot open "<<m_filename<< " requested. Exiting... "<<std::endl;
505  throw std::runtime_error (m_name + " : cannot open " + m_filename + " requested");
506  }
507 
508  writing.close();
509 
510  }
511 };
512 
513 
514 }
void setLabels(livector1D labels)
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void swap(GenericDispls &) noexcept
#define M_DISPLS
std::vector< darray3E > dvecarr3E
#define M_VALUEI
std::vector< long > livector1D
void warningXML(bitpit::Logger *log, std::string name)
BaseManipulation is the base class of any manipulation object of the library.
GenericDispls(bool readMode=true)
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void setReadFilename(std::string filename)
void setTemplate(bool flag)
void setDispl(dvecarr3E displs)
void setWriteFilename(std::string filename)
void setWriteDir(std::string dir)
GenericDispls is the class to read from file an initial set of displacements as a generic vector fiel...
std::array< double, 3 > darray3E
void setReadDir(std::string dir)
void swap(BaseManipulation &x) noexcept
#define M_VECTORLI
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")