Hi, I have try to solve a nonlinear problem which has a very unpleasant Jacobian matrix. So, I would like to use automatic differentiation. There is an example how to use Sacado, but I am not still absolutely sure how to use this. (step-33) Let's assume that I have a nonlinear Poisson problem, for example
div( grad phi) - exp(phi) phi = 0
with Dirichlet and Neumann boundary conditions. I try to solve this using Newton's method
F'(u_k,delta u) = - F(u_k)
u_k+1 = u_k+delta u
When I am assembling the system matrix and right hand side, I am doing the following:
std::vector<types::global_dof_index> dof_indices;
std::vector<Sacado::Fad::DFad<double> > independent_local_dof_values(dofs_per_cell);
std::vector<double> derivatives (dofs_per_cell);
for(; cell!=endc; ++cell){
fe_values.reinit (cell);
cell->get_dof_indices (dof_indices);
for (unsigned int i=0; i<dofs_per_cell; ++i){
independent_local_dof_values[i] = old_solution(dof_indices[i]);
for (unsigned int i=0; i<dofs_per_cell; ++i){ independent_local_dof_values[i].diff (i, dofs_per_cell);
for (unsigned int i=0; i<fe_values.dofs_per_cell; ++i){
Sacado::Fad::DFad<double> F_i = 0;
for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point){
for (unsigned int d=0; d<dim; d++){
F_i -= old_solution(dof_indices[i])*fe_v.shape_grad_component(i, point, component_i)[d] *
independent_local_dof_values[i]*fe_v.shape_value_component(i, point, component_i) -
std::exp(old_solution(dof_indices[i])*fe_v.shape_grad_component(i, point, component_i)[d]) *
old_solution(dof_indices[i])*fe_v.shape_value_component(i, point, component_i) *
independent_local_dof_values[i]*fe_v.shape_value_component(i, point, component_i) *
fe_v.JxW(point);
}
}
for (unsigned int k=0; k<dofs_per_cell; ++k)
derivatives[k] = F_i.fastAccessDx(k);
system_matrix.add(dof_indices[i], dof_indices, derivatives);
right_hand_side(dof_indices[i]) -= F_i.val();
}
}
I feel a little bit insecure how to do this, but this pretty much what I should do?
old_solution is a vector which contains dof's from the previous iteration.
-Heikki