Corsen.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * \file Corsen.cpp
  3. * \brief Charge les différents fichiers en mémoire et lance la
  4. * génération du graphe.
  5. * \author The VLE Development Team
  6. * See the AUTHORS or Authors.txt file
  7. * \version 2.0
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  11. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include "Corsen.hpp"
  27. int Coordinates::_columnsNumber = 0;
  28. Corsen::Corsen() : _graph()
  29. {
  30. _pfs = NULL;
  31. _efs = NULL;
  32. _ofs = NULL;
  33. _gfs = NULL;
  34. _lfs = NULL;
  35. _sfs = NULL;
  36. _dfs = NULL;
  37. _elevationMatrixBv = NULL;
  38. _outlet = NULL;
  39. _soilMatrix = NULL;
  40. _gradientMatrix = NULL;
  41. _elevationMatrix = NULL;
  42. _outlets = NULL;
  43. _param = NULL;
  44. _layers = NULL;
  45. }
  46. void Corsen::read(std::vector < std::string* > & files,
  47. std::string& absolutePath) throw (FileNotFound, ReadError,
  48. InvalidFileFormat,
  49. std::invalid_argument)
  50. {
  51. std::stringstream ss;
  52. _pfs = new ParametersFileStream(*files.at(0), *files.at(1));
  53. _efs = new ElevationFileStream(*files.at(2));
  54. _ofs = new OutletFileStream(*files.at(3));
  55. _gfs = new SlopeFileStream(*files.at(4));
  56. _dfs = new DrainFileStream<corsenContainers::vecS, float>(*files.at(4));
  57. ContextData * ctxtD = ContextData::getInstance();
  58. ContextFileReader::read((*files.at(5)).c_str(), ctxtD->_numberDays,
  59. ctxtD->_simulationStartDate,
  60. ctxtD->_outletsCoordinates);
  61. _sfs = new SoilFileStream(absolutePath + ctxtD->_soilFileName);
  62. _layers = new std::vector<Layer>();
  63. _layers->push_back(Layer(std::string("Reseau Hydrographique"), 0, 0,
  64. RESEAU_HYDRO, false));
  65. _param = _pfs->read();
  66. Coordinates::_columnsNumber = _param->_columnsNumber;
  67. _soilMatrix = _sfs->read();
  68. _gradientMatrix = _gfs->read();
  69. _elevationMatrix = _efs->read();
  70. _outlets = _ofs->read();
  71. int layerNumber = Layer::find(*_layers, RESEAU_HYDRO);
  72. if(layerNumber == -1)
  73. throw std::invalid_argument(
  74. "Impossible de trouver l'élément dans le tableau de couches");
  75. ss << *files.at(6) << layerNumber;
  76. _lfs = new LayersFileStream(ss.str(), _param);
  77. _layers->at(layerNumber).setData(_lfs->read());
  78. }
  79. void Corsen::buildGraph()
  80. {
  81. Coordinates c(ContextData::getInstance()->_outletsCoordinates[0].line, ContextData::getInstance()->_outletsCoordinates[0].column);
  82. for(std::list<Outlet *>::const_iterator it = _outlets->begin(); it!=_outlets->end();it++)
  83. {
  84. if((*it)->getCoord() == c)
  85. {
  86. _outlet = *it;
  87. _outlet->setBrother(NULL);
  88. break;
  89. }
  90. _outlet = (*it)->searchChildNode(c);
  91. if (_outlet != NULL)
  92. {
  93. _outlet->setBrother(NULL);
  94. break;
  95. }
  96. }
  97. _layers->at(Layer::find(*_layers, RESEAU_HYDRO)).matbiton(_outlet->getCoord(), _param);
  98. _efs->write(*_elevationMatrix, std::string(".sav"));
  99. std::stack<Node *> stack;
  100. stack.push(_outlet);
  101. _elevationMatrixBv = new std::vector<float>(_elevationMatrix->size());
  102. while (not stack.empty())
  103. {
  104. Node * s = stack.top();
  105. unsigned int index = s->getCoord().getLine() * _param->_columnsNumber + s->getCoord().getColumn();
  106. _elevationMatrixBv->at(index) = _elevationMatrix->at(index);
  107. stack.pop();
  108. if (s->getBrother())
  109. stack.push(s->getBrother());
  110. if (s->getChild())
  111. stack.push(s->getChild());
  112. }
  113. _graph.build(_elevationMatrixBv, _param);
  114. }
  115. void Corsen::display()
  116. {
  117. std::cout<<"Les paramètres du programme sont :"<<_param->toString()<<std::endl;
  118. std::cout<<"Liste d'adjacence :"<<std::endl;
  119. _graph.display();
  120. #ifdef DEBUG
  121. std::cout<<"Tableau de couches :"<<std::endl;
  122. for(std::vector<Layer>::const_iterator it = _layers->begin(); it!=_layers->end(); it++)
  123. it->display(_param);
  124. #endif
  125. }
  126. Corsen::~Corsen()
  127. {
  128. if(_outlets !=NULL)
  129. {
  130. for(std::list<Outlet *>::iterator it = _outlets->begin(); it!=_outlets->end(); it++)
  131. {
  132. delete (*it);
  133. *it = NULL;
  134. }
  135. }
  136. if(_soilMatrix != NULL)
  137. delete _soilMatrix;
  138. if(_gradientMatrix !=NULL)
  139. delete _gradientMatrix;
  140. if(_param !=NULL)
  141. delete _param;
  142. if(_outlets !=NULL)
  143. delete _outlets;
  144. if(_elevationMatrix !=NULL)
  145. delete _elevationMatrix;
  146. if(_pfs != NULL)
  147. delete _pfs;
  148. if(_efs != NULL)
  149. delete _efs;
  150. if(_ofs != NULL)
  151. delete _ofs;
  152. if(_gfs != NULL)
  153. delete _gfs;
  154. if(_sfs != NULL)
  155. delete _sfs;
  156. if(_lfs != NULL)
  157. delete _lfs;
  158. if(_layers != NULL)
  159. delete _layers;
  160. if(_elevationMatrixBv != NULL)
  161. delete _elevationMatrixBv;
  162. if(_dfs != NULL)
  163. delete _dfs;
  164. ContextData::kill();
  165. #ifdef DEBUG
  166. std::cout<<"destruction achevée \n";
  167. #endif
  168. }