Esempio n. 1
0
int main(int argc, char** argv)
{

  initialize_global_variables();

  int ret = init_mpi(&argc, &argv);

  if (ret != POTFIT_SUCCESS) {
    shutdown_mpi();
    return EXIT_FAILURE;
  }

  read_input_files(argc, argv);

  g_mpi.init_done = 1;

  ret = broadcast_params_mpi();

  switch (ret) {
    case POTFIT_ERROR_MPI_CLEAN_EXIT:
      shutdown_mpi();
      return EXIT_SUCCESS;
    case POTFIT_ERROR:
      shutdown_mpi();
      return EXIT_FAILURE;
  }

  g_calc.ndim = g_pot.opt_pot.idxlen;
  g_calc.ndimtot = g_pot.opt_pot.len;

  // main force vector, all forces, energies, stresses, etc. will be stored here
  g_calc.force = (double*)Malloc(g_calc.mdim * sizeof(double));

  // starting positions for the force vector
  set_force_vector_pointers();

#if defined(APOT)
#if defined(MPI)
  MPI_Bcast(g_pot.opt_pot.table, g_calc.ndimtot, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif  // MPI
  update_calc_table(g_pot.opt_pot.table, g_pot.calc_pot.table, 1);
#endif  // APOT

  if (g_mpi.myid > 0) {
    start_mpi_worker(g_calc.force);
  } else {
#if defined(MPI)
    if (g_mpi.num_cpus > g_config.nconf) {
      warning("You are using more CPUs than you have configurations!\n");
      warning("While this will not do any harm, you are wasting %d CPUs.\n",
              g_mpi.num_cpus - g_config.nconf);
    }
#endif  // MPI

    time_t start_time;
    time_t end_time;

    time(&start_time);

    if (g_param.opt && g_calc.ndim > 0) {
      run_optimization();
    } else if (g_calc.ndim == 0) {
      printf(
          "\nOptimization disabled due to 0 free parameters. Calculating "
          "errors.\n");
    } else {
      printf("\nOptimization disabled. Calculating errors.\n\n");
    }

    time(&end_time);

#if defined(APOT)
    double tot = calc_forces(g_pot.opt_pot.table, g_calc.force, 0);
#else
    double tot = calc_forces(g_pot.calc_pot.table, g_calc.force, 0);
#endif  // APOT

#if defined(UQ)

    return uncertainty_quantification(tot,g_files.sloppyfile);

#endif //UQ

      
    write_pot_table_potfit(g_files.endpot);
    return 0;
    {
      int format = -1;

      switch (g_pot.format_type) {
        case POTENTIAL_FORMAT_UNKNOWN:
          error(1, "Unknown potential format detected! (%s:%d)\n", __FILE__,
                __LINE__);
        case POTENTIAL_FORMAT_ANALYTIC:
          format = 0;
          break;
        case POTENTIAL_FORMAT_TABULATED_EQ_DIST:
          format = 3;
          break;
        case POTENTIAL_FORMAT_TABULATED_NON_EQ_DIST:
          format = 4;
          break;
      }

      printf("\nPotential in format %d written to file \t%s\n", format,
             g_files.endpot);
    }

    if (g_param.writeimd == 1)
      write_pot_table_imd(g_files.imdpot);

    if (g_param.plot == 1)
      write_plotpot_pair(&g_pot.calc_pot, g_files.plotfile);

    if (g_param.write_lammps == 1)
      write_pot_table_lammps();

// will not work with MPI
#if defined(PDIST) && !defined(MPI)
    write_pairdist(&g_pot.opt_pot, g_files.distfile);
#endif  // PDIST && !MPI

    // write the error files for forces, energies, stresses, ...
    write_errors(g_calc.force, tot);

    /* calculate total runtime */
    if (g_param.opt && g_mpi.myid == 0 && g_calc.ndim > 0) {
      printf("\nRuntime: %d hours, %d minutes and %d seconds.\n",
             (int)difftime(end_time, start_time) / 3600,
             ((int)difftime(end_time, start_time) % 3600) / 60,
             (int)difftime(end_time, start_time) % 60);
      printf("%d force calculations, each took %f seconds\n", g_calc.fcalls,
             (double)difftime(end_time, start_time) / g_calc.fcalls);
    }

#if defined(MPI)
    calc_forces(NULL, NULL, 1); /* go wake up other threads */
#endif                          // MPI
  }                             /* myid == 0 */

// do some cleanups before exiting

#if defined(MPI)
  // kill MPI
  shutdown_mpi();
#endif  // MPI

  free_allocated_memory();

  return 0;
}
bool CommandAddRefs::run() {
    if (!m_input_files.empty()) {
        read_input_files();
    }

    bool todo = !m_relation_ids.empty();
    if (!m_relation_ids.empty()) {
        todo = find_relations_in_relations();
    }

    if (todo) {
        find_nodes_and_ways_in_relations();
    }

    if (!m_way_ids.empty()) {
        find_nodes_in_ways();
    }

    osmium::io::Reader reader(m_source_file, osmium::osm_entity_bits::object);
    osmium::io::Header header = reader.header();
    header.set("generator", m_generator);

    m_vout << "Opening output file...\n";
    osmium::io::Writer writer(m_output_file, header, m_output_overwrite, m_fsync);

    m_vout << "Copying from source to output file...\n";
    while (osmium::memory::Buffer buffer = reader.read()) {
        for (auto it = buffer.begin<osmium::OSMObject>(); it != buffer.end<osmium::OSMObject>(); ++it) {
            switch (it->type()) {
                case osmium::item_type::node:
                    if (m_node_ids.count(it->id())) {
                        m_node_ids.erase(it->id());
                        writer(*it);
                    }
                    break;
                case osmium::item_type::way:
                    if (m_way_ids.count(it->id())) {
                        m_way_ids.erase(it->id());
                        writer(*it);
                    }
                    break;
                case osmium::item_type::relation:
                    if (m_relation_ids.count(it->id())) {
                        m_relation_ids.erase(it->id());
                        writer(*it);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    writer.close();
    reader.close();

    print_missing_ids("node", m_node_ids);
    print_missing_ids("way", m_way_ids);
    print_missing_ids("relation", m_relation_ids);

    show_memory_used();
    m_vout << "Done.\n";

    return m_node_ids.empty() && m_way_ids.empty() && m_relation_ids.empty();
}