<commit_before>#ifndef _peclet_solve_nonlinear_problem_h_
#define _peclet_solve_nonlinear_problem_h_


/*! Setup and solve a Newton iteration */
template<int dim>
void Peclet<dim>::step_newton()
{
    this->old_newton_solution = this->newton_solution;
    
    this->assemble_system();

    this->apply_boundary_values_and_constraints();

    this->solve_linear_system();

    this->newton_solution -= this->newton_residual;
}

/*! Iterate the Newton method to solve the nonlinear problem */
template<int dim>
bool Peclet<dim>::solve_nonlinear_problem()
{
    this->newton_solution = this->solution;
    
    bool converged = false;

    unsigned int i;
    
    double old_norm_residual = 1.e32;
    
    double scale = this->newton_solution.l2_norm()/this->newton_solution.size();
    
    for (i = 0; i < this->params.nonlinear_solver.max_iterations; ++i)
    {
        this->step_newton();
        
        Output::write_solution_to_vtk( // @todo Debugging
            "newton_residual.vtk",
            this->dof_handler,
            this->newton_residual);

        Output::write_solution_to_vtk( // @todo Debugging
            "newton_solution.vtk",
            this->dof_handler,
            this->newton_solution);
        
        double norm_residual = scale*this->newton_residual.l2_norm()/this->newton_residual.size();
        
        std::cout << "Newton iteration normalized L2 norm residual = " << norm_residual << std::endl;
        
        if (norm_residual > old_norm_residual)
        {
            converged = false;
            
            std::cout << "Newton iteration diverged." << std::endl;
            
            Output::write_solution_to_vtk( // @todo Debugging
                "diverged_newton_solution.vtk",
                this->dof_handler,
                this->newton_solution);
                
            if (this->time_step_size == this->params.time.min_step_size)
            {
                assert(converged);
            }
            
            return converged;
        }
        
        old_norm_residual = norm_residual;
        
        if (norm_residual < this->params.nonlinear_solver.tolerance)
        {
            converged = true;
            break;
        }

    }

    if ((this->time_step_size > this->params.time.min_step_size) & !converged)
    {
        return converged;<commit_msg>Scaling the tolerance with the L2 norm of the current Newton iteration solution. I don't know yet if this is what Danaila did.<commit_after>#ifndef _peclet_solve_nonlinear_problem_h_
#define _peclet_solve_nonlinear_problem_h_


/*! Setup and solve a Newton iteration */
template<int dim>
void Peclet<dim>::step_newton()
{
    this->old_newton_solution = this->newton_solution;
    
    this->assemble_system();

    this->apply_boundary_values_and_constraints();

    this->solve_linear_system();

    this->newton_solution -= this->newton_residual;
}

/*! Iterate the Newton method to solve the nonlinear problem */
template<int dim>
bool Peclet<dim>::solve_nonlinear_problem()
{
    this->newton_solution = this->solution;
    
    bool converged = false;

    unsigned int i;
    
    double old_norm_residual = 1.e32;
    
    for (i = 0; i < this->params.nonlinear_solver.max_iterations; ++i)
    {
        this->step_newton();
        
        Output::write_solution_to_vtk( // @todo Debugging
            "newton_residual.vtk",
            this->dof_handler,
            this->newton_residual);

        Output::write_solution_to_vtk( // @todo Debugging
            "newton_solution.vtk",
            this->dof_handler,
            this->newton_solution);
        
        double norm_residual = this->newton_residual.l2_norm();
        
        std::cout << "Newton iteration normalized L2 norm residual = " << norm_residual << std::endl;
        
        if (norm_residual > old_norm_residual)
        {
            converged = false;
            
            std::cout << "Newton iteration diverged." << std::endl;
            
            Output::write_solution_to_vtk( // @todo Debugging
                "diverged_newton_solution.vtk",
                this->dof_handler,
                this->newton_solution);
                
            if (this->time_step_size == this->params.time.min_step_size)
            {
                assert(converged);
            }
            
            return converged;
        }
        
        old_norm_residual = norm_residual;
        
        if (norm_residual < this->newton_solution.l2_norm()*this->params.nonlinear_solver.tolerance)
        {
            converged = true;
            break;
        }

    }

    if ((this->time_step_size > this->params.time.min_step_size) & !converged)
    {
        return converged;
{'fraction_non_alphanumeric': 0.10291631445477599, 'fraction_numerical': 0.0031699070160608623, 'mean_word_length': 2.077373211963589, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549982', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1434, 'n_words': 248}