コード例 #1
0
ファイル: loadparticles.cpp プロジェクト: sigvebs/PDtools
void LoadParticles::loadBinaryBody(Particles &particles, FILE *rawData,
                                   unordered_map<string, int> parameters) {
  particles.maxParticles(m_nParticles);
  particles.nParticles(m_nParticles);
  particles.totParticles(m_nParticles);
  // Storing only non-basic parameters in the parameters
  int counter = 0;
  vector<pair<int, int>> data_config_mapping;
  for (auto param : parameters) {
    bool found = false;
    for (string basic_parameter : basicParameters) {
      if (param.first == basic_parameter) {
        found = true;
      }
    }

    if (!found) {
      particles.parameters()[param.first] = counter;
      data_config_mapping.push_back(pair<int, int>(counter, param.second));
      counter++;
    }
  }

  //--------------------------------------------------------------------------
  bool idIsset = false;
  int idPos = 0;
  if (parameters.count("id") > 0) {
    idIsset = true;
    idPos = parameters["id"];
  }

  vector<pair<int, int>> position_config;
  int dim = 0;
  if (parameters.count("x") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(0, parameters["x"]));
  }
  if (parameters.count("y") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(1, parameters["y"]));
  }
  if (parameters.count("z") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(2, parameters["z"]));
  }
  particles.dim(dim);

  //--------------------------------------------------------------------------
  // Creating the data matrix
  particles.initializeMatrices();

  int nColumns = m_nColumns;
  ivec &idToCol = particles.getIdToCol_v();
  arma::ivec &colToId = particles.colToId();
  arma::mat &r = particles.r();
  arma::mat &data = particles.data();

  // Reading all the data from file
  for (unsigned int i = 0; i < particles.nParticles(); i++) {
    double line[nColumns];
    fread(&line[0], nColumns * sizeof(double), 1, rawData);

    // Collecting the data
    if (idIsset) {
      idToCol[int(line[idPos])] = i;
      colToId[i] = int(line[idPos]);
    } else {
      idToCol[i] = i;
      colToId[i] = i;
    }

    for (pair<int, int> pc : position_config) {
      r(i, pc.first) = line[pc.second];
    }
    for (pair<int, int> dfc : data_config_mapping) {
      data(i, dfc.first) = line[dfc.second];
    }
  }
}
コード例 #2
0
ファイル: loadparticles.cpp プロジェクト: sigvebs/PDtools
void LoadParticles::loadBody(Particles &particles, std::fstream &rawData,
                             unordered_map<string, int> parameters) {
  particles.maxParticles(m_nParticles);
  particles.nParticles(m_nParticles);
  particles.totParticles(m_nParticles);
  string line;

  //--------------------------------------------------------------------------
  // Storing only non-basic parameters in the parameters
  int counter = 0;
  vector<pair<int, int>> data_config_mapping;
  for (auto param : parameters) {
    bool found = false;

    for (string basic_parameter : basicParameters) {
      if (param.first == basic_parameter) {
        found = true;
      }
    }

    if (!found) {
      particles.parameters()[param.first] = counter;
      data_config_mapping.push_back(pair<int, int>(counter, param.second));
      counter++;
    }
  }

  //--------------------------------------------------------------------------
  bool idIsset = false;
  int idPos = 0;
  if (parameters.count("id") > 0) {
    idIsset = true;
    idPos = parameters["id"];
  }

  vector<pair<int, int>> position_config;
  int dim = 0;
  if (parameters.count("x") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(0, parameters["x"]));
  }
  if (parameters.count("y") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(1, parameters["y"]));
  }
  if (parameters.count("z") > 0) {
    dim++;
    position_config.push_back(pair<int, int>(2, parameters["z"]));
  }
  particles.dim(dim);

  //--------------------------------------------------------------------------
  // Creating the data matrix
  particles.initializeMatrices();
  ivec &idToCol = particles.getIdToCol_v();
  arma::ivec &colToId = particles.colToId();
  arma::mat &r = particles.r();
  arma::mat &data = particles.data();

  // Reading all the data from file
  for (unsigned int i = 0; i < particles.nParticles(); i++) {
    vector<string> lineSplit;
    getline(rawData, line);
    boost::trim_if(line, boost::is_any_of("\t "));
    boost::split(lineSplit, line, boost::is_any_of("\t "),
                 boost::token_compress_on);

    // Collecting the data
    if (idIsset) {
      idToCol[stoi(lineSplit[idPos])] = i;
      colToId[i] = stoi(lineSplit[idPos]);
    } else {
      idToCol[i] = i;
      colToId[i] = i;
    }

    for (pair<int, int> pc : position_config) {
      r(i, pc.first) = stod(lineSplit[pc.second]);
    }
    for (pair<int, int> dfc : data_config_mapping) {
      data(i, dfc.first) = stod(lineSplit[dfc.second]);
    }
  }
}