Esempio n. 1
0
int Cosisim::execute( GsTL_project* ) {
  // Initialize the global random number generator
  Global_random_number_generator::instance()->seed( seed_ );

  // Set up a progress notifier	
  int total_steps = simul_grid_->size() * (nb_of_realizations_);
  int frequency = std::max( total_steps / 20, 1 );
  SmartPtr<Progress_notifier> progress_notifier = 
    utils::create_notifier( "Running CoSisim", 
			    total_steps, frequency );

  Grid_path path(simul_grid_,simul_grid_->selected_property(),target_grid_region_);

  // loop on all realizations
  for( int nreal = 0; nreal < nb_of_realizations_ ; nreal ++ ) {

    // update the progress notifier
    progress_notifier->message() << "working on realization " 
                                 << nreal+1 << gstlIO::end;
    if( !progress_notifier->notify() ) return 1;


    // Create a new property to hold the realization and tell the simulation 
    // grid to use it as the current property 
    appli_message( "Creating new realization" );
    Grid_continuous_property* prop = multireal_property_->new_realization();
    prop->set_parameters(parameters_);
    simul_grid_->select_property( prop->name() );

    path.set_property( prop->name() );
    path.randomize();

    int status = 0;
    if( do_median_ik_ )
      status = median_ik( path.begin(), path.end(), progress_notifier.raw_ptr() );
    else
      status = full_ik(  path.begin(), path.end(), progress_notifier.raw_ptr() );

    reset_indicator_properties();

    // check if the simulation was aborted
    if( status == 1 ) {
      clean( prop );
      return 1;
    }

  } // end loop on realizations

  remove_temporary_properties();
  return 0;
}
bool Unary_property_item_action::execute(GsTL_object_item* item) {

    Error_messages_handler error_messages;
    Grid_continuous_property* prop = dynamic_cast<Grid_continuous_property*>(item);
    if(prop == 0) { //May be inside a region
        GsTL_object_property_item* prop_in_group = dynamic_cast<GsTL_object_property_item*>(item);
        if(prop_in_group)
            prop = prop_in_group->property();
    }

    if(prop==0) {
        GsTLcerr << "Command " << unary_action_name_.toStdString() << " could not be performed; a property was not correctly selected";
        GsTLcerr << gstlIO::end;
        return false;
    }

    std::string grid_name = item->parent()->parent()->item_data(0).toString().toStdString();  //prop->group->grid
    std::string params = grid_name+"::"+prop->name();
    bool ok = project_->execute(unary_action_name_.toStdString(), params , &error_messages);

    if (!ok)
    {
        GsTLcerr << "Command " << unary_action_name_.toStdString() << " could not be performed: \n";
        if (!error_messages.empty())
            GsTLcerr << error_messages.errors() << "\n";
        GsTLcerr << gstlIO::end;
    }
    return ok;
}
bool Rename_property_item_action::execute(GsTL_object_item* item) {

    Grid_continuous_property* prop = dynamic_cast<Grid_continuous_property*>(item);
    if(prop == 0) return false;

    std::string old_name = prop->name();
    Rename_dialog rename_diag("Property",old_name.c_str(), dynamic_cast<QWidget*>(this->parent()));
    int r = rename_diag.exec();
    if (r==1) {
        Geostat_grid* grid = dynamic_cast<Geostat_grid*>(item->parent()->parent());
        if(grid == 0) return false;
        return grid->reNameProperty(old_name, rename_diag.new_name().toStdString() );
    }
    else
        return false;

}
Esempio n. 4
0
int LU_sim::execute( GsTL_project* ) {
  
  // Initialize the global random number generator
  Global_random_number_generator::instance()->seed( seed_ );
  
  
  // Set up a progress notifier	
  int total_steps = simul_grid_->size() * (nb_of_realizations_);
  int frequency = std::max( total_steps / 20, 1 );
  SmartPtr<Progress_notifier> progress_notifier = 
    utils::create_notifier( "Running LU_sim", 
			    total_steps, frequency );

  // In LU simulation, the marginal is a Gaussian cdf, 
  // with mean 0 and variance 1.
  Gaussian_cdf marginal( 0.0, 1.0 );

  //typedef Geostat_grid::random_path_iterator iterator;
  typedef Gval_iterator< TabularMapIndex > iterator;

  std::vector< int > id_unknown;
  std::vector< int > id_data;

  //harddata_grid_->select_property(harddata_property_->name());
  if( harddata_grid_ ) {
    for( int i=0; i< harddata_property_->size(); i++ ) {
      if(!harddata_property_->is_informed(i) ) continue;
      id_data.push_back( i );
    }
  }
  for( int i=0; i< simul_grid_->size(); i++ ) {
    if( simul_grid_ == harddata_grid_ && harddata_property_->is_informed(i) ) continue;
    id_unknown.push_back( i );
  }

  iterator begin_d( harddata_grid_, harddata_property_ ,0,  
			      id_data.size(), TabularMapIndex(&id_data) );
  iterator end_d( harddata_grid_, harddata_property_ ,id_data.size(),  
			      id_data.size(), TabularMapIndex(&id_data) );

  Grid_continuous_property* prop = multireal_property_->new_realization();
  
  iterator begin_u( simul_grid_, prop ,0,  
			      id_unknown.size(), TabularMapIndex(&id_unknown) );
  iterator end_u( simul_grid_, prop ,id_unknown.size(),  
			      id_unknown.size(), TabularMapIndex(&id_unknown) );

  Random_number_generator gen;
  LU_simulator<
        Covariance<Location>,
        Random_number_generator,
        Geostat_grid> 
    lu_sim( covar_,gen, simul_grid_ );
  lu_sim.initialize_matrix(begin_u, end_u, begin_d, end_d);

  // loop on all realizations
  for( int nreal = 0; nreal < nb_of_realizations_ ; nreal ++ ) {

    // update the progress notifier
    progress_notifier->message() << "working on realization " 
                                 << nreal+1 << gstlIO::end;
    if( !progress_notifier->notify() ) return 1;


    // Create a new property to hold the realization and tell the simulation 
    // grid to use it as the current property 
    appli_message( "Creating new realization" );
    //Grid_continuous_property* prop = multireal_property_->new_realization();
    if(nreal>0) prop = multireal_property_->new_realization();
    prop->set_parameters(parameters_);
    simul_grid_->select_property( prop->name() );


    // initialize the new realization with the hard data, if that was requested 
    if( property_copier_ ) {
      property_copier_->copy( harddata_grid_, harddata_property_,
                              simul_grid_, prop );
    }


    iterator begin_unk( simul_grid_, prop ,0,  
			   id_unknown.size(), TabularMapIndex(&id_unknown) );
    iterator  end_unk( simul_grid_, prop ,id_unknown.size(),  
			   id_unknown.size(), TabularMapIndex(&id_unknown) );

    int status = lu_sim(begin_unk,end_unk,marginal);

    // initialize the new realization with the hard data, if that was requested 
  //  if( property_copier_ ) {
  //    property_copier_->copy( harddata_grid_, harddata_property_,
  //                            simul_grid_, prop );
      //initializer_->assign( prop, harddata_grid_, harddata_property_->name() );
  //  }


    if( status == -1 ) {
      clean( prop );
      return 1;
    }
    // back-transform if needed
    if( use_target_hist_ ) {
      cdf_transform( prop->begin(), prop->end(), 
        marginal, *target_cdf_.raw_ptr() );
    }

  }

  clean();

  return 0;
}
/* Convert_continuous_to_categorical_property grid_name::prop1::[prop2::]
* will copy convert the property from continuous to categorical
*/
bool Convert_continuous_to_categorical_property::
init( std::string& parameters, GsTL_project* proj,
      Error_messages_handler* errors ) {

  std::vector< std::string > params =
    String_Op::decompose_string( parameters, Actions::separator,
                      				   Actions::unique );

  if( params.size() <2 ) {
    errors->report( "some parameters are missing, Needs at least two parameters (grid + property)" );
    return false;
  }

  SmartPtr<Named_interface> grid_ni =
    Root::instance()->interface( gridModels_manager + "/" + params[0] );
  Geostat_grid* grid = dynamic_cast<Geostat_grid*>( grid_ni.raw_ptr() );
  if( !grid ) {
    std::ostringstream message;
    message << "No grid called \"" << params[0] << "\" was found";
    errors->report( message.str() );
    return false;
  }

  for(int i=1; i<params.size(); ++i) {
    Grid_categorical_property* cprop = grid->categorical_property(params[i]);
    if(cprop) continue;
    Grid_continuous_property* prop = grid->property(params[i]);
    if(prop == 0) continue;
    std::set<int> cat_codes;
    std::string prop_name = prop->name()+" - categorical";
    cprop = grid->add_categorical_property(prop_name);
    while(!cprop) {
      prop_name.append("_0");
      cprop = grid->add_categorical_property(prop_name);
    }
    for(int nodeid=0; nodeid < prop->size(); ++nodeid) {
      if( prop->is_informed(nodeid)) {
        int code = static_cast<int>(prop->get_value(nodeid));
        cprop->set_value(code,nodeid);
        cat_codes.insert(code);
      }
    }
    // Check is sequential coding
    bool is_sequential_coding = false;
    std::set<int>::iterator it = cat_codes.begin();
    if( *it == 0  ) {
      std::advance(it, cat_codes.size()-1);
      if( *it == cat_codes.size()-1 ) {
        is_sequential_coding = true;
      }
      
    }

    if( !is_sequential_coding  ) {  // Need to build a categorical definition
      CategoricalPropertyDefinitionName* cat_def = 0;
      std::string catdef_name = grid->name()+"-"+prop->name();
      while(!cat_def) {
        SmartPtr<Named_interface> ni = Root::instance()->new_interface( "categoricaldefinition://"+catdef_name,categoricalDefinition_manager +"/"+catdef_name );
        cat_def = dynamic_cast<CategoricalPropertyDefinitionName*>(ni.raw_ptr());
          if(!cat_def) catdef_name.append("_0");
      }

      std::set<int>::iterator it_cat_codes = cat_codes.begin();
      for(; it_cat_codes != cat_codes.end(); ++it_cat_codes) {
        cat_def->add_category(*it_cat_codes, QString("Code %1").arg(*it_cat_codes).toStdString() );
      }
      cprop->set_category_definition( cat_def );
    }

  }

  proj->update( params[0] );
  return true;
}
Esempio n. 6
0
int dssim::execute( GsTL_project* ) {
  
  // Initialize the global random number generator
  Global_random_number_generator::instance()->seed( seed_ );
  
  
  // Set up a progress notifier	
  int total_steps = simul_grid_->size() * (nb_of_realizations_);
  int frequency = std::max( total_steps / 20, 1 );
  SmartPtr<Progress_notifier> progress_notifier = 
    utils::create_notifier( "Running dssim", 
			    total_steps, frequency );

  // Initialize the marginal cdf for the direct sequential simulation
 // LogNormal_cdf marginal( 0.1, 1.0 );
 // LogNormal_cdf ccdf;


  // set up the cdf-estimator
  typedef First2_moments_cdf_Kestimator< Covariance<Location>,
                                   Neighborhood,
                                   geostat_utils::KrigingConstraints
                                  >    Kriging_cdf_estimator;
  Kriging_cdf_estimator cdf_estimator( covar_,
				       *Kconstraints_,
				       *combiner_ );

  // set up the sampler
  Random_number_generator gen;
  Monte_carlo_sampler_t< Random_number_generator > sampler( gen );
  
  // compute the random path
  Grid_path path(simul_grid_, simul_grid_->selected_property(), target_grid_region_);
  //simul_grid_->init_random_path();

  // loop on all realizations
  for( int nreal = 0; nreal < nb_of_realizations_ ; nreal ++ ) {

    // update the progress notifier
    progress_notifier->message() << "working on realization " 
                                 << nreal+1 << gstlIO::end;
    if( !progress_notifier->notify() ) return 1;


    // Create a new property to hold the realization and tell the simulation 
    // grid to use it as the current property 
    appli_message( "Creating new realization" );
    Grid_continuous_property* prop = multireal_property_->new_realization();
    prop->set_parameters(parameters_);
    simul_grid_->select_property( prop->name() );
    neighborhood_->select_property( prop->name() );

    // initialize the new realization with the hard data, if that was requested 
    if( property_copier_ ) {
      property_copier_->copy( harddata_grid_, harddata_property_,
                              simul_grid_, prop );
      //initializer_->assign( prop, harddata_grid_, harddata_property_->name() );
    }

    path.randomize();
    path.set_property(prop->name());

    appli_message( "Doing simulation" );
    // do the simulation
    int status = 
      sequential_simulation( path.begin(),
			     path.end(),
			     *(neighborhood_.raw_ptr()),
			     *ccdf_,
			     cdf_estimator,
			     *marginal_,
			     sampler, progress_notifier.raw_ptr()
			     );
    if( status == -1 ) {
      clean( prop );
      return 1;
    }
  }

  clean();

  return 0;
}