int main(int argc, char **argv) { unsigned int num_pts = 100; unsigned int num_pairs = num_pts / 2.0; const double alpha = 3.0; const double beta = 10.0; const double obs_stddev = 1e-3; MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, "infinite_dim/inverse_options", "", NULL); libMesh::LibMeshInit init(argc, argv); // Generate the mesh on which the samples live. libMesh::Mesh mesh; libMesh::MeshTools::Generation::build_line(mesh, num_pts, 0.0, 1.0, EDGE2); // Use a helper object to define some of the properties of our samples QUESO::FunctionOperatorBuilder fobuilder; fobuilder.order = "FIRST"; fobuilder.family = "LAGRANGE"; fobuilder.num_req_eigenpairs = num_pairs; // Define the mean of the prior QUESO::LibMeshFunction mean(fobuilder, mesh); // Define the precision operator of the prior QUESO::LibMeshNegativeLaplacianOperator precision(fobuilder, mesh); // Define the prior measure QUESO::InfiniteDimensionalGaussian mu(env, mean, precision, alpha, beta); // Create likelihood object Likelihood llhd(obs_stddev); // The worst hack in the world ever // boost::shared_ptr<LibMeshFunction> lm_draw(boost::static_pointer_cast<LibMeshFunction>(mu.draw())); // std::cout << "before zero" << std::endl; // lm_draw->equation_systems->get_system<ExplicitSystem>("Function").solution->zero(); // std::cout << "after zero" << std::endl; // Create the options helper object that determines what options to pass to // the sampler QUESO::InfiniteDimensionalMCMCSamplerOptions opts(env, ""); // Set the number of iterations to do opts.m_num_iters = 1000; // Set the frequency with which we save samples opts.m_save_freq = 10; // Set the RWMH step size opts.m_rwmh_step = 0.1; // Construct the sampler, and set the name of the output file (will only // write HDF5 files) QUESO::InfiniteDimensionalMCMCSampler s(env, mu, llhd, &opts); for (unsigned int i = 0; i < opts.m_num_iters; i++) { s.step(); if (i % 100 == 0) { std::cout << "sampler iteration: " << i << std::endl; std::cout << "avg acc prob is: " << s.avg_acc_prob() << std::endl; std::cout << "l2 norm is: " << s.llhd_val() << std::endl; } } return 0; }
int main(int argc, char **argv) { unsigned int num_pts = 100; unsigned int num_pairs = num_pts / 2.0; const double alpha = 3.0; const double beta = 10.0; const double obs_stddev = 1e-3; int ierr, my_rank, my_sub_rank; MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, argv[1], "", NULL); #ifndef QUESO_HAVE_HDF5 std::cerr << "Cannot run infinite dimensional inverse problems\n" << "without HDF5 enabled." << std::endl; MPI_Finalize(); return 0; #else // When the number of processes passed to `mpirun` is different from the // number of subenvironments asked for in the input file, QUESO creates a // subcommunicator that 'does the right thing'. // // For example, let's say you execute `mpirun -np 6`, but only asked for 3 // QUESO subenvironments, QUESO creates a subcommunicator containing two // processes for each of the three subenvironments. This subcommunicator is // the one returned by env.subComm(). To get a raw MPI communicator, call // Comm() on a QUESO communicator. MPI_Comm libMeshComm = env.subComm().Comm(); // Get full rank ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // Get subrank ierr = MPI_Comm_rank(libMeshComm, &my_sub_rank); std::stringstream ss; ss << "Hello from processor " << my_rank << " with sub rank " << my_sub_rank << " in subenvironment " << env.subIdString() << std::endl; std::cout << ss.str(); // Need an artifical block here because libMesh needs to call PetscFinalize // before we call MPI_Finalize { libMesh::LibMeshInit init(argc, argv, libMeshComm); // Generate the mesh on which the samples live. libMesh::Mesh mesh(init.comm()); libMesh::MeshTools::Generation::build_line(mesh, num_pts, 0.0, 1.0, libMeshEnums::EDGE2); // Use a helper object to define some of the properties of our samples QUESO::FunctionOperatorBuilder fobuilder; fobuilder.order = "FIRST"; fobuilder.family = "LAGRANGE"; fobuilder.num_req_eigenpairs = num_pairs; // Define the mean of the prior QUESO::LibMeshFunction mean(fobuilder, mesh); // Define the precision operator of the prior QUESO::LibMeshNegativeLaplacianOperator precision(fobuilder, mesh); // Define the prior measure QUESO::InfiniteDimensionalGaussian mu(env, mean, precision, alpha, beta); // Create likelihood object Likelihood llhd(obs_stddev); // Create the options helper object that determines what options to pass to // the sampler QUESO::InfiniteDimensionalMCMCSamplerOptions opts(env, ""); // Construct the sampler, and set the name of the output file (will only // write HDF5 files) QUESO::InfiniteDimensionalMCMCSampler s(env, mu, llhd, &opts); for (unsigned int i = 0; i < opts.m_num_iters; i++) { s.step(); if (i % 100 == 0) { std::cout << "sampler iteration: " << i << std::endl; std::cout << "avg acc prob is: " << s.avg_acc_prob() << std::endl; std::cout << "l2 norm is: " << s.llhd_val() << std::endl; } } } #endif // QUESO_HAVE_HDF5 MPI_Finalize(); return 0; }