Real DerivedRBConstruction<RBConstruction>::truth_solve(int plot_solution) { START_LOG("truth_solve()", "DerivedRBConstruction"); EquationSystems& es = this->get_equation_systems(); RBConstruction& uber_system = es.get_system<RBConstruction>(uber_system_name); set_uber_current_parameters(); uber_system.get_rb_evaluation().set_parameters(uber_system.get_parameters()); uber_system.get_rb_evaluation().rb_solve(uber_system.get_rb_evaluation().get_n_basis_functions()); if(plot_solution > 0) { uber_system.load_rb_solution(); *solution = *(uber_system.solution); #ifdef LIBMESH_HAVE_EXODUS_API ExodusII_IO(get_mesh()).write_equation_systems ("unter_uber_truth.e", this->get_equation_systems()); #endif } STOP_LOG("truth_solve()", "DerivedRBConstruction"); // Don't bother returning the norm of the uber solution return 0.; }
// The main program int main(int argc, char** argv) { // Initialize libMesh LibMeshInit init(argc, argv); // Parameters GetPot infile("fem_system_params.in"); const Real global_tolerance = infile("global_tolerance", 0.); const unsigned int nelem_target = infile("n_elements", 400); const bool transient = infile("transient", true); const Real deltat = infile("deltat", 0.005); unsigned int n_timesteps = infile("n_timesteps", 1); //const unsigned int coarsegridsize = infile("coarsegridsize", 1); const unsigned int coarserefinements = infile("coarserefinements", 0); const unsigned int max_adaptivesteps = infile("max_adaptivesteps", 10); //const unsigned int dim = 2; #ifdef LIBMESH_HAVE_EXODUS_API const unsigned int write_interval = infile("write_interval", 5); #endif // Create a mesh, with dimension to be overridden later, distributed // across the default MPI communicator. Mesh mesh(init.comm()); GetPot infileForMesh("convdiff_mprime.in"); std::string find_mesh_here = infileForMesh("mesh","psiLF_mesh.xda"); mesh.read(find_mesh_here); std::cout << "Read in mesh from: " << find_mesh_here << "\n\n"; // And an object to refine it MeshRefinement mesh_refinement(mesh); mesh_refinement.coarsen_by_parents() = true; mesh_refinement.absolute_global_tolerance() = global_tolerance; mesh_refinement.nelem_target() = nelem_target; mesh_refinement.refine_fraction() = 0.3; mesh_refinement.coarsen_fraction() = 0.3; mesh_refinement.coarsen_threshold() = 0.1; //mesh_refinement.uniformly_refine(coarserefinements); // Print information about the mesh to the screen. mesh.print_info(); // Create an equation systems object. EquationSystems equation_systems (mesh); // Name system ConvDiff_MprimeSys & system = equation_systems.add_system<ConvDiff_MprimeSys>("Diff_ConvDiff_MprimeSys"); // Steady-state problem system.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system)); // Sanity check that we are indeed solving a steady problem libmesh_assert_equal_to (n_timesteps, 1); // Read in all the equation systems data from the LF solve (system, solutions, rhs, etc) std::string find_psiLF_here = infileForMesh("psiLF_file","psiLF.xda"); std::cout << "Looking for psiLF at: " << find_psiLF_here << "\n\n"; equation_systems.read(find_psiLF_here, READ, EquationSystems::READ_HEADER | EquationSystems::READ_DATA | EquationSystems::READ_ADDITIONAL_DATA); // Check that the norm of the solution read in is what we expect it to be Real readin_L2 = system.calculate_norm(*system.solution, 0, L2); std::cout << "Read in solution norm: "<< readin_L2 << std::endl << std::endl; //DEBUG //equation_systems.write("right_back_out.xda", WRITE, EquationSystems::WRITE_DATA | // EquationSystems::WRITE_ADDITIONAL_DATA); #ifdef LIBMESH_HAVE_GMV //GMVIO(equation_systems.get_mesh()).write_equation_systems(std::string("right_back_out.gmv"), equation_systems); #endif // Initialize the system //equation_systems.init (); //already initialized by read-in // And the nonlinear solver options NewtonSolver *solver = new NewtonSolver(system); system.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver); solver->quiet = infile("solver_quiet", true); solver->verbose = !solver->quiet; solver->max_nonlinear_iterations = infile("max_nonlinear_iterations", 15); solver->relative_step_tolerance = infile("relative_step_tolerance", 1.e-3); solver->relative_residual_tolerance = infile("relative_residual_tolerance", 0.0); solver->absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0); // And the linear solver options solver->max_linear_iterations = infile("max_linear_iterations", 50000); solver->initial_linear_tolerance = infile("initial_linear_tolerance", 1.e-3); // Print information about the system to the screen. equation_systems.print_info(); // Now we begin the timestep loop to compute the time-accurate // solution of the equations...not that this is transient, but eh, why not... for (unsigned int t_step=0; t_step != n_timesteps; ++t_step) { // A pretty update message std::cout << "\n\nSolving time step " << t_step << ", time = " << system.time << std::endl; // Adaptively solve the timestep unsigned int a_step = 0; for (; a_step != max_adaptivesteps; ++a_step) { // VESTIGIAL for now ('vestigial' eh ? ;) ) std::cout << "\n\n I should be skipped what are you doing here lalalalalalala *!**!*!*!*!*!* \n\n"; system.solve(); system.postprocess(); ErrorVector error; AutoPtr<ErrorEstimator> error_estimator; // To solve to a tolerance in this problem we // need a better estimator than Kelly if (global_tolerance != 0.) { // We can't adapt to both a tolerance and a mesh // size at once libmesh_assert_equal_to (nelem_target, 0); UniformRefinementEstimator *u = new UniformRefinementEstimator; // The lid-driven cavity problem isn't in H1, so // lets estimate L2 error u->error_norm = L2; error_estimator.reset(u); } else { // If we aren't adapting to a tolerance we need a // target mesh size libmesh_assert_greater (nelem_target, 0); // Kelly is a lousy estimator to use for a problem // not in H1 - if we were doing more than a few // timesteps we'd need to turn off or limit the // maximum level of our adaptivity eventually error_estimator.reset(new KellyErrorEstimator); } // Calculate error std::vector<Real> weights(9,1.0); // based on u, v, p, c, their adjoints, and source parameter // Keep the same default norm type. std::vector<FEMNormType> norms(1, error_estimator->error_norm.type(0)); error_estimator->error_norm = SystemNorm(norms, weights); error_estimator->estimate_error(system, error); // Print out status at each adaptive step. Real global_error = error.l2_norm(); std::cout << "Adaptive step " << a_step << ": " << std::endl; if (global_tolerance != 0.) std::cout << "Global_error = " << global_error << std::endl; if (global_tolerance != 0.) std::cout << "Worst element error = " << error.maximum() << ", mean = " << error.mean() << std::endl; if (global_tolerance != 0.) { // If we've reached our desired tolerance, we // don't need any more adaptive steps if (global_error < global_tolerance) break; mesh_refinement.flag_elements_by_error_tolerance(error); } else { // If flag_elements_by_nelem_target returns true, this // should be our last adaptive step. if (mesh_refinement.flag_elements_by_nelem_target(error)) { mesh_refinement.refine_and_coarsen_elements(); equation_systems.reinit(); a_step = max_adaptivesteps; break; } } // Carry out the adaptive mesh refinement/coarsening mesh_refinement.refine_and_coarsen_elements(); equation_systems.reinit(); std::cout << "Refined mesh to " << mesh.n_active_elem() << " active elements and " << equation_systems.n_active_dofs() << " active dofs." << std::endl; } // End loop over adaptive steps // Do one last solve if necessary if (a_step == max_adaptivesteps) { QoISet qois; std::vector<unsigned int> qoi_indices; qoi_indices.push_back(0); qois.add_indices(qoi_indices); qois.set_weight(0, 1.0); system.assemble_qoi_sides = true; //QoI doesn't involve sides std::cout << "\n~*~*~*~*~*~*~*~*~ adjoint solve start ~*~*~*~*~*~*~*~*~\n" << std::endl; std::pair<unsigned int, Real> adjsolve = system.adjoint_solve(); std::cout << "number of iterations to solve adjoint: " << adjsolve.first << std::endl; std::cout << "final residual of adjoint solve: " << adjsolve.second << std::endl; std::cout << "\n~*~*~*~*~*~*~*~*~ adjoint solve end ~*~*~*~*~*~*~*~*~" << std::endl; NumericVector<Number> &dual_solution = system.get_adjoint_solution(0); NumericVector<Number> &primal_solution = *system.solution; primal_solution.swap(dual_solution); ExodusII_IO(mesh).write_timestep("super_adjoint.exo", equation_systems, 1, /* This number indicates how many time steps are being written to the file */ system.time); primal_solution.swap(dual_solution); system.assemble(); //overwrite residual read in from psiLF solve // The total error estimate system.postprocess(); //to compute M_HF(psiLF) and M_LF(psiLF) terms Real QoI_error_estimate = (-0.5*(system.rhs)->dot(dual_solution)) + system.get_MHF_psiLF() - system.get_MLF_psiLF(); std::cout << "\n\n 0.5*M'_HF(psiLF)(superadj): " << std::setprecision(17) << 0.5*(system.rhs)->dot(dual_solution) << "\n"; std::cout << " M_HF(psiLF): " << std::setprecision(17) << system.get_MHF_psiLF() << "\n"; std::cout << " M_LF(psiLF): " << std::setprecision(17) << system.get_MLF_psiLF() << "\n"; std::cout << "\n\n Residual L2 norm: " << system.calculate_norm(*system.rhs, L2) << "\n"; std::cout << " Residual discrete L2 norm: " << system.calculate_norm(*system.rhs, DISCRETE_L2) << "\n"; std::cout << " Super-adjoint L2 norm: " << system.calculate_norm(dual_solution, L2) << "\n"; std::cout << " Super-adjoint discrete L2 norm: " << system.calculate_norm(dual_solution, DISCRETE_L2) << "\n"; std::cout << "\n\n QoI error estimate: " << std::setprecision(17) << QoI_error_estimate << "\n\n"; //DEBUG std::cout << "\n------------ herp derp ------------" << std::endl; //libMesh::out.precision(16); //dual_solution.print(); //system.get_adjoint_rhs().print(); AutoPtr<NumericVector<Number> > adjresid = system.solution->clone(); (system.matrix)->vector_mult(*adjresid,system.get_adjoint_solution(0)); SparseMatrix<Number>& adjmat = *system.matrix; (system.matrix)->get_transpose(adjmat); adjmat.vector_mult(*adjresid,system.get_adjoint_solution(0)); //std::cout << "******************** matrix-superadj product (libmesh) ************************" << std::endl; //adjresid->print(); adjresid->add(-1.0, system.get_adjoint_rhs(0)); //std::cout << "******************** superadjoint system residual (libmesh) ***********************" << std::endl; //adjresid->print(); std::cout << "\n\nadjoint system residual (discrete L2): " << system.calculate_norm(*adjresid,DISCRETE_L2) << std::endl; std::cout << "adjoint system residual (L2, all): " << system.calculate_norm(*adjresid,L2) << std::endl; std::cout << "adjoint system residual (L2, 0): " << system.calculate_norm(*adjresid,0,L2) << std::endl; std::cout << "adjoint system residual (L2, 1): " << system.calculate_norm(*adjresid,1,L2) << std::endl; std::cout << "adjoint system residual (L2, 2): " << system.calculate_norm(*adjresid,2,L2) << std::endl; std::cout << "adjoint system residual (L2, 3): " << system.calculate_norm(*adjresid,3,L2) << std::endl; std::cout << "adjoint system residual (L2, 4): " << system.calculate_norm(*adjresid,4,L2) << std::endl; std::cout << "adjoint system residual (L2, 5): " << system.calculate_norm(*adjresid,5,L2) << std::endl; /* AutoPtr<NumericVector<Number> > sadj_matlab = system.solution->clone(); AutoPtr<NumericVector<Number> > adjresid_matlab = system.solution->clone(); if(FILE *fp=fopen("superadj_matlab.txt","r")){ Real value; int counter = 0; int flag = 1; while(flag != -1){ flag = fscanf(fp,"%lf",&value); if(flag != -1){ sadj_matlab->set(counter, value); counter += 1; } } fclose(fp); } (system.matrix)->vector_mult(*adjresid_matlab,*sadj_matlab); //std::cout << "******************** matrix-superadj product (matlab) ***********************" << std::endl; //adjresid_matlab->print(); adjresid_matlab->add(-1.0, system.get_adjoint_rhs(0)); //std::cout << "******************** superadjoint system residual (matlab) ***********************" << std::endl; //adjresid_matlab->print(); std::cout << "\n\nmatlab import adjoint system residual (discrete L2): " << system.calculate_norm(*adjresid_matlab,DISCRETE_L2) << "\n" << std::endl; */ /* AutoPtr<NumericVector<Number> > sadj_fwd_hack = system.solution->clone(); AutoPtr<NumericVector<Number> > adjresid_fwd_hack = system.solution->clone(); if(FILE *fp=fopen("superadj_forward_hack.txt","r")){ Real value; int counter = 0; int flag = 1; while(flag != -1){ flag = fscanf(fp,"%lf",&value); if(flag != -1){ sadj_fwd_hack->set(counter, value); counter += 1; } } fclose(fp); } (system.matrix)->vector_mult(*adjresid_fwd_hack,*sadj_fwd_hack); //std::cout << "******************** matrix-superadj product (fwd_hack) ***********************" << std::endl; //adjresid_fwd_hack->print(); adjresid_fwd_hack->add(-1.0, system.get_adjoint_rhs(0)); //std::cout << "******************** superadjoint system residual (fwd_hack) ***********************" << std::endl; //adjresid_fwd_hack->print(); std::cout << "\n\nfwd_hack import adjoint system residual (discrete L2): " << system.calculate_norm(*adjresid_fwd_hack,DISCRETE_L2) << "\n" << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 0): " << system.calculate_norm(*adjresid_fwd_hack,0,L2) << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 1): " << system.calculate_norm(*adjresid_fwd_hack,1,L2) << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 2): " << system.calculate_norm(*adjresid_fwd_hack,2,L2) << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 3): " << system.calculate_norm(*adjresid_fwd_hack,3,L2) << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 4): " << system.calculate_norm(*adjresid_fwd_hack,4,L2) << std::endl; std::cout << "fwd_hack adjoint system residual (L2, 5): " << system.calculate_norm(*adjresid_fwd_hack,5,L2) << std::endl; */ //std::cout << "************************ system.matrix ***********************" << std::endl; //system.matrix->print(); std::cout << "\n------------ herp derp ------------" << std::endl; // The cell wise breakdown ErrorVector cell_wise_error; cell_wise_error.resize((system.rhs)->size()); for(unsigned int i = 0; i < (system.rhs)->size() ; i++) { if(i < system.get_mesh().n_elem()) cell_wise_error[i] = fabs(-0.5*((system.rhs)->el(i) * dual_solution(i)) + system.get_MHF_psiLF(i) - system.get_MLF_psiLF(i)); else cell_wise_error[i] = fabs(-0.5*((system.rhs)->el(i) * dual_solution(i))); /*csv from 'save data' from gmv output gives a few values at each node point (value for every element that shares that node), yet paraview display only seems to show one of them -> the value in an element is given at each of the nodes that it has, hence the repetition; what is displayed in paraview is each element's value; even though MHF_psiLF and MLF_psiLF are stored by element this seems to give elemental contributions that agree with if we had taken the superadj-residual dot product by integrating over elements*/ /*at higher mesh resolutions and lower k, weird-looking artifacts start to appear and it no longer agrees with output from manual integration of superadj-residual...*/ } // Plot it std::ostringstream error_gmv; error_gmv << "error.gmv"; cell_wise_error.plot_error(error_gmv.str(), equation_systems.get_mesh()); //alternate element-wise breakdown, outputed as values matched to element centroids; for matlab plotz primal_solution.swap(dual_solution); system.postprocess(1); primal_solution.swap(dual_solution); system.postprocess(2); std::cout << "\n\n -0.5*M'_HF(psiLF)(superadj): " << std::setprecision(17) << system.get_half_adj_weighted_resid() << "\n"; primal_solution.swap(dual_solution); std::string write_error_here = infileForMesh("error_est_output_file", "error_est_breakdown.dat"); std::ofstream output(write_error_here); for(unsigned int i = 0 ; i < system.get_mesh().n_elem(); i++) { Point elem_cent = system.get_mesh().elem(i)->centroid(); if(output.is_open()) { output << elem_cent(0) << " " << elem_cent(1) << " " << fabs(system.get_half_adj_weighted_resid(i) + system.get_MHF_psiLF(i) - system.get_MLF_psiLF(i)) << "\n"; } } output.close(); } // End if at max adaptive steps #ifdef LIBMESH_HAVE_EXODUS_API // Write out this timestep if we're requested to if ((t_step+1)%write_interval == 0) { std::ostringstream file_name; /* // We write the file in the ExodusII format. file_name << "out_" << std::setw(3) << std::setfill('0') << std::right << t_step+1 << ".e"; //this should write out the primal which should be the same as what's read in... ExodusII_IO(mesh).write_timestep(file_name.str(), equation_systems, 1, //number of time steps written to file system.time); */ } #endif // #ifdef LIBMESH_HAVE_EXODUS_API } // All done. return 0; } //end main
Real RBEIMConstruction::truth_solve(int plot_solution) { START_LOG("truth_solve()", "RBEIMConstruction"); // matrix should have been set to inner_product_matrix during initialization // if(!single_matrix_mode) // { // matrix->zero(); // matrix->add(1., *inner_product_matrix); // } // else // { // assemble_inner_product_matrix(matrix); // } int training_parameters_found_index = -1; if( _parametrized_functions_in_training_set_initialized ) { // Check if parameters are in the training set. If so, we can just load the // solution from _parametrized_functions_in_training_set for(unsigned int i=0; i<get_n_training_samples(); i++) { if(get_parameters() == get_params_from_training_set(i)) { training_parameters_found_index = i; break; } } } // If the parameters are in the training set, just copy the solution vector if(training_parameters_found_index >= 0) { *solution = *_parametrized_functions_in_training_set[training_parameters_found_index]; update(); // put the solution into current_local_solution as well } // Otherwise, we have to compute the projection else { RBEIMEvaluation& eim_eval = libmesh_cast_ref<RBEIMEvaluation&>(get_rb_evaluation()); eim_eval.set_parameters( get_parameters() ); // Compute truth representation via projection const MeshBase& mesh = this->get_mesh(); AutoPtr<FEMContext> c = this->build_context(); FEMContext &context = libmesh_cast_ref<FEMContext&>(*c); this->init_context(context); rhs->zero(); MeshBase::const_element_iterator el = mesh.active_local_elements_begin(); const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end(); for ( ; el != end_el; ++el) { context.pre_fe_reinit(*this, *el); context.elem_fe_reinit(); for(unsigned int var=0; var<n_vars(); var++) { const std::vector<Real> &JxW = context.element_fe_var[var]->get_JxW(); const std::vector<std::vector<Real> >& phi = context.element_fe_var[var]->get_phi(); const std::vector<Point> &xyz = context.element_fe_var[var]->get_xyz(); unsigned int n_qpoints = context.element_qrule->n_points(); unsigned int n_var_dofs = libmesh_cast_int<unsigned int> (context.dof_indices_var[var].size()); DenseSubVector<Number>& subresidual_var = *context.elem_subresiduals[var]; for(unsigned int qp=0; qp<n_qpoints; qp++) for(unsigned int i=0; i != n_var_dofs; i++) subresidual_var(i) += JxW[qp] * eim_eval.evaluate_parametrized_function(var, xyz[qp]) * phi[i][qp]; } // Apply constraints, e.g. periodic constraints this->get_dof_map().constrain_element_vector(context.get_elem_residual(), context.dof_indices); // Add element vector to global vector rhs->add_vector(context.get_elem_residual(), context.dof_indices); } // Solve to find the best fit, then solution stores the truth representation // of the function to be approximated solve(); // Make sure we didn't max out the number of iterations if( (this->n_linear_iterations() >= this->get_equation_systems().parameters.get<unsigned int>("linear solver maximum iterations")) && (this->final_linear_residual() > this->get_equation_systems().parameters.get<Real>("linear solver tolerance")) ) { libMesh::out << "Warning: Linear solver may not have converged! Final linear residual = " << this->final_linear_residual() << ", number of iterations = " << this->n_linear_iterations() << std::endl << std::endl; // libmesh_error(); } if(reuse_preconditioner) { // After we've done a solve we can now reuse the preconditioner // because the matrix is not changing linear_solver->reuse_preconditioner(true); } } if(plot_solution > 0) { #ifdef LIBMESH_HAVE_EXODUS_API ExodusII_IO(get_mesh()).write_equation_systems ("truth.e", this->get_equation_systems()); #endif } STOP_LOG("truth_solve()", "RBEIMConstruction"); return 0.; }
Real RBEIMConstruction::truth_solve(int plot_solution) { START_LOG("truth_solve()", "RBEIMConstruction"); int training_parameters_found_index = -1; if( _parametrized_functions_in_training_set_initialized ) { // Check if parameters are in the training set. If so, we can just load the // solution from _parametrized_functions_in_training_set for(unsigned int i=0; i<get_n_training_samples(); i++) { if(get_parameters() == get_params_from_training_set(i)) { training_parameters_found_index = i; break; } } } // If the parameters are in the training set, just copy the solution vector if(training_parameters_found_index >= 0) { *solution = *_parametrized_functions_in_training_set[training_parameters_found_index]; update(); // put the solution into current_local_solution as well } // Otherwise, we have to compute the projection else { RBEIMEvaluation& eim_eval = cast_ref<RBEIMEvaluation&>(get_rb_evaluation()); eim_eval.set_parameters( get_parameters() ); // Compute truth representation via projection const MeshBase& mesh = this->get_mesh(); UniquePtr<DGFEMContext> c = this->build_context(); DGFEMContext &context = cast_ref<DGFEMContext&>(*c); this->init_context(context); rhs->zero(); MeshBase::const_element_iterator el = mesh.active_local_elements_begin(); const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end(); for ( ; el != end_el; ++el) { context.pre_fe_reinit(*this, *el); context.elem_fe_reinit(); // All variables should have the same quadrature rule, hence // we can get JxW and xyz based on first_elem_fe. FEBase* first_elem_fe = NULL; context.get_element_fe( 0, first_elem_fe ); unsigned int n_qpoints = context.get_element_qrule().n_points(); const std::vector<Real> &JxW = first_elem_fe->get_JxW(); const std::vector<Point> &xyz = first_elem_fe->get_xyz(); // Loop over qp before var because parametrized functions often use // some caching based on qp. for (unsigned int qp=0; qp<n_qpoints; qp++) { for (unsigned int var=0; var<n_vars(); var++) { FEBase* elem_fe = NULL; context.get_element_fe( var, elem_fe ); const std::vector<std::vector<Real> >& phi = elem_fe->get_phi(); DenseSubVector<Number>& subresidual_var = context.get_elem_residual( var ); unsigned int n_var_dofs = cast_int<unsigned int>(context.get_dof_indices( var ).size()); Number eval_result = eim_eval.evaluate_parametrized_function(var, xyz[qp], *(*el)); for (unsigned int i=0; i != n_var_dofs; i++) subresidual_var(i) += JxW[qp] * eval_result * phi[i][qp]; } } // Apply constraints, e.g. periodic constraints this->get_dof_map().constrain_element_vector(context.get_elem_residual(), context.get_dof_indices() ); // Add element vector to global vector rhs->add_vector(context.get_elem_residual(), context.get_dof_indices() ); } // Solve to find the best fit, then solution stores the truth representation // of the function to be approximated solve_for_matrix_and_rhs(*inner_product_solver, *inner_product_matrix, *rhs); if (assert_convergence) check_convergence(*inner_product_solver); } if(plot_solution > 0) { #ifdef LIBMESH_HAVE_EXODUS_API ExodusII_IO(get_mesh()).write_equation_systems ("truth.exo", this->get_equation_systems()); #endif } STOP_LOG("truth_solve()", "RBEIMConstruction"); return 0.; }
int main(int argc, char** argv){ //initialize libMesh LibMeshInit init(argc, argv); //parameters GetPot infile("fem_system_params.in"); const Real global_tolerance = infile("global_tolerance", 0.); const unsigned int nelem_target = infile("n_elements", 400); const bool transient = infile("transient", true); const Real deltat = infile("deltat", 0.005); unsigned int n_timesteps = infile("n_timesteps", 1); //const unsigned int coarsegridsize = infile("coarsegridsize", 1); const unsigned int coarserefinements = infile("coarserefinements", 0); const unsigned int max_adaptivesteps = infile("max_adaptivesteps", 10); //const unsigned int dim = 2; #ifdef LIBMESH_HAVE_EXODUS_API const unsigned int write_interval = infile("write_interval", 5); #endif // Create a mesh, with dimension to be overridden later, distributed // across the default MPI communicator. Mesh mesh(init.comm()); Mesh mesh2(init.comm()); GetPot infileForMesh("convdiff_mprime.in"); std::string find_mesh_here = infileForMesh("divided_mesh","meep.exo"); mesh.read(find_mesh_here); mesh2.read(find_mesh_here); //mesh.read("psiHF_mesh_1Dfused.xda"); // And an object to refine it /*MeshRefinement mesh_refinement(mesh); mesh_refinement.coarsen_by_parents() = true; mesh_refinement.absolute_global_tolerance() = global_tolerance; mesh_refinement.nelem_target() = nelem_target; mesh_refinement.refine_fraction() = 0.3; mesh_refinement.coarsen_fraction() = 0.3; mesh_refinement.coarsen_threshold() = 0.1; mesh_refinement.uniformly_refine(coarserefinements);*/ // Print information about the mesh to the screen. mesh.print_info(); // Create an equation systems object. EquationSystems equation_systems (mesh); EquationSystems equation_systems_mix(mesh2); //name system ConvDiff_PrimarySys & system_primary = equation_systems.add_system<ConvDiff_PrimarySys>("ConvDiff_PrimarySys"); ConvDiff_AuxSys & system_aux = equation_systems.add_system<ConvDiff_AuxSys>("ConvDiff_AuxSys"); ConvDiff_MprimeSys & system_mix = equation_systems_mix.add_system<ConvDiff_MprimeSys>("ConvDiff_MprimeSys"); //steady-state problem system_primary.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_primary)); system_aux.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_aux)); system_mix.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_mix)); libmesh_assert_equal_to (n_timesteps, 1); /*equation_systems.read("psiLF.xda", READ, EquationSystems::READ_HEADER | EquationSystems::READ_DATA | EquationSystems::READ_ADDITIONAL_DATA); equation_systems.print_info();*/ // Initialize the system equation_systems.init (); // Set the time stepping options system_primary.deltat = deltat; system_aux.deltat = deltat;//this is ignored for SteadySolver...right? // And the nonlinear solver options NewtonSolver *solver_primary = new NewtonSolver(system_primary); system_primary.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_primary); solver_primary->quiet = infile("solver_quiet", true); solver_primary->verbose = !solver_primary->quiet; solver_primary->max_nonlinear_iterations = infile("max_nonlinear_iterations", 15); solver_primary->relative_step_tolerance = infile("relative_step_tolerance", 1.e-3); solver_primary->relative_residual_tolerance = infile("relative_residual_tolerance", 0.0); solver_primary->absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0); NewtonSolver *solver_aux = new NewtonSolver(system_aux); system_aux.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_aux); solver_aux->quiet = infile("solver_quiet", true); solver_aux->verbose = !solver_aux->quiet; solver_aux->max_nonlinear_iterations = infile("max_nonlinear_iterations", 15); solver_aux->relative_step_tolerance = infile("relative_step_tolerance", 1.e-3); solver_aux->relative_residual_tolerance = infile("relative_residual_tolerance", 0.0); solver_aux->absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0); // And the linear solver options solver_primary->max_linear_iterations = infile("max_linear_iterations", 50000); solver_primary->initial_linear_tolerance = infile("initial_linear_tolerance", 1.e-3); solver_aux->max_linear_iterations = infile("max_linear_iterations", 50000); solver_aux->initial_linear_tolerance = infile("initial_linear_tolerance", 1.e-3); // Print information about the system to the screen. equation_systems.print_info(); // Now we begin the timestep loop to compute the time-accurate // solution of the equations...not that this is transient, but eh, why not... for (unsigned int t_step=0; t_step != n_timesteps; ++t_step){ // A pretty update message std::cout << "\n\nSolving time step " << t_step << ", time = " << system_primary.time << std::endl; // Adaptively solve the timestep unsigned int a_step = 0; /*for (; a_step != max_adaptivesteps; ++a_step) { system.solve(); system.postprocess(); ErrorVector error; AutoPtr<ErrorEstimator> error_estimator; // To solve to a tolerance in this problem we // need a better estimator than Kelly if (global_tolerance != 0.) { // We can't adapt to both a tolerance and a mesh // size at once libmesh_assert_equal_to (nelem_target, 0); UniformRefinementEstimator *u = new UniformRefinementEstimator; // The lid-driven cavity problem isn't in H1, so // lets estimate L2 error u->error_norm = L2; error_estimator.reset(u); } else { // If we aren't adapting to a tolerance we need a // target mesh size libmesh_assert_greater (nelem_target, 0); // Kelly is a lousy estimator to use for a problem // not in H1 - if we were doing more than a few // timesteps we'd need to turn off or limit the // maximum level of our adaptivity eventually error_estimator.reset(new KellyErrorEstimator); } // Calculate error std::vector<Real> weights(9,1.0); // based on u, v, p, c, their adjoints, and source parameter // Keep the same default norm type. std::vector<FEMNormType> norms(1, error_estimator->error_norm.type(0)); error_estimator->error_norm = SystemNorm(norms, weights); error_estimator->estimate_error(system, error); // Print out status at each adaptive step. Real global_error = error.l2_norm(); std::cout << "Adaptive step " << a_step << ": " << std::endl; if (global_tolerance != 0.) std::cout << "Global_error = " << global_error << std::endl; if (global_tolerance != 0.) std::cout << "Worst element error = " << error.maximum() << ", mean = " << error.mean() << std::endl; if (global_tolerance != 0.) { // If we've reached our desired tolerance, we // don't need any more adaptive steps if (global_error < global_tolerance) break; mesh_refinement.flag_elements_by_error_tolerance(error); } else { // If flag_elements_by_nelem_target returns true, this // should be our last adaptive step. if (mesh_refinement.flag_elements_by_nelem_target(error)) { mesh_refinement.refine_and_coarsen_elements(); equation_systems.reinit(); a_step = max_adaptivesteps; break; } } // Carry out the adaptive mesh refinement/coarsening mesh_refinement.refine_and_coarsen_elements(); equation_systems.reinit(); std::cout << "Refined mesh to " << mesh.n_active_elem() << " active elements and " << equation_systems.n_active_dofs() << " active dofs." << std::endl; }*/ // Do one last solve if necessary if (a_step == max_adaptivesteps) { system_primary.solve(); std::cout << "\n\n Residual L2 norm (primary): " << system_primary.calculate_norm(*system_primary.rhs, L2) << "\n"; system_aux.solve(); std::cout << "\n\n Residual L2 norm (auxiliary): " << system_aux.calculate_norm(*system_aux.rhs, L2) << "\n"; equation_systems_mix.init(); DirectSolutionTransfer sol_transfer(init.comm()); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("c")), system_mix.variable(system_mix.variable_number("c"))); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("zc")), system_mix.variable(system_mix.variable_number("zc"))); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("fc")), system_mix.variable(system_mix.variable_number("fc"))); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_c")), system_mix.variable(system_mix.variable_number("aux_c"))); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_zc")), system_mix.variable(system_mix.variable_number("aux_zc"))); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_fc")), system_mix.variable(system_mix.variable_number("aux_fc"))); std::cout << "c: " << system_mix.calculate_norm(*system_mix.solution, 0, L2) << " " << system_primary.calculate_norm(*system_primary.solution, 0, L2) << std::endl; std::cout << "zc: " << system_mix.calculate_norm(*system_mix.solution, 1, L2) << " " << system_primary.calculate_norm(*system_primary.solution, 1, L2) << std::endl; std::cout << "fc: " << system_mix.calculate_norm(*system_mix.solution, 2, L2) << " " << system_primary.calculate_norm(*system_primary.solution, 2, L2) << std::endl; std::cout << "aux_c: " << system_mix.calculate_norm(*system_mix.solution, 3, L2) << " " << system_aux.calculate_norm(*system_aux.solution, 0, L2) << std::endl; std::cout << "aux_zc: " << system_mix.calculate_norm(*system_mix.solution, 4, L2) << " " << system_aux.calculate_norm(*system_aux.solution, 1, L2) << std::endl; std::cout << "aux_fc: " << system_mix.calculate_norm(*system_mix.solution, 5, L2) << " " << system_aux.calculate_norm(*system_aux.solution, 2, L2) << std::endl; std::cout << "Overall: " << system_mix.calculate_norm(*system_mix.solution, L2) << std::endl; system_mix.postprocess(); //DEBUG std::cout << " M_HF(psiLF): " << std::setprecision(17) << system_mix.get_MHF_psiLF() << "\n"; std::cout << " I(psiLF): " << std::setprecision(17) << system_mix.get_MLF_psiLF() << "\n"; } // Advance to the next timestep in a transient problem system_primary.time_solver->advance_timestep(); #ifdef LIBMESH_HAVE_EXODUS_API // Write out this timestep if we're requested to if ((t_step+1)%write_interval == 0) { //std::ostringstream file_name; // We write the file in the ExodusII format. //file_name << "out_" // << std::setw(3) // << std::setfill('0') // << std::right // << t_step+1 // << ".e"; //ExodusII_IO(mesh).write_timestep(file_name.str(), ExodusII_IO(mesh).write_timestep("psiLF.exo", equation_systems_mix, 1, /* This number indicates how many time steps are being written to the file */ system_primary.time); mesh.write("psiLF_mesh.xda"); equation_systems_mix.write("psiLF.xda", WRITE, EquationSystems::WRITE_DATA | EquationSystems::WRITE_ADDITIONAL_DATA); } #endif // #ifdef LIBMESH_HAVE_EXODUS_API } // All done. return 0; } //end main
int main(int argc, char** argv){ //initialize libMesh LibMeshInit init(argc, argv); //parameters GetPot infile("fem_system_params.in"); const bool transient = infile("transient", true); const Real deltat = infile("deltat", 0.005); unsigned int n_timesteps = infile("n_timesteps", 20); const int nx = infile("nx",100); const int ny = infile("ny",100); const int nz = infile("nz",100); //const unsigned int dim = 3; const unsigned int max_r_steps = infile("max_r_steps", 3); const unsigned int max_r_level = infile("max_r_level", 3); const Real refine_percentage = infile("refine_percentage", 0.1); const Real coarsen_percentage = infile("coarsen_percentage", 0.0); const std::string indicator_type = infile("indicator_type", "kelly"); const bool write_error = infile("write_error",false); const bool flag_by_elem_frac = infile("flag_by_elem_frac",true); #ifdef LIBMESH_HAVE_EXODUS_API const unsigned int write_interval = infile("write_interval", 5); #endif // Create a mesh, with dimension to be overridden later, distributed // across the default MPI communicator. Mesh mesh(init.comm()); //create mesh unsigned int dim; if(nz == 0){ //to check if oscillations happen in 2D as well... dim = 2; MeshTools::Generation::build_square(mesh, nx, ny, 497150.0, 501750.0, 537350.0, 540650.0, QUAD9); }else{ dim = 3; MeshTools::Generation::build_cube(mesh, nx, ny, nz, 497150.0, 501750.0, 537350.0, 540650.0, 0.0, 100.0, HEX27); } // Print information about the mesh to the screen. mesh.print_info(); // Create an equation systems object. EquationSystems equation_systems (mesh); //name system ContamTransSysInv & system = equation_systems.add_system<ContamTransSysInv>("ContamTransInv"); //solve as steady or transient if(transient){ //system.time_solver = AutoPtr<TimeSolver>(new EulerSolver(system)); //backward Euler system.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system)); std::cout << "\n\nAaahhh transience not yet available!\n" << std::endl; n_timesteps = 1; } else{ system.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system)); libmesh_assert_equal_to (n_timesteps, 1); //this doesn't seem to work? } // Initialize the system equation_systems.init (); //initial conditions read_initial_parameters(); system.project_solution(initial_value, initial_grad, equation_systems.parameters); finish_initialization(); // Set the time stepping options... system.deltat = deltat; //...and the nonlinear solver options... NewtonSolver *solver = new NewtonSolver(system); system.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver); solver->quiet = infile("solver_quiet", true); solver->verbose = !solver->quiet; solver->max_nonlinear_iterations = infile("max_nonlinear_iterations", 15); solver->relative_step_tolerance = infile("relative_step_tolerance", 1.e-3); solver->relative_residual_tolerance = infile("relative_residual_tolerance", 0.0); solver->absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.0); // And the linear solver options solver->max_linear_iterations = infile("max_linear_iterations", 10000); solver->initial_linear_tolerance = infile("initial_linear_tolerance",1.e-13); solver->minimum_linear_tolerance = infile("minimum_linear_tolerance",1.e-13); solver->linear_tolerance_multiplier = infile("linear_tolerance_multiplier",1.e-3); // Mesh Refinement object - to test effect of constant refined mesh (not refined at every timestep) MeshRefinement mesh_refinement(mesh); mesh_refinement.refine_fraction() = refine_percentage; mesh_refinement.coarsen_fraction() = coarsen_percentage; mesh_refinement.max_h_level() = max_r_level; // Print information about the system to the screen. equation_systems.print_info(); ExodusII_IO exodusIO = ExodusII_IO(mesh); //for writing multiple timesteps to one file for (unsigned int r_step=0; r_step<max_r_steps; r_step++) { std::cout << "\nBeginning Solve " << r_step+1 << std::endl; for (unsigned int t_step=0; t_step != n_timesteps; ++t_step) { std::cout << "\n\nSolving time step " << t_step << ", time = " << system.time << std::endl; system.solve(); system.postprocess(); // Advance to the next timestep in a transient problem system.time_solver->advance_timestep(); } //end stepping through time loop std::cout << "\n Refining the mesh..." << std::endl; // The \p ErrorVector is a particular \p StatisticsVector // for computing error information on a finite element mesh. ErrorVector error; if (indicator_type == "patch") { // The patch recovery estimator should give a // good estimate of the solution interpolation // error. PatchRecoveryErrorEstimator error_estimator; error_estimator.set_patch_reuse(false); //anisotropy trips up reuse error_estimator.estimate_error (system, error); } else if (indicator_type == "kelly") { // The Kelly error estimator is based on // an error bound for the Poisson problem // on linear elements, but is useful for // driving adaptive refinement in many problems KellyErrorEstimator error_estimator; error_estimator.estimate_error (system, error); } // Write out the error distribution if(write_error){ std::ostringstream ss; ss << r_step; #ifdef LIBMESH_HAVE_EXODUS_API std::string error_output = "error_"+ss.str()+".e"; #else std::string error_output = "error_"+ss.str()+".gmv"; #endif error.plot_error( error_output, mesh ); } // This takes the error in \p error and decides which elements // will be coarsened or refined. if(flag_by_elem_frac) mesh_refinement.flag_elements_by_elem_fraction(error); else mesh_refinement.flag_elements_by_error_fraction (error); // This call actually refines and coarsens the flagged // elements. mesh_refinement.refine_and_coarsen_elements(); // This call reinitializes the \p EquationSystems object for // the newly refined mesh. One of the steps in the // reinitialization is projecting the \p solution, // \p old_solution, etc... vectors from the old mesh to // the current one. equation_systems.reinit (); std::cout << "System has: " << equation_systems.n_active_dofs() << " degrees of freedom." << std::endl; } //end refinement loop //use that final refinement for (unsigned int t_step=0; t_step != n_timesteps; ++t_step) { std::cout << "\n\nSolving time step " << t_step << ", time = " << system.time << std::endl; system.solve(); system.postprocess(); Number QoI_computed = system.get_QoI_value("computed", 0); std::cout<< "Computed QoI is " << std::setprecision(17) << QoI_computed << std::endl; // Advance to the next timestep in a transient problem system.time_solver->advance_timestep(); } //end stepping through time loop #ifdef LIBMESH_HAVE_EXODUS_API for (unsigned int t_step=0; t_step != n_timesteps; ++t_step) { // Write out this timestep if we're requested to if ((t_step+1)%write_interval == 0) { std::ostringstream ex_file_name; std::ostringstream tplot_file_name; // We write the file in the ExodusII format. //ex_file_name << "out_" // << std::setw(3) // << std::setfill('0') // << std::right // << t_step+1 // << ".e"; tplot_file_name << "out_" << std::setw(3) << std::setfill('0') << std::right << t_step+1 << ".plt"; //ExodusII_IO(mesh).write_timestep(ex_file_name.str(), // equation_systems, // 1, /* This number indicates how many time steps // are being written to the file */ // system.time); exodusIO.write_timestep("output.exo", equation_systems, t_step+1, system.time); //outputs all timesteps in one file TecplotIO(mesh).write_equation_systems(tplot_file_name.str(), equation_systems); } } #endif // #ifdef LIBMESH_HAVE_EXODUS_API // All done. return 0; } //end main
// The main program int main(int argc, char** argv) { //for record-keeping std::cout << "Running: " << argv[0]; for (int i=1; i<argc; i++) std::cout << " " << argv[i]; std::cout << std::endl << std::endl; clock_t begin = std::clock(); // Initialize libMesh LibMeshInit init(argc, argv); // Parameters GetPot solverInfile("fem_system_params.in"); const bool transient = solverInfile("transient", false); unsigned int n_timesteps = solverInfile("n_timesteps", 1); const int nx = solverInfile("nx",100); const int ny = solverInfile("ny",100); const int nz = solverInfile("nz",100); GetPot infile("contamTrans.in"); //std::string find_mesh_here = infile("initial_mesh","mesh.exo"); bool doContinuation = infile("do_continuation",false); bool splitSuperAdj = infile("split_super_adjoint",true); //solve as single adjoint or two forwards int maxIter = infile("max_model_refinements",0); //maximum number of model refinements double refStep = infile("refinement_step",0.1); //additional proportion of domain refined per step //this refers to additional basis functions...number of elements will be more... double qoiErrorTol = infile("relative_error_tolerance",0.01); //stopping criterion //bool doDivvyMatlab = infile("do_divvy_in_Matlab",false); //output files to determine next refinement in Matlab bool avoid_sides = infile("avoid_sides",false); //DEBUG, whether to avoid sides while refining if(refStep*maxIter > 1) maxIter = round(ceil(1./refStep)); Mesh mesh(init.comm()); Mesh mesh2(init.comm()); //create mesh unsigned int dim; if(nz == 0){ //to check if oscillations happen in 2D as well... dim = 2; MeshTools::Generation::build_square(mesh, nx, ny, 0., 2300., 0., 1650., QUAD9); MeshTools::Generation::build_square(mesh2, nx, ny, 0., 2300., 0., 1650., QUAD9); }else{ dim = 3; MeshTools::Generation::build_cube(mesh, nx, ny, nz, 0., 2300., 0., 1650., 0., 100., HEX27); MeshTools::Generation::build_cube(mesh2, nx, ny, nz, 0., 2300., 0., 1650., 0., 100., HEX27); } mesh.print_info(); //DEBUG // Create an equation systems object. EquationSystems equation_systems (mesh); EquationSystems equation_systems_mix(mesh2); //name system ConvDiff_PrimarySys & system_primary = equation_systems.add_system<ConvDiff_PrimarySys>("ConvDiff_PrimarySys"); //for primary variables ConvDiff_AuxSys & system_aux = equation_systems.add_system<ConvDiff_AuxSys>("ConvDiff_AuxSys"); //for auxiliary variables ConvDiff_MprimeSys & system_mix = equation_systems_mix.add_system<ConvDiff_MprimeSys>("Diff_ConvDiff_MprimeSys"); //for superadj ConvDiff_PrimarySadjSys & system_sadj_primary = equation_systems.add_system<ConvDiff_PrimarySadjSys>("ConvDiff_PrimarySadjSys"); //for split superadj ConvDiff_AuxSadjSys & system_sadj_aux = equation_systems.add_system<ConvDiff_AuxSadjSys>("ConvDiff_AuxSadjSys"); //for split superadj //steady-state problem system_primary.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_primary)); system_aux.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_aux)); system_mix.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_mix)); system_sadj_primary.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_sadj_primary)); system_sadj_aux.time_solver = AutoPtr<TimeSolver>(new SteadySolver(system_sadj_aux)); libmesh_assert_equal_to (n_timesteps, 1); // Initialize the system equation_systems.init (); equation_systems_mix.init(); //initial guess for primary state read_initial_parameters(); system_primary.project_solution(initial_value, initial_grad, equation_systems.parameters); finish_initialization(); //nonlinear solver options NewtonSolver *solver_sadj_primary = new NewtonSolver(system_sadj_primary); system_sadj_primary.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_sadj_primary); solver_sadj_primary->quiet = solverInfile("solver_quiet", true); solver_sadj_primary->verbose = !solver_sadj_primary->quiet; solver_sadj_primary->max_nonlinear_iterations = solverInfile("max_nonlinear_iterations", 15); solver_sadj_primary->relative_step_tolerance = solverInfile("relative_step_tolerance", 1.e-3); solver_sadj_primary->relative_residual_tolerance = solverInfile("relative_residual_tolerance", 0.0); solver_sadj_primary->absolute_residual_tolerance = solverInfile("absolute_residual_tolerance", 0.0); NewtonSolver *solver_sadj_aux = new NewtonSolver(system_sadj_aux); system_sadj_aux.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_sadj_aux); solver_sadj_aux->quiet = solverInfile("solver_quiet", true); solver_sadj_aux->verbose = !solver_sadj_aux->quiet; solver_sadj_aux->max_nonlinear_iterations = solverInfile("max_nonlinear_iterations", 15); solver_sadj_aux->relative_step_tolerance = solverInfile("relative_step_tolerance", 1.e-3); solver_sadj_aux->relative_residual_tolerance = solverInfile("relative_residual_tolerance", 0.0); solver_sadj_aux->absolute_residual_tolerance = solverInfile("absolute_residual_tolerance", 0.0); NewtonSolver *solver_primary = new NewtonSolver(system_primary); system_primary.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_primary); solver_primary->quiet = solverInfile("solver_quiet", true); solver_primary->verbose = !solver_primary->quiet; solver_primary->max_nonlinear_iterations = solverInfile("max_nonlinear_iterations", 15); solver_primary->relative_step_tolerance = solverInfile("relative_step_tolerance", 1.e-3); solver_primary->relative_residual_tolerance = solverInfile("relative_residual_tolerance", 0.0); solver_primary->absolute_residual_tolerance = solverInfile("absolute_residual_tolerance", 0.0); NewtonSolver *solver_aux = new NewtonSolver(system_aux); system_aux.time_solver->diff_solver() = AutoPtr<DiffSolver>(solver_aux); solver_aux->quiet = solverInfile("solver_quiet", true); solver_aux->verbose = !solver_aux->quiet; solver_aux->max_nonlinear_iterations = solverInfile("max_nonlinear_iterations", 15); solver_aux->relative_step_tolerance = solverInfile("relative_step_tolerance", 1.e-3); solver_aux->relative_residual_tolerance = solverInfile("relative_residual_tolerance", 0.0); solver_aux->absolute_residual_tolerance = solverInfile("absolute_residual_tolerance", 0.0); solver_primary->require_residual_reduction = solverInfile("require_residual_reduction",true); solver_sadj_primary->require_residual_reduction = solverInfile("require_residual_reduction",true); solver_aux->require_residual_reduction = solverInfile("require_residual_reduction",true); solver_sadj_aux->require_residual_reduction = solverInfile("require_residual_reduction",true); //linear solver options solver_primary->max_linear_iterations = solverInfile("max_linear_iterations",10000); solver_primary->initial_linear_tolerance = solverInfile("initial_linear_tolerance",1.e-13); solver_primary->minimum_linear_tolerance = solverInfile("minimum_linear_tolerance",1.e-13); solver_primary->linear_tolerance_multiplier = solverInfile("linear_tolerance_multiplier",1.e-3); solver_aux->max_linear_iterations = solverInfile("max_linear_iterations",10000); solver_aux->initial_linear_tolerance = solverInfile("initial_linear_tolerance",1.e-13); solver_aux->minimum_linear_tolerance = solverInfile("minimum_linear_tolerance",1.e-13); solver_aux->linear_tolerance_multiplier = solverInfile("linear_tolerance_multiplier",1.e-3); solver_sadj_primary->max_linear_iterations = solverInfile("max_linear_iterations",10000); solver_sadj_primary->initial_linear_tolerance = solverInfile("initial_linear_tolerance",1.e-13); solver_sadj_primary->minimum_linear_tolerance = solverInfile("minimum_linear_tolerance",1.e-13); solver_sadj_primary->linear_tolerance_multiplier = solverInfile("linear_tolerance_multiplier",1.e-3); solver_sadj_aux->max_linear_iterations = solverInfile("max_linear_iterations",10000); solver_sadj_aux->initial_linear_tolerance = solverInfile("initial_linear_tolerance",1.e-13); solver_sadj_aux->minimum_linear_tolerance = solverInfile("minimum_linear_tolerance",1.e-13); solver_sadj_aux->linear_tolerance_multiplier = solverInfile("linear_tolerance_multiplier",1.e-3); /*if(doDivvyMatlab){ //DOF maps and such to help visualize std::ofstream output_global_dof("global_dof_map.dat"); for(unsigned int i = 0 ; i < system_mix.get_mesh().n_elem(); i++){ std::vector< dof_id_type > di; system_mix.get_dof_map().dof_indices(system_mix.get_mesh().elem(i), di); if(output_global_dof.is_open()){ output_global_dof << i << " "; for(unsigned int j = 0; j < di.size(); j++) output_global_dof << di[j] << " "; output_global_dof << "\n"; } } output_global_dof.close(); std::ofstream output_elem_cent("elem_centroids.dat"); for(unsigned int i = 0 ; i < system_mix.get_mesh().n_elem(); i++){ Point elem_cent = system_mix.get_mesh().elem(i)->centroid(); if(output_elem_cent.is_open()){ output_elem_cent << elem_cent(0) << " " << elem_cent(1) << "\n"; } } output_elem_cent.close(); }*/ //inverse dof map (elements in support of each node, assuming every 6 dofs belong to same node) std::vector<std::set<dof_id_type> > node_to_elem; node_to_elem.resize(round(system_mix.n_dofs()/6.)); for(unsigned int i = 0 ; i < system_mix.get_mesh().n_elem(); i++){ std::vector< dof_id_type > di; system_mix.get_dof_map().dof_indices(system_mix.get_mesh().elem(i), di); for(unsigned int j = 0; j < di.size(); j++) node_to_elem[round(floor(di[j]/6.))].insert(i); } int refIter = 0; double relError = 2.*qoiErrorTol; while(refIter <= maxIter && relError > qoiErrorTol){ if(!doContinuation){ //clear out previous solutions system_primary.solution->zero(); system_aux.solution->zero(); system_sadj_primary.solution->zero(); system_sadj_aux.solution->zero(); } system_mix.solution->zero(); clock_t begin_inv = std::clock(); system_primary.solve(); system_primary.clearQoI(); clock_t end_inv = std::clock(); clock_t begin_err_est = std::clock(); std::cout << "\n End primary solve, begin auxiliary solve..." << std::endl; system_aux.solve(); std::cout << "\n End auxiliary solve..." << std::endl; clock_t end_aux = std::clock(); system_primary.postprocess(); system_aux.postprocess(); system_sadj_primary.set_c_vals(system_primary.get_c_vals()); system_sadj_aux.set_auxc_vals(system_aux.get_auxc_vals()); equation_systems_mix.reinit(); //combine primary and auxiliary variables into psi DirectSolutionTransfer sol_transfer(init.comm()); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_c")), system_mix.variable(system_mix.variable_number("aux_c"))); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_zc")), system_mix.variable(system_mix.variable_number("aux_zc"))); sol_transfer.transfer(system_aux.variable(system_aux.variable_number("aux_fc")), system_mix.variable(system_mix.variable_number("aux_fc"))); AutoPtr<NumericVector<Number> > just_aux = system_mix.solution->clone(); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("c")), system_mix.variable(system_mix.variable_number("c"))); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("zc")), system_mix.variable(system_mix.variable_number("zc"))); sol_transfer.transfer(system_primary.variable(system_primary.variable_number("fc")), system_mix.variable(system_mix.variable_number("fc"))); if(!splitSuperAdj){ //solve super-adjoint as single adjoint system_mix.assemble_qoi_sides = true; //QoI doesn't involve sides std::cout << "\n~*~*~*~*~*~*~*~*~ adjoint solve start ~*~*~*~*~*~*~*~*~\n" << std::endl; std::pair<unsigned int, Real> adjsolve = system_mix.adjoint_solve(); std::cout << "number of iterations to solve adjoint: " << adjsolve.first << std::endl; std::cout << "final residual of adjoint solve: " << adjsolve.second << std::endl; std::cout << "\n~*~*~*~*~*~*~*~*~ adjoint solve end ~*~*~*~*~*~*~*~*~" << std::endl; NumericVector<Number> &dual_sol = system_mix.get_adjoint_solution(0); //DEBUG system_mix.assemble(); //calculate residual to correspond to solution //std::cout << "\n sadj norm: " << system_mix.calculate_norm(dual_sol, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 0, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 1, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 2, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 3, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 4, L2) << std::endl; //DEBUG //std::cout << system_mix.calculate_norm(dual_sol, 5, L2) << std::endl; //DEBUG }else{ //solve super-adjoint as system_mix.assemble(); //calculate residual to correspond to solution std::cout << "\n Begin primary super-adjoint solve...\n" << std::endl; system_sadj_primary.solve(); std::cout << "\n End primary super-adjoint solve, begin auxiliary super-adjoint solve...\n" << std::endl; system_sadj_aux.solve(); std::cout << "\n End auxiliary super-adjoint solve...\n" << std::endl; const std::string & adjoint_solution0_name = "adjoint_solution0"; system_mix.add_vector(adjoint_solution0_name, false, GHOSTED); system_mix.set_vector_as_adjoint(adjoint_solution0_name,0); NumericVector<Number> &eep = system_mix.add_adjoint_rhs(0); system_mix.set_adjoint_already_solved(true); NumericVector<Number> &dual_sol = system_mix.get_adjoint_solution(0); NumericVector<Number> &primal_sol = *system_mix.solution; dual_sol.swap(primal_sol); sol_transfer.transfer(system_sadj_aux.variable(system_sadj_aux.variable_number("sadj_aux_c")), system_mix.variable(system_mix.variable_number("aux_c"))); sol_transfer.transfer(system_sadj_aux.variable(system_sadj_aux.variable_number("sadj_aux_zc")), system_mix.variable(system_mix.variable_number("aux_zc"))); sol_transfer.transfer(system_sadj_aux.variable(system_sadj_aux.variable_number("sadj_aux_fc")), system_mix.variable(system_mix.variable_number("aux_fc"))); sol_transfer.transfer(system_sadj_primary.variable(system_sadj_primary.variable_number("sadj_c")), system_mix.variable(system_mix.variable_number("c"))); sol_transfer.transfer(system_sadj_primary.variable(system_sadj_primary.variable_number("sadj_zc")), system_mix.variable(system_mix.variable_number("zc"))); sol_transfer.transfer(system_sadj_primary.variable(system_sadj_primary.variable_number("sadj_fc")), system_mix.variable(system_mix.variable_number("fc"))); //std::cout << "\n sadj norm: " << system_mix.calculate_norm(primal_sol, L2) << std::endl; //DEBUG dual_sol.swap(primal_sol); //std::cout << "\n sadj norm: " << system_mix.calculate_norm(primal_sol, L2) << std::endl; //DEBUG //std::cout << system_sadj_primary.calculate_norm(*system_sadj_primary.solution, 0, L2) << std::endl; //DEBUG //std::cout << system_sadj_primary.calculate_norm(*system_sadj_primary.solution, 1, L2) << std::endl; //DEBUG //std::cout << system_sadj_primary.calculate_norm(*system_sadj_primary.solution, 2, L2) << std::endl; //DEBUG //std::cout << system_sadj_aux.calculate_norm(*system_sadj_aux.solution, 0, L2) << std::endl; //DEBUG //std::cout << system_sadj_aux.calculate_norm(*system_sadj_aux.solution, 1, L2) << std::endl; //DEBUG //std::cout << system_sadj_aux.calculate_norm(*system_sadj_aux.solution, 2, L2) << std::endl; //DEBUG } NumericVector<Number> &dual_solution = system_mix.get_adjoint_solution(0); /* NumericVector<Number> &primal_solution = *system_mix.solution; //DEBUG primal_solution.swap(dual_solution); //DEBUG ExodusII_IO(mesh).write_timestep("super_adjoint.exo", equation_systems, 1, / his number indicates how many time steps are being written to the file system_mix.time); //DEBUG primal_solution.swap(dual_solution); //DEBUG */ //adjoint-weighted residual AutoPtr<NumericVector<Number> > adjresid = system_mix.solution->zero_clone(); adjresid->pointwise_mult(*system_mix.rhs,dual_solution); adjresid->scale(-0.5); std::cout << "\n -0.5*M'_HF(psiLF)(superadj): " << adjresid->sum() << std::endl; //DEBUG //LprimeHF(psiLF) AutoPtr<NumericVector<Number> > LprimeHF_psiLF = system_mix.solution->zero_clone(); LprimeHF_psiLF->pointwise_mult(*system_mix.rhs,*just_aux); std::cout << " L'_HF(psiLF): " << LprimeHF_psiLF->sum() << std::endl; //DEBUG //QoI and error estimate std::cout << "QoI: " << std::setprecision(17) << system_primary.getQoI() << std::endl; std::cout << "QoI Error estimate: " << std::setprecision(17) << adjresid->sum()+LprimeHF_psiLF->sum() << std::endl; relError = fabs((adjresid->sum()+LprimeHF_psiLF->sum())/system_primary.getQoI()); //print out information std::cout << "Estimated absolute relative qoi error: " << relError << std::endl << std::endl; std::cout << "Estimated HF QoI: " << std::setprecision(17) << system_primary.getQoI()+adjresid->sum()+LprimeHF_psiLF->sum() << std::endl; clock_t end = clock(); std::cout << "Time so far: " << double(end-begin)/CLOCKS_PER_SEC << " seconds..." << std::endl; std::cout << "Time for inverse problem: " << double(end_inv-begin_inv)/CLOCKS_PER_SEC << " seconds..." << std::endl; std::cout << "Time for extra error estimate bits: " << double(end-begin_err_est)/CLOCKS_PER_SEC << " seconds..." << std::endl; std::cout << " Time to get auxiliary problems: " << double(end_aux-begin_err_est)/CLOCKS_PER_SEC << " seconds..." << std::endl; std::cout << " Time to get superadjoint: " << double(end-end_aux)/CLOCKS_PER_SEC << " seconds...\n" << std::endl; //proportion of domain refined at this iteration MeshBase::element_iterator elem_it = mesh.elements_begin(); const MeshBase::element_iterator elem_end = mesh.elements_end(); double numMarked = 0.; for (; elem_it != elem_end; ++elem_it){ Elem* elem = *elem_it; numMarked += elem->subdomain_id(); } std::cout << "Refinement fraction: " << numMarked/system_mix.get_mesh().n_elem() << std::endl << std::endl; //output at each iteration std::stringstream ss; ss << refIter; std::string str = ss.str(); std::string write_error_basis_blame = (infile("error_est_output_file_basis_blame", "error_est_breakdown_basis_blame")) + str + ".dat"; std::ofstream output2(write_error_basis_blame); for(unsigned int i = 0 ; i < adjresid->size(); i++){ if(output2.is_open()) output2 << (*adjresid)(i) + (*LprimeHF_psiLF)(i) << "\n"; } output2.close(); if(refIter < maxIter && relError > qoiErrorTol){ //if further refinement needed //collapse error contributions into nodes std::vector<std::pair<Number,dof_id_type> > node_errs(round(system_mix.n_dofs()/6.)); for(unsigned int node_num = 0; node_num < node_errs.size(); node_num++){ node_errs[node_num] = std::pair<Number,dof_id_type> (fabs((*adjresid)(6*node_num) + (*LprimeHF_psiLF)(6*node_num) + (*adjresid)(6*node_num+1) + (*LprimeHF_psiLF)(6*node_num+1) + (*adjresid)(6*node_num+2) + (*LprimeHF_psiLF)(6*node_num+2) + (*adjresid)(6*node_num+3) + (*LprimeHF_psiLF)(6*node_num+3) + (*adjresid)(6*node_num+4) + (*LprimeHF_psiLF)(6*node_num+4) + (*adjresid)(6*node_num+5) + (*LprimeHF_psiLF)(6*node_num+5)), node_num); } //find nodes contributing the most //double refPcnt = std::min((refIter+1)*refStep,1.); double refPcnt = std::min(refStep,1.); //additional refinement (compared to previous iteration) int cutoffLoc = round(node_errs.size()*refPcnt); std::sort(node_errs.begin(), node_errs.end()); std::reverse(node_errs.begin(), node_errs.end()); //find elements in support of worst offenders std::vector<dof_id_type> markMe; markMe.reserve(cutoffLoc*8); for(int i = 0; i < cutoffLoc; i++){ markMe.insert(markMe.end(), node_to_elem[node_errs[i].second].begin(), node_to_elem[node_errs[i].second].end()); } //mark those elements for refinement. for(int i = 0; i < markMe.size(); i++){ if(!avoid_sides || (avoid_sides && !mesh.elem(markMe[i])->on_boundary())) mesh.elem(markMe[i])->subdomain_id() = 1; //assuming HF regions marked with 1 } //to test whether assignment matches matlab's /*std::string stash_assign = "divvy_c_poke.txt"; std::ofstream output_dbg(stash_assign.c_str()); MeshBase::element_iterator elem_it = mesh.elements_begin(); const MeshBase::element_iterator elem_end = mesh.elements_end(); for (; elem_it != elem_end; ++elem_it){ Elem* elem = *elem_it; if(output_dbg.is_open()){ output_dbg << elem->id() << " " << elem->subdomain_id() << "\n"; } } output_dbg.close();*/ #ifdef LIBMESH_HAVE_EXODUS_API std::stringstream ss2; ss2 << refIter + 1; std::string str = ss2.str(); std::string write_divvy = "divvy" + str + ".exo"; ExodusII_IO (mesh).write_equation_systems(write_divvy,equation_systems); //DEBUG #endif // #ifdef LIBMESH_HAVE_EXODUS_API } refIter += 1; } std::string stash_assign = "divvy_final.txt"; std::ofstream output_dbg(stash_assign.c_str()); MeshBase::element_iterator elem_it = mesh.elements_begin(); const MeshBase::element_iterator elem_end = mesh.elements_end(); double numMarked = 0.; for (; elem_it != elem_end; ++elem_it){ Elem* elem = *elem_it; numMarked += elem->subdomain_id(); if(output_dbg.is_open()){ output_dbg << elem->id() << " " << elem->subdomain_id() << "\n"; } } output_dbg.close(); std::cout << "\nRefinement concluded..." << std::endl; std::cout << "Final refinement fraction: " << numMarked/system_mix.get_mesh().n_elem() << std::endl; std::cout << "Final estimated relative error: " << relError << std::endl; return 0; //done } //end main