Beispiel #1
0
bool Cosisim::initialize( const Parameters_handler* parameters, 
                          Error_messages_handler* errors ) {

  OPEN_DEBUG_STREAM( "cosisim.dbg" );

  // Extract the parameters input by the user from the parameter handler
  
  //-------------
  // The "simulation" grid parameters
  
  std::string simul_grid_name = parameters->value( "Grid_Name.value" );
  errors->report( simul_grid_name.empty(), 
            		  "Grid_Name", "No grid selected" );
  std::string property_name = parameters->value( "Property_Name.value" );
  errors->report( property_name.empty(), 
            		  "Property_Name", "No property name specified" );

  // Get the simulation grid from the grid manager  
  if( simul_grid_name.empty() ) return false;

  bool ok = geostat_utils::create( simul_grid_, simul_grid_name,
                      				 "Grid_Name", errors );
  if( !ok ) return false;

  // create  a multi-realization property
  multireal_property_ =
    simul_grid_->add_multi_realization_property( property_name );



  int nb_indicators =
    String_Op::to_number<int>( parameters->value( "Nb_Indicators.value" ) );

    
  //-------------
  // The cdf parameters (# of thresholds, marginal, ...)

  bool categorical = 
    String_Op::to_number<bool>( 
		    parameters->value( "Categorical_Variable_Flag.value" )
		  );
  
  std::string marginal_probabilities_string = 
    parameters->value( "Marginal_Probabilities.value" );
  std::vector<double> marginal_probs = 
    String_Op::to_numbers<double>( marginal_probabilities_string );

    errors->report( marginal_probs.size() != nb_indicators,
                    "Marginal_Probabilities", 
                    "Enter one probability value per indicator" );

  if( categorical ) {
    // we ignore the input threshold values. The categories are numbered
    // from 0 to k (if there are k+1 categories).
    ccdf_ = new Categ_non_param_cdf<float>( marginal_probs.size() );
    marginal_ = new Categ_non_param_cdf<float>( marginal_probs.size(),
						marginal_probs.begin() );
    errors->report( !is_valid_cdf( marginal_probs.begin(), marginal_probs.end(),
                                   GsTL::discrete_variable_tag() ),
                    "Marginal_Probabilities", 
                    "Values must be between 0 and 1 and sum up to 1" );
  }
  else {

    std::vector<float> thresholds = 
      String_Op::to_numbers<float>( parameters->value( "Thresholds.value" ) );
    errors->report( !GsTL::is_sorted( thresholds.begin(), thresholds.end() ),
                    "Thresholds", 
                    "Threshold values must be sorted in ascending order" );
    
    ccdf_ = new Non_param_cdf<>( thresholds.begin(), thresholds.end() );
    marginal_ = new Non_param_cdf<>( thresholds.begin(), 
				     thresholds.end(),
				     marginal_probs.begin() );

    errors->report( !is_valid_cdf( marginal_probs.begin(), marginal_probs.end(),
                                   GsTL::continuous_variable_tag() ),
                    "Marginal_Probabilities", 
                    "The values entered do not define a valid cdf" );

    // Set up the extrapolation tails
    geostat_utils::set_cdf_extrapolation_tail(parameters,errors, 
     *((Non_param_cdf<>*) ccdf_), "lowerTailCdf", "upperTailCdf");
    geostat_utils::set_cdf_extrapolation_tail(parameters,errors, 
     *((Non_param_cdf<>*) marginal_), "lowerTailCdf", "upperTailCdf");
  }

  

  //-------------
  // The hard data parameters

  std::string prim_harddata_grid_name = 
    parameters->value( "Primary_Harddata_Grid.value" );

  Geostat_grid* prim_harddata_grid = 0;
  Geostat_grid* sec_harddata_grid = 0;

  if( !prim_harddata_grid_name.empty() ) {
    // Get the harddata grid from the grid manager
    bool ok = geostat_utils::create( prim_harddata_grid, prim_harddata_grid_name, 
      	                           	 "Primary_Harddata_Grid", errors );
    if( !ok ) return false;
    
    if( !dynamic_cast<Point_set*>(prim_harddata_grid) && 
        prim_harddata_grid != simul_grid_ ) {
      std::ostringstream error_message;
      error_message << "the grid should either be the same as the simulation grid "
                    << "or be a set of points";
      errors->report( std::string( "Primary_Harddata_Grid" ), 
                      error_message.str() );
    }

    // get the properties
    std::string primary_indicators_str =
      parameters->value( "Primary_Indicators.value" );
    std::vector<std::string> primary_indicators_names = 
      String_Op::decompose_string( primary_indicators_str, ";", false );

    for( unsigned int i= 0; i < primary_indicators_names.size() ; i++ ) {
      primary_indicators_.push_back( 
          prim_harddata_grid->property( primary_indicators_names[i] )
        );
    }

    if( nb_indicators != primary_indicators_names.size() ) {
      std::ostringstream message;
      message << "Specify exactly " << nb_indicators << " properties";
      errors->report( std::string("Primary_Indicators"), message.str() );
    }
  }


  std::string sec_harddata_grid_name = 
    parameters->value( "Secondary_Harddata_Grid.value" );
  if( !sec_harddata_grid_name.empty() ) {

    // Get the harddata grid from the grid manager
    bool ok2 = geostat_utils::create( sec_harddata_grid, sec_harddata_grid_name, 
  	                           	     "Secondary_Harddata_Grid", errors );
    if( !ok2 ) return false;

    if( !dynamic_cast<Point_set*>( sec_harddata_grid ) && 
        sec_harddata_grid != simul_grid_ ) {
      std::ostringstream error_message;
      error_message << "the grid should either be the same as the simulation grid "
                    << "or be a set of points";
      errors->report( std::string( "Secondary_Harddata_Grid" ),
                      error_message.str() );
    }

    
    std::string secondary_indicators_str =
      parameters->value( "Secondary_Indicators.value" );
    errors->report( secondary_indicators_str.empty(), 
                    "Secondary_Indicators", "No property names specified" );

    std::vector<std::string> secondary_indicators_names = 
      String_Op::decompose_string( secondary_indicators_str, ";", false );

    for( unsigned int i= 0; i < secondary_indicators_names.size() ; i++ ) {
      secondary_indicators_.push_back( 
          sec_harddata_grid->property( secondary_indicators_names[i] )
        );
    }

    if( nb_indicators != secondary_indicators_names.size() ) {
      std::ostringstream message;
      message << "Specify exactly " << nb_indicators << " properties";
      errors->report( std::string("Secondary_Indicators"), message.str() );
    }
  }


  // check if errors were reported so far
  if( !errors->empty() ) return false;


  //--------------------
  // Create K new properties on the simulation grid. Those are the properties
  // the simulation algorithm will be working on for each realization
  for( unsigned int j = 0 ; j < nb_indicators ; j++ ) {
    std::ostringstream prop_name;
    prop_name << "__tmp_indicator_" << j;
    GsTLGridProperty* prop = 
      geostat_utils::add_property_to_grid( simul_grid_, prop_name.str() );
    indicators_.push_back( prop );
  }

  //--------------------
  // Currently, we always assign the hard data to the simulation grid
  if( prim_harddata_grid ) {
    property_copier_ = 
      Property_copier_factory::get_copier( prim_harddata_grid, simul_grid_ );

    if( !property_copier_ ) {
      std::ostringstream message;
      message << "It is currently not possible to copy a property from a "
              << prim_harddata_grid->classname() << " to a " 
              << simul_grid_->classname() ;
      errors->report( !property_copier_, "Assign_Hard_Data", message.str() );
      return false;
    }
    
    //initializer_ = new Grid_initializer( simul_grid_, false );
    for( unsigned int i = 0 ; i < primary_indicators_.size() ; i++ ) {
      property_copier_->copy( prim_harddata_grid, primary_indicators_[i],
                              simul_grid_, indicators_[i] );
    }
  }


  //-------------
  // Number of realizations and random number seed
  nb_of_realizations_ = 
    String_Op::to_number<int>( parameters->value( "Nb_Realizations.value" ) );
  
  seed_ = String_Op::to_number<int>( parameters->value( "Seed.value" ) );




  //-------------
  // Set-up the indicator coding function

  if( categorical ) 
    indicator_coder_ = Indicator<float>( new Class_indicator_function<float> );
  else 
    indicator_coder_ = Indicator<float>( new Indicator_function<float> );

  

  //-------------
  // The Ik parameter (median or full)
  do_median_ik_ = 
    String_Op::to_number<bool>( parameters->value( "Median_Ik_Flag.value" ) );
 


  //-------------
  // The search neighborhood parameters

  int prim_max_neigh = 
    String_Op::to_number<int>( 
      parameters->value( "Max_Conditioning_Data_Primary.value" )
    );
  
  GsTLTriplet prim_ellips_ranges;
  GsTLTriplet prim_ellips_angles;
  bool prim_extract_ok =
    geostat_utils::extract_ellipsoid_definition( prim_ellips_ranges, 
                                                 prim_ellips_angles,
		  			                                     "Search_Ellipsoid_Primary.value",
			  		                                     parameters, errors );
  if( !prim_extract_ok ) return false;


  GsTLTriplet sec_ellips_ranges;
  GsTLTriplet sec_ellips_angles;
  int sec_max_neigh = 0;
  if( sec_harddata_grid ) {
    sec_max_neigh = 
      String_Op::to_number<int>( 
        parameters->value( "Max_Conditioning_Data_Secondary.value" ) 
      );
  
    bool sec_extract_ok = 
      geostat_utils::extract_ellipsoid_definition( sec_ellips_ranges, 
                                                   sec_ellips_angles,
		  	  		                                     "Search_Ellipsoid_Secondary.value",
			  	  	                                     parameters, errors );
  
    if( !sec_extract_ok ) return false;
  }


  //-------------
  // Variogram (covariance) initialization 
  // We distinguish two cases: 
  //   - median ik: there is only one covariance-set 
  //   - full ik  : there are as many covariances as indicators

  std::vector< CovarianceType > covar_vector;
  std::vector<double> Bz_values;
  if( sec_harddata_grid ) {
    std::string Bz_string = parameters->value( "Bz_Values.value" );
    Bz_values = String_Op::to_numbers<double>( Bz_string );
  }
  else
    Bz_values.assign( nb_indicators, 0.0 );

  
  if( do_median_ik_ ) {
    CovarianceType cov;
    bool init_cov_ok =
      geostat_utils::initialize_covariance( &cov, "Variogram_Median_Ik", 
	  		                                    parameters, errors );
    if( !init_cov_ok ) return false;

    covar_vector.assign( nb_indicators, cov );
    errors->report( Bz_values.empty(), "Bz_Values", "No values for Bz" );
    covariances_.push_back( MarkovBayesCovariance(covar_vector[0], Bz_values[0]) );

  }
  else {
    // We are doing full ik

    covar_vector.resize( nb_indicators );
    if( Bz_values.size() != nb_indicators ) {
      errors->report( "Bz_Values", "Enter one Bz value per indicator" );
      return false;
    }

    // initialize all the covariances
    bool init_cov_ok =
      geostat_utils::initialize_covariance( &covar_vector[0], "Variogram_Full_Ik", 
	  	                      	              parameters, errors );
    if( !init_cov_ok ) return false;
    covariances_.push_back( MarkovBayesCovariance(covar_vector[0], Bz_values[0]) );


    for( int i=1; i < nb_indicators ; i++ ) {
      std::ostringstream tagname;
      tagname << "Variogram_Full_Ik_"  << i+1 ;
      init_cov_ok =
        geostat_utils::initialize_covariance( &covar_vector[i], tagname.str(), 
                        			                parameters, errors );
      if( !init_cov_ok ) return false;
      covariances_.push_back( MarkovBayesCovariance( covar_vector[i], 
                                                     Bz_values[i] )   );
    }
  }

    Search_filter* prim_filter = geostat_utils::
      set_advanced_search("AdvancedSearch_Primary", parameters, errors);
    Search_filter* soft_filter = NULL;
    if(sec_harddata_grid) 
     soft_filter = geostat_utils::
      set_advanced_search("AdvancedSearch_Secondary", parameters, errors);

 
  //--------------------
  // neighborhoods initialization
  init_neighborhoods( simul_grid_, sec_harddata_grid,
                      prim_ellips_ranges, prim_ellips_angles,
                      sec_ellips_ranges, sec_ellips_angles,
                      prim_max_neigh, sec_max_neigh,
                      covar_vector, prim_filter, soft_filter );

  delete prim_filter;
  delete soft_filter;

  //---------------------
  // Kriging constraints and kriging combiner
  geostat_utils::Kriging_type ktype = 
    geostat_utils::kriging_type( "Kriging_Type.value", parameters, errors );  
  
  if( ktype == geostat_utils::SK ) {
    for( int i = 0; i < nb_indicators ; i++ ) {
      typedef geostat_utils::WeightIterator Iter1;
      typedef geostat_utils::NeighIterator  Iter2;
      typedef CoSK_combiner<Iter1,Iter2> CoSKCombiner;

      std::vector<double> means;
      // use the same mean for the primary and the secondary data: they are both
      // P(I(u)=1)
      means.assign( 2, *(marginal_->p_begin() + i) );
      combiners_.push_back( 
        geostat_utils::CoKrigingCombiner( new CoSKCombiner( means ) )
       );
    }

    Co_SKConstraints_impl< geostat_utils::NeighIterator, 
                           geostat_utils::Location > sk_constr;
    kconstraints_ = new geostat_utils::CoKrigingConstraints( &sk_constr );
  }
  else {
    combiners_.resize( nb_indicators );
    kconstraints_ = new geostat_utils::CoKrigingConstraints;
  }

  std::string region_name = parameters->value( "Grid_Name.region" );
  if (!region_name.empty() && simul_grid_->region( region_name ) == NULL ) {
    errors->report("Grid_Name","Region "+region_name+" does not exist");
  }
  else grid_region_.set_temporary_region( region_name, simul_grid_);

  if( simul_grid_ != sec_harddata_grid ) {
    std::string region_name = parameters->value( "Secondary_Harddata_Grid.region" );
    if (!region_name.empty() && sec_harddata_grid->region( region_name ) == NULL ) {
      errors->report("Secondary_Harddata_Grid","Region "+region_name+" does not exist");
    }
    else soft_grid_region_.set_temporary_region( region_name, sec_harddata_grid);
  }


  //-------------------------------
  // Done!

  if( !errors->empty() )
    return false;

  return true;
}
bool LU_sim::initialize( const Parameters_handler* parameters,
			Error_messages_handler* errors ) {

  OPEN_DEBUG_STREAM( "LU_sim.dbg" );

  // Extract the parameters input by the user from the parameter handler
  
  //-------------
  // The "simulation" grid parameters
  
  std::string simul_grid_name = parameters->value( "Grid_Name.value" );
  errors->report( simul_grid_name.empty(), 
            		  "Grid_Name", "No grid selected" );
  std::string property_name = parameters->value( "Property_Name.value" );
  errors->report( property_name.empty(), 
            		  "Property_Name", "No property name specified" );

  // Get the simulation grid from the grid manager  
  if( simul_grid_name.empty() ) return false;

  bool ok = geostat_utils::create( simul_grid_, simul_grid_name,
                          				 "Grid_Name", errors );
  if( !ok ) return false;

  // create  a multi-realization property
  multireal_property_ =
    simul_grid_->add_multi_realization_property( property_name );



  //-------------
  // The hard data parameters

  std::string harddata_grid_name = parameters->value( "Hard_Data.grid" );
 
  if( !harddata_grid_name.empty() ) {
    std::string hdata_prop_name = parameters->value( "Hard_Data.property" );
    errors->report( hdata_prop_name.empty(), 
		                "Hard_Data", "No property name specified" );

    // Get the harddata grid from the grid manager
    bool ok = geostat_utils::create( harddata_grid_, harddata_grid_name, 
      	                           	  "Hard_Data", errors );
    if( !ok ) return false;

    harddata_property_ = harddata_grid_->property( hdata_prop_name );
    if( !harddata_property_ ) {
      	std::ostringstream error_stream;
      	error_stream <<  harddata_grid_name 
		                 <<  " does not have a property called " 
		                 << hdata_prop_name;
      	errors->report( "Hard_Data", error_stream.str() );
        return false;
    }

  }


  // hard data assignement and transform is only needed if we have a valid
  // hard data grid and property.  We always assign the data if it belongs
  // the same grid

  
  bool  assign_harddata = 
      String_Op::to_number<bool>( parameters->value( "Assign_Hard_Data.value" ) );
  if( harddata_grid_ == NULL ) assign_harddata=false; 
  else if( harddata_grid_ == simul_grid_ ) assign_harddata=true;

  if( assign_harddata ) {
    property_copier_ = 
      Property_copier_factory::get_copier( harddata_grid_, simul_grid_ );
    if( !property_copier_ ) {
      std::ostringstream message;
      message << "It is currently not possible to copy a property from a "
              << harddata_grid_->classname() << " to a " 
              << simul_grid_->classname() ;
      errors->report( !property_copier_, "Assign_Hard_Data", message.str() );
      return false;
    }
  }
 

  //-------------
  // Target histogram

  use_target_hist_ = 
    String_Op::to_number<bool>( parameters->value( "Use_Target_Histogram.value" ) );

  if(use_target_hist_) {
	  bool ok = distribution_utils::get_continuous_cdf(target_cdf_,parameters, errors, "nonParamCdf");
	  if(!ok) return false;
	  if( harddata_property_ ) {
		  harddata_property_ = 
        distribution_utils::gaussian_transform_property( harddata_property_, target_cdf_.raw_ptr(), harddata_grid_ );
		  if( !harddata_property_ ) return false;
      
      clear_temp_properties_ = true;
		  harddata_grid_->select_property( harddata_property_->name() );
	  }
  }


  //-------------
  // Number of realizations and random number seed

  nb_of_realizations_ = 
    String_Op::to_number<int>( parameters->value( "Nb_Realizations.value" ) );
  
  seed_ = String_Op::to_number<int>( parameters->value( "Seed.value" ) );

  //-------------
  // Variogram (covariance) initialization 

  bool init_cov_ok = 
    geostat_utils::initialize_covariance( &covar_, "Variogram", 
                                          parameters, errors );
  if( !init_cov_ok ) return false;


  //----------------
  // Report errors if any

  if( !errors->empty() ) {
    clean();
    return false;
  }
  this->extract_parameters(parameters);
  return true;
}
Beispiel #3
0
bool dssim::initialize( const Parameters_handler* parameters,
			Error_messages_handler* errors ) {

  OPEN_DEBUG_STREAM( "dssim.dbg" );

  // Extract the parameters input by the user from the parameter handler
  
  //-------------
  // The "simulation" grid parameters
  
  std::string simul_grid_name = parameters->value( "Grid_Name.value" );
  errors->report( simul_grid_name.empty(), 
            		  "Grid_Name", "No grid selected" );
  std::string property_name = parameters->value( "Property_Name.value" );
  errors->report( property_name.empty(), 
            		  "Property_Name", "No property name specified" );

  // Get the simulation grid from the grid manager  
  if( simul_grid_name.empty() ) return false;

  bool ok = geostat_utils::create( simul_grid_, simul_grid_name,
                          				 "Grid_Name", errors );
  if( !ok ) return false;

  // create  a multi-realization property
  multireal_property_ =
    simul_grid_->add_multi_realization_property( property_name );



  //-------------
  // The hard data parameters

  std::string harddata_grid_name = parameters->value( "Hard_Data.grid" );
 
  if( !harddata_grid_name.empty() ) {
    std::string hdata_prop_name = parameters->value( "Hard_Data.property" );
    errors->report( hdata_prop_name.empty(), 
		                "Hard_Data", "No property name specified" );

    // Get the harddata grid from the grid manager
    bool ok = geostat_utils::create( harddata_grid_, harddata_grid_name, 
      	                           	  "Hard_Data", errors );
    if( !ok ) return false;

    harddata_property_ = harddata_grid_->property( hdata_prop_name );
    if( !harddata_property_ ) {
      	std::ostringstream error_stream;
      	error_stream <<  harddata_grid_name 
		                 <<  " does not have a property called " 
		                 << hdata_prop_name;
      	errors->report( "Hard_Data", error_stream.str() );
        return false;
    }

  }


  // hard data assignement and transform is only needed if we have a valid
  // hard data grid and property

  bool assign_harddata = 
    String_Op::to_number<bool>( parameters->value( "Assign_Hard_Data.value" ) );
  if( harddata_grid_ == NULL ) assign_harddata=false; 
  else if( harddata_grid_ == simul_grid_ ) assign_harddata=true;

  if( harddata_grid_ ) {
    property_copier_ = 
      Property_copier_factory::get_copier( harddata_grid_, simul_grid_ );
    if( !property_copier_ ) {
      std::ostringstream message;
      message << "It is currently not possible to copy a property from a "
              << harddata_grid_->classname() << " to a " 
              << simul_grid_->classname() ;
      errors->report( !property_copier_, "Assign_Hard_Data", message.str() );
      return false;
    }
  }
 

  //-------------
  // Number of realizations and random number seed

  nb_of_realizations_ = 
    String_Op::to_number<int>( parameters->value( "Nb_Realizations.value" ) );
  
  seed_ = String_Op::to_number<int>( parameters->value( "Seed.value" ) );




  //-------------
  // Variogram (covariance) initialization 

  bool init_cov_ok = 
    geostat_utils::initialize_covariance( &covar_, "Variogram", 
                                          parameters, errors );
  if( !init_cov_ok ) return false;



  //-------------
  // Set up the search neighborhood

  int max_neigh = 
    String_Op::to_number<int>( parameters->value( "Max_Conditioning_Data.value" ) );
  
  GsTLTriplet ranges;
  GsTLTriplet angles;
  bool extract_ok =
    geostat_utils::extract_ellipsoid_definition( ranges, angles, 
                                					       "Search_Ellipsoid.value",
		  			                                     parameters, errors );
  if( !extract_ok ) return false;
  extract_ok = geostat_utils::is_valid_range_triplet( ranges );
  errors->report( !extract_ok,
                  "Search_Ellipsoid",
                  "Ranges must verify: major range >= " 
                  "medium range >= minor range >= 0" );
  if( !extract_ok ) return false;

  // If the hard data are not "relocated" on the simulation grid,
  // use a "combined neighborhood", otherwise use a single 
  // neighborhood
  if( !harddata_grid_ || assign_harddata ) {
    neighborhood_ = SmartPtr<Neighborhood>( 
                      simul_grid_->neighborhood( ranges, angles, &covar_ ) 
                    );
  }
  else {
    Neighborhood* simul_neigh = 
      simul_grid_->neighborhood( ranges, angles, &covar_ );
    Neighborhood* harddata_neigh = 
      harddata_grid_->neighborhood( ranges, angles, &covar_ );
    harddata_neigh->max_size( max_neigh );
    harddata_neigh->select_property( harddata_property_->name() );

    neighborhood_ = 
      SmartPtr<Neighborhood>( new Combined_neighborhood_dedup( harddata_neigh,
							                                           simul_neigh, &covar_, false ) );
  }

  neighborhood_->max_size( max_neigh );




  //-----------------
  // The kriging constraints and combiner

  geostat_utils::KrigTagMap tags_map;
  tags_map[ geostat_utils::KT  ] = "Trend.value";
  tags_map[ geostat_utils::LVM ] = "Local_Mean_Property.value";

  geostat_utils::KrigDefaultsMap defaults;
  defaults[ geostat_utils::SK ] = "0.0";

  geostat_utils::Kriging_type ktype = 
    geostat_utils::kriging_type( "Kriging_Type.value", parameters, errors );
  if(ktype == geostat_utils::SK ) 
	  defaults[ geostat_utils::SK ] = parameters->value("SK_mean.value");
 
	
  geostat_utils::initialize( ktype, combiner_, Kconstraints_,
                             tags_map,
                             parameters, errors,
                             simul_grid_, defaults );


// Type and parametrization of the cdf
  get_cdf( parameters, errors  );
  //ccdf_ =  get_cdf( parameters, errors );




  //----------------
  // Report errors if any

  if( !errors->empty() )
    return false;

  return true;
}