#include "grid.hpp" #include "attribute_template_impl.hpp" #include "object_template_impl.hpp" #include "group_template_impl.hpp" namespace xmlioserver { namespace tree { /// ////////////////////// Définitions ////////////////////// /// CGrid::CGrid(void) : CObjectTemplate(), CGridAttributes() , withAxis(false), isChecked(false), axis(), domain() , storeIndex(1), out_i_index(1), out_j_index(1), out_l_index(1) { /* Ne rien faire de plus */ } CGrid::CGrid(const StdString & id) : CObjectTemplate(id), CGridAttributes() , withAxis(false), isChecked(false), axis(), domain() , storeIndex(1), out_i_index(1), out_j_index(1), out_l_index(1) { /* Ne rien faire de plus */ } CGrid::~CGrid(void) { this->axis.reset() ; this->domain.reset() ; for (StdSize i = 0; i < this->storeIndex.size(); i++) { this->storeIndex[i].reset(); this->out_i_index[i].reset(); this->out_j_index[i].reset(); this->out_l_index[i].reset(); } } ///--------------------------------------------------------------- StdString CGrid::GetName(void) { return (StdString("grid")); } StdString CGrid::GetDefName(void) { return (CGrid::GetName()); } ENodeType CGrid::GetType(void) { return (eGrid); } //---------------------------------------------------------------- const std::deque & CGrid::getStoreIndex(void) const { return (this->storeIndex ); } //--------------------------------------------------------------- const std::deque & CGrid::getOutIIndex(void) const { return (this->out_i_index ); } //--------------------------------------------------------------- const std::deque & CGrid::getOutJIndex(void) const { return (this->out_j_index ); } //--------------------------------------------------------------- const std::deque & CGrid::getOutLIndex(void) const { return (this->out_l_index ); } //--------------------------------------------------------------- const boost::shared_ptr CGrid::getRelAxis (void) const { return (this->axis ); } //--------------------------------------------------------------- const boost::shared_ptr CGrid::getRelDomain(void) const { return (this->domain ); } //--------------------------------------------------------------- bool CGrid::hasAxis(void) const { return (this->withAxis); } //--------------------------------------------------------------- void CGrid::solveReference(void) { if (this->isChecked) return; this->solveDomainRef() ; this->solveAxisRef() ; if (this->storeIndex.size() == 1) { this->computeIndex() ; } else { this->computeIndexServer(); } this->isChecked = true; } //--------------------------------------------------------------- void CGrid::solveDomainRef(void) { if (!domain_ref.isEmpty()) { if (CObjectFactory::HasObject(domain_ref.getValue())) { this->domain = CObjectFactory::GetObject(domain_ref.getValue()) ; domain->checkAttributes() ; } else ERROR("CGrid::solveDomainRef(void)", << "Référence au domaine incorrecte") ; } else ERROR("CGrid::solveDomainRef(void)", << "Domaine non défini") ; } //--------------------------------------------------------------- void CGrid::solveAxisRef(void) { if (!axis_ref.isEmpty()) { this->withAxis = true ; if (CObjectFactory::GetObject(axis_ref.getValue())) { this->axis = CObjectFactory::GetObject(axis_ref.getValue()) ; axis->checkAttributes() ; } else ERROR("CGrid::solveAxisRef(void)", << "Référence a l'axe incorrecte") ; } else withAxis = false ; } //--------------------------------------------------------------- void CGrid::computeIndex(void) { const int ni = domain->ni.getValue() , nj = domain->nj.getValue() , size = (this->hasAxis()) ? axis->size.getValue() : 1 ; /*std::cout << ni << " : " << nj << " : " << size << std::endl;*/ const int data_dim = domain->data_dim.getValue() , data_n_index = domain->data_n_index.getValue() , data_ibegin = domain->data_ibegin.getValue() , data_jbegin = (data_dim == 2) ? domain->data_jbegin.getValue() : -1; ARRAY(int, 1) data_i_index = domain->data_i_index.getValue() , data_j_index = domain->data_j_index.getValue() ; /*std::cout << data_n_index << " : " << data_i_index << " : " << data_j_index << std::endl; */ ARRAY(bool, 2) mask = domain->mask.getValue() ; int indexCount = 0; for(int l = 0; l < size ; l++) { for(int n = 0, i = 0, j = 0; n < data_n_index; n++) { int temp_i = (*data_i_index)[n] + data_ibegin, temp_j = (data_dim == 1) ? -1 : (*data_j_index)[n] + data_jbegin; i = (data_dim == 1) ? (temp_i - 2) % ni : (temp_i - 1) ; j = (data_dim == 1) ? (temp_i - 2) / ni : (temp_j - 1) ; if ((i >= 0 && i < ni) && (j >= 0 && j < nj) && (*mask)[i][j]) indexCount++ ; } } //std::cout << indexCount << std::endl; ARRAY_ASSIGN(this->storeIndex[0] , int, 1, [indexCount]); ARRAY_ASSIGN(this->out_l_index[0], int, 1, [indexCount]); ARRAY_ASSIGN(this->out_i_index[0], int, 1, [indexCount]); ARRAY_ASSIGN(this->out_j_index[0], int, 1, [indexCount]); for(int count = 0, indexCount = 0, l = 0; l < size; l++) { for(int n = 0, i = 0, j = 0; n < data_n_index; n++, count++) { int temp_i = (*data_i_index)[n] + data_ibegin, temp_j = (data_dim == 1) ? -1 : (*data_j_index)[n] + data_jbegin; i = (data_dim == 1) ? (temp_i - 2) % ni : (temp_i - 1) ; j = (data_dim == 1) ? (temp_i - 2) / ni : (temp_j - 1) ; if ((i >= 0 && i < ni) && (j >= 0 && j < nj) && (*mask)[i][j]) { (*this->storeIndex[0]) [indexCount] = count ; (*this->out_l_index[0])[indexCount] = l ; (*this->out_i_index[0])[indexCount] = i ; (*this->out_j_index[0])[indexCount] = j ; indexCount++ ; } } } } //---------------------------------------------------------------- boost::shared_ptr CGrid::CreateGrid(boost::shared_ptr domain) { StdString new_id = StdString("__") + domain->getId() + StdString("__") ; boost::shared_ptr grid = CGroupFactory::CreateChild(CObjectFactory::GetObject ("grid_definition"), new_id); grid->domain_ref.setValue(domain->getId()); return (grid); } boost::shared_ptr CGrid::CreateGrid(boost::shared_ptr domain, boost::shared_ptr axis) { StdString new_id = StdString("__") + domain->getId() + StdString("_") + axis->getId() + StdString("__") ; boost::shared_ptr grid = CGroupFactory::CreateChild(CObjectFactory::GetObject ("grid_definition"), new_id); grid->domain_ref.setValue(domain->getId()); grid->axis_ref.setValue(axis->getId()); return (grid); } //---------------------------------------------------------------- template <> void CGrid::outputField (const ARRAY(double, 1) stored, ARRAY(double, 3) field) const { for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++) (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]][(*out_l_index[0])[n]] = (*stored)[n] ; } //--------------------------------------------------------------- template <> void CGrid::outputField (const ARRAY(double, 1) stored, ARRAY(double, 2) field) const { for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++) (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]] = (*stored)[n] ; } //--------------------------------------------------------------- template <> void CGrid::outputField (const ARRAY(double, 1) stored, ARRAY(double, 1) field) const { for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++) (*field)[(*out_i_index[0])[n]] = (*stored)[n] ; } //---------------------------------------------------------------- void CGrid::storeField_arr (const double * const data, ARRAY(double, 1) stored) const { const StdSize size = storeIndex[0]->num_elements() ; stored->resize(boost::extents[size]) ; for(StdSize i = 0; i < size; i++) (*stored)[i] = data[(*storeIndex[0])[i]] ; } //--------------------------------------------------------------- void CGrid::toBinary (StdOStream & os) const { SuperClass::toBinary(os); os.write (reinterpret_cast(&this->isChecked), sizeof(bool)); if (this->isChecked) { this->storeIndex [0]->toBinary(os); this->out_i_index[0]->toBinary(os); this->out_j_index[0]->toBinary(os); this->out_l_index[0]->toBinary(os); } } //--------------------------------------------------------------- void CGrid::fromBinary(StdIStream & is) { bool hasIndex = false; SuperClass::fromBinary(is); is.read (reinterpret_cast(&hasIndex), sizeof(bool)); if (hasIndex) { ARRAY_CREATE(storeIndex_ , int, 1, [0]); ARRAY_CREATE(out_l_index_, int, 1, [0]); ARRAY_CREATE(out_i_index_, int, 1, [0]); ARRAY_CREATE(out_j_index_, int, 1, [0]); storeIndex_ ->fromBinary(is); out_i_index_->fromBinary(is); out_j_index_->fromBinary(is); out_l_index_->fromBinary(is); this->storeIndex .push_back(storeIndex_); this->out_i_index.push_back(out_i_index_); this->out_j_index.push_back(out_j_index_); this->out_l_index.push_back(out_l_index_); } } //--------------------------------------------------------------- void CGrid::computeIndexServer(void) { ARRAY(int, 1) storeIndex_srv = this->storeIndex[0]; ARRAY(int, 1) out_i_index_srv = this->out_i_index[0]; ARRAY(int, 1) out_j_index_srv = this->out_j_index[0]; ARRAY(int, 1) out_l_index_srv = this->out_l_index[0]; ARRAY(int, 2) local_mask = this->domain->getLocalMask(); const std::vector & ibegin = this->domain->getIBeginSub(); const std::vector & jbegin = this->domain->getJBeginSub(); const int ibegin_srv = this->domain->ibegin.getValue(); const int jbegin_srv = this->domain->jbegin.getValue(); const int zoom_ni_srv = this->domain->zoom_ni_loc.getValue(); const int zoom_nj_srv = this->domain->zoom_nj_loc.getValue(); const int ibegin_zoom_srv = this->domain->zoom_ibegin_loc.getValue(); const int jbegin_zoom_srv = this->domain->zoom_jbegin_loc.getValue(); StdSize dn = 0; for (StdSize j = 1; j < this->out_i_index.size(); j++) dn += this->out_i_index[j]->size(); ARRAY_ASSIGN(storeIndex_srv , int, 1, [dn]); ARRAY_ASSIGN(out_i_index_srv, int, 1, [dn]); ARRAY_ASSIGN(out_j_index_srv, int, 1, [dn]); ARRAY_ASSIGN(out_l_index_srv, int, 1, [dn]); for (StdSize i = 0, dn = 0; i < ibegin.size(); i++) { ARRAY(int, 1) storeIndex_cl = this->storeIndex [i + 1]; ARRAY(int, 1) out_i_index_cl = this->out_i_index[i + 1]; ARRAY(int, 1) out_j_index_cl = this->out_j_index[i + 1]; ARRAY(int, 1) out_l_index_cl = this->out_l_index[i + 1]; const int ibegin_cl = ibegin[i]; const int jbegin_cl = jbegin[i]; for (StdSize n = dn, m = 0; n < (dn + storeIndex_cl->size()); n++, m++) { (*storeIndex_srv)[n] = (*storeIndex_cl)[m] + dn; // Faux mais inutile dans tous les cas. (*out_i_index_srv)[n] = (*out_i_index_cl)[m] + (ibegin_cl - 1) - (ibegin_srv - 1) - (ibegin_zoom_srv - 1); (*out_j_index_srv)[n] = (*out_j_index_cl)[m] + (jbegin_cl - 1) - (jbegin_srv - 1) - (jbegin_zoom_srv - 1); (*out_l_index_srv)[n] = (*out_l_index_cl)[m]; } dn += storeIndex_cl->size(); } if (storeIndex_srv->size() != 0) { const int ibegin_t = *std::min_element(out_i_index_srv->begin(), out_i_index_srv->end()); const int iend_t = *std::max_element(out_i_index_srv->begin(), out_i_index_srv->end()); const int jbegin_t = *std::min_element(out_j_index_srv->begin(), out_j_index_srv->end()); const int jend_t = *std::max_element(out_j_index_srv->begin(), out_j_index_srv->end()); if ((ibegin_t < 0) || (jbegin_t < 0) || (iend_t >= zoom_ni_srv) || (jend_t >= zoom_nj_srv)) { ERROR("CGrid::computeIndexServer(void)", <<"Erreur d'indexation de la grille au niveau du serveur") ; } } if (storeIndex_srv->size() != 0) for (StdSize u = 0; u < storeIndex_srv->size(); u++) (*local_mask)[(*out_i_index_srv)[u]][(*out_j_index_srv)[u]] = 1; //~ StdOFStream ofs(this->getId().c_str()); //~ for (StdSize h = 0; h < storeIndex_srv->size(); h++) //~ { //~ ofs << "(" << (*storeIndex_srv)[h] << ";" //~ << (*out_i_index_srv)[h] << "," //~ << (*out_j_index_srv)[h] << "," //~ << (*out_l_index_srv)[h] << ")" << std::endl; //~ } //~ ofs.close(); this->storeIndex.resize(1); this->out_i_index.resize(1); this->out_j_index.resize(1); this->out_l_index.resize(1); } //---------------------------------------------------------------- void CGrid::inputFieldServer (const std::deque storedClient, ARRAY(double, 1) storedServer) const { if ((this->storeIndex.size()-1 ) != storedClient.size()) ERROR("CGrid::inputFieldServer(...)", << "[ Nombre de tableau attendu = " << (this->storeIndex.size()-1) << ", " << "[ Nombre de tableau reçu = " << storedClient.size() << "] " << "Les données d'un client sont manquantes !") ; if (storedServer.get() != NULL) storedServer->resize(boost::extents[this->storeIndex.size()-1]); else ARRAY_ASSIGN(storedServer, double, 1, [this->storeIndex.size()-1]); for (StdSize i = 0, n = 0; i < storedClient.size(); i++) for (StdSize j = 0; j < storedClient[i]->num_elements(); j++) (*storedServer)[n++] = (*storedClient[i])[j]; } ///--------------------------------------------------------------- } // namespace tree } // namespace xmlioserver