24 #ifdef MANGO_PETSC_AVAILABLE
31 #ifdef MANGO_PETSC_AVAILABLE
34 PETSC_COMM_WORLD = MPI_COMM_SELF;
37 ierr = PetscInitialize(&(solver->
argc),&(solver->
argv),(
char *)0,
help);
38 if (ierr)
throw std::runtime_error(
"Error in PetscInitialize in mango::Package_petsc::optimize().");
39 ierr = PetscInitializeFortran();
42 TaoCreate(PETSC_COMM_SELF, &my_tao);
45 VecCreateSeq(PETSC_COMM_SELF, solver->
N_parameters, &tao_state_vec);
49 VecGetArray(tao_state_vec, &temp_array);
51 VecRestoreArray(tao_state_vec, &temp_array);
53 std::cout <<
"Here comes petsc vec for initial condition:" << std::endl;
54 VecView(tao_state_vec, PETSC_VIEWER_STDOUT_SELF);
56 TaoSetInitialVector(my_tao, tao_state_vec);
58 if (solver->
verbose > 0) std::cout <<
"PETSc has been initialized." << std::endl;
62 TaoSetType(my_tao, TAONM);
63 TaoSetObjectiveRoutine(my_tao, &mango_petsc_objective_function, (
void*)solver);
66 throw std::runtime_error(
"Should not get here! For the petsc_pounders algorithm, mango_optimize_least_squares_petsc should be called instead of mango_optimize_petsc.");
69 throw std::runtime_error(
"Should not get here!");
74 Vec lower_bounds_vec, upper_bounds_vec;
76 VecCreateSeq(PETSC_COMM_SELF, solver->
N_parameters, &lower_bounds_vec);
77 VecCreateSeq(PETSC_COMM_SELF, solver->
N_parameters, &upper_bounds_vec);
80 VecSetValue(lower_bounds_vec, j, solver->
lower_bounds[j], INSERT_VALUES);
81 VecSetValue(upper_bounds_vec, j, solver->
upper_bounds[j], INSERT_VALUES);
83 VecAssemblyBegin(lower_bounds_vec);
84 VecAssemblyBegin(upper_bounds_vec);
85 VecAssemblyEnd(lower_bounds_vec);
86 VecAssemblyEnd(upper_bounds_vec);
88 TaoSetVariableBounds(my_tao, lower_bounds_vec, upper_bounds_vec);
92 TaoSetFromOptions(my_tao);
94 if (solver->
verbose > 0) TaoView(my_tao, PETSC_VIEWER_STDOUT_SELF);
97 VecGetArray(tao_state_vec, &temp_array);
99 VecRestoreArray(tao_state_vec, &temp_array);
102 VecDestroy(&lower_bounds_vec);
103 VecDestroy(&upper_bounds_vec);
107 VecDestroy(&tao_state_vec);
112 throw std::runtime_error(
"Error! A PETSc algorithm was requested, but Mango was compiled without PETSc support.");
120 #ifdef MANGO_PETSC_AVAILABLE
121 PetscErrorCode mango::Package_petsc::mango_petsc_objective_function(Tao my_tao, Vec x, PetscReal* f_petsc,
void* user_context) {
123 const double* x_array;
124 VecGetArrayRead(x, &x_array);
134 VecRestoreArrayRead(x, &x_array);