Beispiel #1
0
int main(int argc, char ** argv)
{
    if( argc < 2 )
    {
        std::cerr << "ERROR: Must run program as '4d_interp <inputfile>'." << std::endl;
        exit(1);
    }
    std::string filename = argv[1];

    MPI_Init(&argc, &argv);
    QUESO::FullEnvironment env(MPI_COMM_WORLD, filename.c_str(), "", NULL);

    // We will read in the previously computed interpolation data
    QUESO::InterpolationSurrogateIOASCII<QUESO::GslVector, QUESO::GslMatrix>
    data_reader;

    data_reader.read( "./4d_interp_data_coarse.dat", env, "param_" );

    // Grab a reference to the data built in the reader
    const QUESO::InterpolationSurrogateData<QUESO::GslVector, QUESO::GslMatrix>&
    data = data_reader.data();

    // The reader read in the data, so now we can give the data
    // to the interpolation surrogate. This object can now be used in a likelihood
    // function for example. Here, we just illustrate calling the surrogate model
    // evaluation.
    QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
    four_d_surrogate( data );

    // A point in parameter space at which we want to use the surrogate
    QUESO::GslVector domainVector(data.get_paramDomain().vectorSpace().zeroVector());
    domainVector[0] = -0.4;
    domainVector[1] = 3.0;
    domainVector[2] = 1.5;
    domainVector[3] = 1.65;

    // Evaluate the surrogate model at the given point in parameter space
    // Because the exact function is quadrilinear, our interpolated value
    // should be exact.
    double value = four_d_surrogate.evaluate(domainVector);

    for( int r = 0; r < env.fullComm().NumProc(); r++ )
    {
        if( env.fullRank() == r )
        {
            std::cout << "======================================" << std::endl
                      << "Processor: " << env.fullRank() << std::endl
                      << "Interpolated value: " << value << std::endl
                      << "Exact value: " << four_d_fn( domainVector[0],domainVector[1],domainVector[2],domainVector[3])
                      << std::endl
                      << "======================================" << std::endl;
        }

        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Finalize();

    return 0;
}
int main(int argc, char ** argv)
{
  MPI_Init(&argc, &argv);
  QUESO::FullEnvironment env(MPI_COMM_WORLD, "test_InterpolationSurrogate/queso_input.txt", "", NULL);

  int return_flag = 0;

  std::string vs_prefix = "param_";

  // Filename for writing/reading surrogate data
  std::string filename1 = "test_write_InterpolationSurrogateBuilder_1.dat";
  std::string filename2 = "test_write_InterpolationSurrogateBuilder_2.dat";

  QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix>
      paramSpace(env,vs_prefix.c_str(), 4, NULL);

  // Point at which we will test the surrogate evaluation
  QUESO::GslVector domainVector(paramSpace.zeroVector());
  domainVector[0] = -0.4;
  domainVector[1] = 3.0;
  domainVector[2] = 1.5;
  domainVector[3] = 1.65;

  double exact_val_1 = four_d_fn_1(domainVector[0],domainVector[1],domainVector[2],domainVector[3]);
  double exact_val_2 = four_d_fn_2(domainVector[0],domainVector[1],domainVector[2],domainVector[3]);

  double tol = 2.0*std::numeric_limits<double>::epsilon();

  // First test surrogate build directly from the computed values
  {
    QUESO::GslVector paramMins(paramSpace.zeroVector());
    paramMins[0] = -1;
    paramMins[1] = -0.5;
    paramMins[2] = 1.1;
    paramMins[3] = -2.1;

    QUESO::GslVector paramMaxs(paramSpace.zeroVector());
    paramMaxs[0] = 0.9;
    paramMaxs[1] = 3.14;
    paramMaxs[2] = 2.1;
    paramMaxs[3] = 4.1;

    QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix>
      paramDomain("param_", paramSpace, paramMins, paramMaxs);

    std::vector<unsigned int> n_points(4);
    n_points[0] = 11;
    n_points[1] = 51;
    n_points[2] = 31;
    n_points[3] = 41;

    // One dataset for each of the two functions
    const unsigned int n_datasets = 2;

    QUESO::InterpolationSurrogateDataSet<QUESO::GslVector, QUESO::GslMatrix>
      data(paramDomain,n_points,n_datasets);

    MyInterpolationBuilder<QUESO::GslVector,QUESO::GslMatrix>
      builder( data );

    builder.build_values();

    QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
      four_d_surrogate_1( data.get_dataset(0) );

    QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
      four_d_surrogate_2( data.get_dataset(1) );

    double test_val_1 = four_d_surrogate_1.evaluate(domainVector);
    double test_val_2 = four_d_surrogate_2.evaluate(domainVector);

    return_flag  = return_flag ||
      test_val( test_val_1, exact_val_1, tol, "test_build_1" ) ||
      test_val( test_val_2, exact_val_2, tol, "test_build_2" );

    // Write the output to test reading next
    QUESO::InterpolationSurrogateIOASCII<QUESO::GslVector,QUESO::GslMatrix>
      data_writer;

    data_writer.write( filename1, data.get_dataset(0) );
    data_writer.write( filename2, data.get_dataset(1) );
  }

  // Now read the data and test
  {
    QUESO::InterpolationSurrogateIOASCII<QUESO::GslVector,QUESO::GslMatrix>
      data_reader_1, data_reader_2;

    data_reader_1.read( filename1, env, vs_prefix.c_str() );
    data_reader_2.read( filename2, env, vs_prefix.c_str() );


    // Build a new surrogate
    QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
      four_d_surrogate_1( data_reader_1.data() );

    QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
      four_d_surrogate_2( data_reader_2.data() );

    double test_val_1 = four_d_surrogate_1.evaluate(domainVector);
    double test_val_2 = four_d_surrogate_2.evaluate(domainVector);

    return_flag  = return_flag ||
      test_val( test_val_1, exact_val_1, tol, "test_read_1" ) ||
      test_val( test_val_2, exact_val_2, tol, "test_read_2" );
  }

  return return_flag;
}
int main(int argc, char ** argv)
{
  std::string inputFileName = "test_InterpolationSurrogate/queso_input.txt";
  const char * test_srcdir = std::getenv("srcdir");
  if (test_srcdir)
    inputFileName = test_srcdir + ('/' + inputFileName);

#ifdef QUESO_HAS_MPI
  MPI_Init(&argc, &argv);
  QUESO::FullEnvironment env(MPI_COMM_WORLD, inputFileName, "", NULL);
#else
  QUESO::FullEnvironment env(inputFileName, "", NULL);
#endif

  int return_flag = 0;

  QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix>
    paramSpace(env,"param_", 3, NULL);

  QUESO::GslVector paramMins(paramSpace.zeroVector());
  paramMins[0] = -1;
  paramMins[1] = -0.5;
  paramMins[2] = 1.1;

  QUESO::GslVector paramMaxs(paramSpace.zeroVector());
  paramMaxs[0] = 0.9;
  paramMaxs[1] = 3.14;
  paramMaxs[2] = 2.1;

  QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix>
    paramDomain("param_", paramSpace, paramMins, paramMaxs);

  std::vector<unsigned int> n_points(3);
  n_points[0] = 101;
  n_points[1] = 51;
  n_points[2] = 31;

  QUESO::InterpolationSurrogateData<QUESO::GslVector, QUESO::GslMatrix>
    data(paramDomain,n_points);

  std::vector<double> values(n_points[0]*n_points[1]*n_points[2]);

  double spacing_x = (paramMaxs[0] - paramMins[0])/(n_points[0]-1);
  double spacing_y = (paramMaxs[1] - paramMins[1])/(n_points[1]-1);
  double spacing_z = (paramMaxs[2] - paramMins[2])/(n_points[2]-1);

  for( unsigned int i = 0; i < n_points[0]; i++ )
    {
      for( unsigned int j = 0; j < n_points[1]; j++ )
        {
          for( unsigned int k = 0; k < n_points[2]; k++ )
            {
              unsigned int n = i + j*n_points[0] + k*n_points[0]*n_points[1];

              double x = paramMins[0] + i*spacing_x;
              double y = paramMins[1] + j*spacing_y;
              double z = paramMins[2] + k*spacing_z;

              values[n] = three_d_fn(x,y,z);
            }
        }
    }

  data.set_values( values );

  QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix>
    three_d_surrogate( data );

  QUESO::GslVector domainVector(paramSpace.zeroVector());
  domainVector[0] = -0.4;
  domainVector[1] = 3.0;
  domainVector[2] = 1.5;

  double test_val = three_d_surrogate.evaluate(domainVector);

  double exact_val = three_d_fn(domainVector[0],domainVector[1],domainVector[2]);

  double tol = 2.0*std::numeric_limits<double>::epsilon();

  double rel_error = (test_val - exact_val)/exact_val;

  if( std::fabs(rel_error) > tol )
    {
      std::cerr << "ERROR: Tolerance exceeded for 3D Lagrange interpolation test."
                << std::endl
                << " test_val  = " << test_val << std::endl
                << " exact_val = " << exact_val << std::endl
                << " rel_error = " << rel_error << std::endl
                << " tol       = " << tol << std::endl;

      return_flag = 1;
    }

#ifdef QUESO_HAS_MPI
  MPI_Finalize();
#endif
  return return_flag;
}