void RCGStrategy::on_parameters_changed_event(common::SignalArgs& args)
{
  common::XML::SignalOptions options(args);
  const common::URI parameters_uri = options.value<common::URI>("parameters_uri");

  if(boost::starts_with(parameters_uri.path(), uri().path()))
  {
    CFdebug << "Acting on trilinos_parameters_changed event from paramater list " << parameters_uri.string() << CFendl;
    m_implementation->reset_solver();
  }
  else
  {
    CFdebug << "Ignoring trilinos_parameters_changed event from paramater list " << parameters_uri.string() << CFendl;
  }
}
void TrilinosVector::read_native(const common::URI& filename, const string type)
{
  if(type == "rhs")
  {
    EpetraExt::readEpetraLinearSystem(filename.path(), m_comm, nullptr, &m_map, nullptr, &m_vec);
  }
  else if(type == "solution")
  {
    EpetraExt::readEpetraLinearSystem(filename.path(), m_comm, nullptr, &m_map, &m_vec);
  }
  else
  {
    throw common::SetupError(FromHere(), "Unknown type " + type + " for vector type when reading");
  }
  
  m_is_created = true;
}
void ReadRestartFile::execute()
{
  Handle<mesh::Mesh> mesh = options().value< Handle<mesh::Mesh> >("mesh");
  if(is_null(mesh))
    throw common::SetupError(FromHere(), "Option mesh is not configured");

  Handle<Time> time = options().value< Handle<Time> >(solver::Tags::time());
  if(is_null(time))
    throw common::SetupError(FromHere(), "Time component not configured");
  
  const common::URI filepath = options().value<common::URI>("file");
  boost::shared_ptr<common::XML::XmlNode> input_file = common::XML::parse_file(filepath);

  common::XML::XmlNode restart_node(input_file->content->first_node("restart"));
  if(!restart_node.is_valid())
    throw common::FileFormatError(FromHere(), "File  " + filepath.path() + " has no restart node");
  
  if(options().value<bool>("read_time_step"))
  {
    time->options().set("time_step", common::from_str<Real>(restart_node.attribute_value("time_step")));
    time->options().set("current_time", common::from_str<Real>(restart_node.attribute_value("current_time")));
    time->options().set("iteration", common::from_str<Uint>(restart_node.attribute_value("iteration")));
  }

  if(common::from_str<Uint>(restart_node.attribute_value("version")) != 1)
    throw common::FileFormatError(FromHere(), "File  " + filepath.path() + " has unsupported version");

  common::PE::Comm& comm = common::PE::Comm::instance();
  if(common::from_str<Uint>(restart_node.attribute_value("nb_procs")) != comm.size())
    throw common::SetupError(FromHere(), "File  " + filepath.path() + " was made for " + restart_node.attribute_value("nb_procs") + " CPUs, but we are loading on " + common::to_str(comm.size()) + " CPUs");

  boost::shared_ptr<common::BinaryDataReader> data_reader = common::allocate_component<common::BinaryDataReader>("DataReader");
  data_reader->options().set("file", common::URI(restart_node.attribute_value("binary_file")));

  common::XML::XmlNode field_node = restart_node.content->first_node("field");
  for(; field_node.is_valid(); field_node.content = field_node.content->next_sibling("field"))
  {
    Handle<mesh::Field> field(mesh->access_component(common::URI(field_node.attribute_value("path"), common::URI::Scheme::CPATH)));
    if(is_null(field))
      throw common::SetupError(FromHere(), "Field " + field_node.attribute_value("path") + " was not found in mesh " + mesh->uri().path());

    data_reader->read_table(*field, common::from_str<Uint>(field_node.attribute_value("index")));
  }
}
Exemple #4
0
void History::open_file(boost::filesystem::fstream& file, const common::URI& file_uri)
{
  boost::filesystem::path path (file_uri.path());
  file.open(path,std::ios_base::out);
  if (!file) // didn't open so throw exception
  {
    throw boost::filesystem::filesystem_error( path.string() + " failed to open",
                                               boost::system::error_code() );
  }
  file.precision(10);
}
Exemple #5
0
void Reader::do_read_mesh_into(const common::URI& path, Mesh& mesh)
{
  boost::shared_ptr<common::XML::XmlDoc> doc = common::XML::parse_file(path);
  common::XML::XmlNode mesh_node(doc->content->first_node("mesh"));

  common::PE::Comm& comm = common::PE::Comm::instance();
  
  if(!mesh_node.is_valid())
    throw common::FileFormatError(FromHere(), "File " + path.path() + " has no mesh node");
  
  if(mesh_node.attribute_value("version") != "1")
    throw common::FileFormatError(FromHere(), "File " + path.path() + " has incorrect version " + mesh_node.attribute_value("version") + "(expected 1)");

  if(common::from_str<Uint>(mesh_node.attribute_value("nb_procs")) != comm.size())
    throw common::FileFormatError(FromHere(), "File " + path.path() + " has was created for " + mesh_node.attribute_value("nb_procs") + " processes and can't load on " + common::to_str(comm.size()) + " processors");
  
  boost::shared_ptr<common::BinaryDataReader> data_reader = common::allocate_component<common::BinaryDataReader>("DataReader");
  data_reader->options().set("file", common::URI(mesh_node.attribute_value("binary_file")));
  
  common::XML::XmlNode topology_node = mesh_node.content->first_node("topology");
  if(!topology_node.is_valid())
    throw common::FileFormatError(FromHere(), "File " + path.path() + " does has no topology node");
  
  common::XML::XmlNode region_node(topology_node.content->first_node("region"));
  typedef std::map<Handle< common::List<Uint> >, std::string> PeriodicMapT;
  PeriodicMapT periodic_links_map; // Collect a map of the periodic links to create, if any
  for(; region_node.is_valid(); region_node.content = region_node.content->next_sibling("region"))
  {
    Region& region = mesh.topology().create_region(region_node.attribute_value("name"));
    common::XML::XmlNode elements_node(region_node.content->first_node("elements"));
    for(; elements_node.is_valid(); elements_node.content = elements_node.content->next_sibling("elements"))
    {
      Elements& elems = region.create_elements(elements_node.attribute_value("element_type"), mesh.geometry_fields());
      data_reader->read_list(elems.glb_idx(), common::from_str<Uint>(elements_node.attribute_value("global_indices")));
      data_reader->read_list(elems.rank(), common::from_str<Uint>(elements_node.attribute_value("ranks")));
      common::XML::XmlNode periodic_node(elements_node.content->first_node("periodic_links_elements"));
      if(periodic_node.is_valid())
      {
        Handle< common::List<Uint> > periodic_links_elements = elems.create_component< common::List<Uint> >("periodic_links_elements");
        data_reader->read_list(*periodic_links_elements, common::from_str<Uint>(periodic_node.attribute_value("index")));
        periodic_links_map.insert(std::make_pair(periodic_links_elements, periodic_node.attribute_value("periodic_link")));
      }
    }
  }

  // Link the periodic elements, now all elements have been created
  for(PeriodicMapT::const_iterator it = periodic_links_map.begin(); it != periodic_links_map.end(); ++it)
  {
    Handle<common::Link> link = it->first->create_component<common::Link>("periodic_link");
    Handle<Elements> elements_to_link(mesh.access_component(common::URI(it->second, common::URI::Scheme::CPATH)));
    if(is_null(elements_to_link))
      throw common::FileFormatError(FromHere(), "Invalid periodic link: " + it->second);

    link->link_to(*elements_to_link);
  }
  
  common::XML::XmlNode dictionaries_node(mesh_node.content->first_node("dictionaries"));
  if(!dictionaries_node.is_valid())
    throw common::FileFormatError(FromHere(), "File " + path.path() + " does has no dictionaries node");
  
  common::XML::XmlNode dictionary_node(dictionaries_node.content->first_node("dictionary"));
  for(; dictionary_node.is_valid(); dictionary_node.content = dictionary_node.content->next_sibling("dictionary"))
  {
    const std::string dict_name = dictionary_node.attribute_value("name");
    // Add the coordinates first
    if(dict_name == "geometry")
    {
      common::XML::XmlNode field_node(dictionary_node.content->first_node("field"));
      for(; field_node.is_valid(); field_node.content = field_node.content->next_sibling("field"))
      {
        if(field_node.attribute_value("name") == "coordinates")
        {
          const Uint table_idx = common::from_str<Uint>(field_node.attribute_value("table_idx"));
          mesh.initialize_nodes(data_reader->block_rows(table_idx), data_reader->block_cols(table_idx));
          data_reader->read_table(mesh.geometry_fields().coordinates(), table_idx);
        }
      }
      
      // Periodic links
      if(is_not_null(dictionary_node.content->first_attribute("periodic_links_nodes")) && is_not_null(dictionary_node.content->first_attribute("periodic_links_active")))
      {
        cf3_assert(is_null(mesh.geometry_fields().get_child("periodic_links_nodes")));
        cf3_assert(is_null(mesh.geometry_fields().get_child("periodic_links_active")));
        Handle< common::List<Uint> >  periodic_links_nodes = mesh.geometry_fields().create_component< common::List<Uint> >("periodic_links_nodes");
        Handle< common::List<bool> > periodic_links_active = mesh.geometry_fields().create_component< common::List<bool> >("periodic_links_active");
        data_reader->read_list(*periodic_links_nodes, common::from_str<Uint>(dictionary_node.attribute_value("periodic_links_nodes")));
        data_reader->read_list(*periodic_links_active, common::from_str<Uint>(dictionary_node.attribute_value("periodic_links_active")));
      }
    }
  }
  
  dictionary_node.content = dictionaries_node.content->first_node("dictionary");
  for(; dictionary_node.is_valid(); dictionary_node.content = dictionary_node.content->next_sibling("dictionary"))
  {
    const std::string dict_name = dictionary_node.attribute_value("name");
    const std::string space_lib_name = dictionary_node.attribute_value("space_lib_name");
    const bool continuous = common::from_str<bool>(dictionary_node.attribute_value("continuous"));
    
    // The entities used by this dictionary
    std::vector< Handle<Entities> > entities_list;
    std::vector<Uint> entities_binary_file_indices;
    common::XML::XmlNode entities_node = dictionary_node.content->first_node("entities");
    for(; entities_node.is_valid(); entities_node.content = entities_node.content->next_sibling("entities"))
    {
      Handle<Entities> entities(mesh.access_component(common::URI(entities_node.attribute_value("path"), common::URI::Scheme::CPATH)));
      if(is_null(entities))
        throw common::FileFormatError(FromHere(), "Referred entities " + entities_node.attribute_value("path") + " doesn't exist in mesh");
      entities_list.push_back(entities);
      entities_binary_file_indices.push_back(common::from_str<Uint>(entities_node.attribute_value("table_idx")));
    }
    
    Dictionary& dictionary = dict_name == "geometry" ? mesh.geometry_fields() :
      (continuous ? mesh.create_continuous_space(dict_name, space_lib_name, entities_list) : mesh.create_discontinuous_space(dict_name, space_lib_name, entities_list));
      
    // Read the global indices
    data_reader->read_list(dictionary.glb_idx(), common::from_str<Uint>(dictionary_node.attribute_value("global_indices")));
    data_reader->read_list(dictionary.rank(), common::from_str<Uint>(dictionary_node.attribute_value("ranks")));
    
    // Read the fields
    common::XML::XmlNode field_node(dictionary_node.content->first_node("field"));
    for(; field_node.is_valid(); field_node.content = field_node.content->next_sibling("field"))
    {
      if(field_node.attribute_value("name") == "coordinates")
        continue;
      const Uint table_idx = common::from_str<Uint>(field_node.attribute_value("table_idx"));
      Field& field = dictionary.create_field(field_node.attribute_value("name"), field_node.attribute_value("description"));
      common::XML::XmlNode tag_node = field_node.content->first_node("tag");
      for(; tag_node.is_valid(); tag_node.content = tag_node.content->next_sibling("tag"))
        field.add_tag(tag_node.attribute_value("name"));
      
      data_reader->read_table(field, table_idx);
    }
    
    // Read in the connectivity tables
    for(Uint i = 0; i != entities_list.size(); ++i)
    {
      data_reader->read_table(entities_list[i]->space(dictionary).connectivity(), entities_binary_file_indices[i]);
    }
  }
}
void TurbulenceStatistics::setup()
{
  if(!m_options_changed)
    return;
  m_options_changed = false;

  m_used_nodes.reset();

  Handle<mesh::Region> region = options().value< Handle<mesh::Region> >("region");
  if(is_null(region))
  {
    return;
  }

  const std::string velocity_var_name = options().value<std::string>("velocity_variable_name");
  const std::string pressure_var_name = options().value<std::string>("pressure_variable_name");
  m_velocity_field.reset();
  m_pressure_field.reset();
  
  const mesh::Mesh& mesh = common::find_parent_component<mesh::Mesh>(*region);
  Handle<mesh::Dictionary> dictionary;
  BOOST_FOREACH(const Handle<mesh::Dictionary>& dict, mesh.dictionaries())
  {
    BOOST_FOREACH(const Handle<mesh::Field>& field, dict->fields())
    {
      if(field->has_variable( velocity_var_name ))
      {
        if(is_not_null(m_velocity_field))
          throw common::SetupError(FromHere(), "There are two fields that contain the variable " + velocity_var_name );
        
        m_velocity_field = field;
        dictionary = dict;
      }
    }
  }
  
  if(is_null(m_velocity_field))
    throw common::SetupError(FromHere(), "There is no field with the variable " + velocity_var_name );
  
  m_dim = mesh.dimension();
  if(m_dim == 1)
  {
    throw common::SetupError(FromHere(), "TurbulenceStatistics are not supported in 1D");
  }
  cf3_assert(m_velocity_field->var_length(velocity_var_name) == m_dim);
  m_velocity_field_offset = m_velocity_field->var_offset( velocity_var_name );
  
  BOOST_FOREACH(const Handle<mesh::Field>& field, dictionary->fields())
  {
    if(field->has_variable( pressure_var_name ))
    { 
      m_pressure_field = field;
    }
  }
  
  if(is_null(m_pressure_field))
    throw common::SetupError(FromHere(), "There is no field with the variable " + pressure_var_name );
  
  m_pressure_field_offset = m_pressure_field->var_offset( pressure_var_name );

  const mesh::Field& coords = dictionary->coordinates();
  
  common::PE::Comm& comm = common::PE::Comm::instance();

  m_used_nodes = mesh::build_used_nodes_list(*region, *dictionary, true, false);
  const int nb_probes = m_probe_locations.size();
  std::vector<int> my_probes_found(nb_probes, 0);
  m_probe_nodes.clear();
  m_probe_indices.clear();
  const common::List<Uint>::ListT& used_nodes_list = m_used_nodes->array();
  BOOST_FOREACH(const Uint node_idx, used_nodes_list)
  {
    if(dictionary->is_ghost(node_idx))
      continue;
    for(int probe_idx = 0; probe_idx != nb_probes; ++probe_idx)
    {
      if(m_probe_locations[probe_idx].size() != m_dim)
      {
        throw common::SetupError(FromHere(), "Probe coordinates of dimension " + common::to_str(m_probe_locations[probe_idx].size()) + " do not match dimension " + common::to_str(m_dim));
      }
      if(((RealVector::Map(&coords[node_idx][0], m_dim) - m_probe_locations[probe_idx]).array().abs() < 1e-10).all())
      {
        m_probe_nodes.push_back(node_idx);
        m_probe_indices.push_back(probe_idx);
        my_probes_found[probe_idx] = 1;
      }
    }
  }

  std::vector<int> global_probes_found(nb_probes);
  if(comm.is_active() && nb_probes > 0)
  {
    comm.all_reduce(common::PE::plus(), my_probes_found, global_probes_found);
  }
  else
  {
    global_probes_found = my_probes_found;
  }
  for(int probe_idx = 0; probe_idx != nb_probes; ++probe_idx)
  {
    if(global_probes_found[probe_idx] == 0)
      throw common::SetupError(FromHere(), "Probe " + common::to_str(probe_idx) + " has no matching node");
    if(global_probes_found[probe_idx] > 1)
      throw common::SetupError(FromHere(), "Probe " + common::to_str(probe_idx) + " was found on " + common::to_str(global_probes_found[probe_idx]) + " CPUs");
  }

  const common::URI original_uri = options().value<common::URI>("file");

  // Init probe files
  m_probe_files.clear();
  const Uint nb_my_probes = m_probe_nodes.size();
  for(Uint my_idx = 0; my_idx != nb_my_probes; ++my_idx)
  {
    const Uint probe_idx = m_probe_indices[my_idx];
    std::string probe_path = (original_uri.base_path() / (original_uri.base_name() + "-probe-" + common::to_str(probe_idx) + original_uri.extension())).path();

    m_probe_files.push_back(boost::make_shared<boost::filesystem::fstream>(probe_path, std::ios_base::out));
    boost::filesystem::fstream& file = *m_probe_files.back();
    if(!file)
      throw common::FileSystemError(FromHere(), "Failed to open file " + probe_path);

    file << "# Probe data for probe " << probe_idx << " at point " << m_probe_locations[probe_idx].transpose() << "\n";
    if(m_dim == 2)
    {
      file << "# U, V, uu, vv, uv, U_rolling, V_rolling, uu_rolling, vv_rolling, uv_rolling\n";
    }
    else if(m_dim == 3)
    {
      file << "# U, V, W, uu, vv, ww, uv, uw, vw, U_rolling, V_rolling, W_rolling, uu_rolling, vv_rolling, ww_rolling, uv_rolling, uw_rolling, vw_rolling\n";
    }
  }

  // Create a field for the statistics data
  m_statistics_field = Handle<mesh::Field>(dictionary->get_child("turbulence_statistics"));
  if(is_null(m_statistics_field))
  {
    if(m_dim == 2)
    {
      m_statistics_field = dictionary->create_field("turbulence_statistics", "V[vector],uu,vv,uv,p").handle<mesh::Field>();
    }
    else if(m_dim == 3)
    {
      m_statistics_field = dictionary->create_field("turbulence_statistics", "V[vector],uu,vv,ww,uv,uw,vw,p").handle<mesh::Field>();
    }
    m_statistics_field->add_tag("turbulence_statistics");
  }

  // Reset statistics without changing m_count
  const Uint nb_accs = (2.*m_dim + m_dim-1 + m_dim-2)*m_probe_nodes.size();
  m_means.assign(nb_accs, MeanAccT());
  m_rolling_means.assign(nb_accs, RollingAccT(boost::accumulators::tag::rolling_window::window_size = options().value<Uint>("rolling_window")));
}