<commit_before>/* Siconos-Numerics, Copyright INRIA 2005-2012.
 * Siconos is a program dedicated to modeling, simulation and control
 * of non smooth dynamical systems.
 * Siconos is a free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
2 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * Siconos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Siconos; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact: <NAME>, <EMAIL>
 */

#include "NonSmoothNewton.h"
#include "NCP_Solvers.h"
#include "fc3d_Solvers.h"
#include <stdlib.h>
#include <stdio.h>
#include "FischerBurmeister.h"
#include "SiconosBlas.h"
#include "AlartCurnierGenerated.h"
#include "fc3d_GlockerFischerBurmeister_functions.h"
#include "op3x3.h"


#define DEBUG_CHECK


#define OPTI_RHO
/* #define DEBUG_MESSAGES */
/* #define DEBUG_STDOUT */
#include "debug.h"

static computeNonsmoothFunction  Function = NULL;
static NewtonFunctionPtr F = NULL;
static NewtonFunctionPtr jacobianF = NULL;
static UpdateSolverPtr updateSolver = NULL;
static PostSolverPtr postSolver = NULL;
static FreeSolverPtr freeSolver = NULL;

/* size of a block */
static int Fsize;
void fc3d_AC_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem, SolverOptions * options);
void fc3d_AC_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem, SolverOptions * options)
{
  /*
    In initialize, these operators are "connected" to their corresponding static variables, that will be used to build local problem
    for each considered contact.
    Local problem is built during call to update (which depends on the storage type for M).
  */

  /* localFC3D = localproblem; */
  /* globalFC3D = problem; */
  DEBUG_PRINTF("fc3d_AC_initialize starts with options->iparam[10] = %i\n", options->iparam[10]);
  if (options->iparam[10] == 0 )
  {
    Function = &(computeAlartCurnierSTD);
  }
  else if (options->iparam[10] == 1 )
  {
    Function = &(computeAlartCurnierJeanMoreau);
  }
  else if (options->iparam[10] == 2 )
  {
    Function = &(fc3d_AlartCurnierFunctionGenerated);
  }
  else if (options->iparam[10] == 3 )
  {
    Function = &fc3d_AlartCurnierJeanMoreauFunctionGenerated;;
  }
  else if (options->iparam[10] == 4 )
  {
    Function = NULL;
  }


/* #ifdef AC_STD */
/* computeNonsmoothFunction  Function = &(computeAlartCurnierSTD); */
/* #endif */
/* #ifdef AC_JeanMoreau */
/* computeNonsmoothFunction  Function = &(computeAlartCurnierJeanMoreau); */
/* #endif */

/* // computeAlartCurnier[JeanMoreau] == AC_Generated */

/* #ifdef AC_Generated */
/* computeNonsmoothFunction  Function = &(fc3d_AlartCurnierFunctionGenerated); */
/* #endif */

/* // HandMade not done */
/* #ifdef AC_HandMade */
/* computeNonsmoothFunction  Function = &(fc3d_AlartCurnierFunctionHandMade); */
/* #endif */

}

void fc3d_AC_free(void);

void fc3d_AC_free()
{
}
void fc3d_AC_post(int contact, double* reaction);
void fc3d_AC_post(int contact, double* reaction)
{
  /* This function is required in the interface but useless in Alart-Curnier case */
}

void fc3d_onecontact_nonsmooth_Newton_solvers_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem,    SolverOptions * localsolver_options)
{

  /*
     Initialize solver (Connect F and its jacobian, set local size ...) according to the chosen formulation.
  */

  /* Alart-Curnier formulation */
  if (localsolver_options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC)
  {
    fc3d_AC_initialize(problem, localproblem,localsolver_options);
    /* Fsize = 3; */
    /* F = &F_AC; */
    /* jacobianF = &jacobianF_AC; */
    /*     updateSolver = &fc3d_AC_update; */
    postSolver = &fc3d_AC_post;
    freeSolver = (FreeSolverPtr)&fc3d_AC_free;

  }
  else if (localsolver_options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC_GP)
  {
    fc3d_AC_initialize(problem, localproblem,localsolver_options);
    /* Fsize = 3; */
    /* F = &F_AC; */
    /* jacobianF = &jacobianF_AC; */
    /*     updateSolver = &fc3d_AC_update; */
    postSolver = &fc3d_AC_post;
    freeSolver = (FreeSolverPtr)&fc3d_AC_free;

  }




  /* Glocker formulation - Fischer-Burmeister function used in Newton */
  else if (localsolver_options->solverId == SICONOS_FRICTION_3D_NCPGlockerFBNewton)
  {
    Fsize = 5;
    NCPGlocker_initialize(problem, localproblem);
    F = &F_GlockerFischerBurmeister;
    jacobianF = &jacobianF_GlockerFischerBurmeister;
    /*     updateSolver = &NCPGlocker_update; */
    postSolver = &NCPGlocker_post;
    freeSolver = (FreeSolverPtr)&NCPGlocker_free;
  }
  else
  {
    fprintf(stderr, "Numerics, fc3d_nsgs failed. Unknown formulation type.\n");
    exit(EXIT_FAILURE);
  }
}

int fc3d_onecontact_nonsmooth_Newton_solvers_solve(FrictionContactProblem* localproblem, double* reaction, SolverOptions * options)
{

  /*  (*updateSolver)(contact, reaction); */
  double * reactionBlock = reaction;

  int * iparam = options->iparam;
  double * dparam = options->dparam;

  int info;
  if (options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC)
  {
    info = fc3d_onecontact_nonsmooth_Newton_solvers_solve_direct(localproblem, reactionBlock, iparam, dparam);
  }
  else if (options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC_GP)
  {
    info = fc3d_onecontact_nonsmooth_Newton_solvers_solve_damped(localproblem, reactionBlock, iparam, dparam);
  }
  else
  {
    info = nonSmoothDirectNewton(Fsize, reactionBlock, &F, &jacobianF, iparam, dparam);
  }
  if (info > 0)
  {
    if (verbose > 0)
    {
      printf("Numerics, fc3d_onecontact_nonsmooth_Newton_solvers_solve, warning. reached max. number of iterations without convergence for contact %i. Residual = %12.8e\n", options->iparam[4], dparam[1]);
      /* note : exit on failure should be done in DefaultCheckSolverOutput */
    }
  }
  return info;
  /*  (*postSolver)(contact,reaction); */
}

void fc3d_onecontact_nonsmooth_Newton_solvers_free(FrictionContactProblem* localproblem)
{
  F = NULL;
  jacobianF = NULL;
  updateSolver = NULL;
  postSolver = NULL;
  (*freeSolver)();
}
<commit_msg>[numerics][fc3d] add the max. number of iterations in the output when the onecontact newton solver reached the max number of iteration<commit_after>/* Siconos-Numerics, Copyright INRIA 2005-2012.
 * Siconos is a program dedicated to modeling, simulation and control
 * of non smooth dynamical systems.
 * Siconos is a free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
2 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * Siconos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Siconos; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact: <NAME>, <EMAIL>
 */

#include "NonSmoothNewton.h"
#include "NCP_Solvers.h"
#include "fc3d_Solvers.h"
#include <stdlib.h>
#include <stdio.h>
#include "FischerBurmeister.h"
#include "SiconosBlas.h"
#include "AlartCurnierGenerated.h"
#include "fc3d_GlockerFischerBurmeister_functions.h"
#include "op3x3.h"


#define DEBUG_CHECK


#define OPTI_RHO
/* #define DEBUG_MESSAGES */
/* #define DEBUG_STDOUT */
#include "debug.h"

static computeNonsmoothFunction  Function = NULL;
static NewtonFunctionPtr F = NULL;
static NewtonFunctionPtr jacobianF = NULL;
static UpdateSolverPtr updateSolver = NULL;
static PostSolverPtr postSolver = NULL;
static FreeSolverPtr freeSolver = NULL;

/* size of a block */
static int Fsize;
void fc3d_AC_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem, SolverOptions * options);
void fc3d_AC_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem, SolverOptions * options)
{
  /*
    In initialize, these operators are "connected" to their corresponding static variables, that will be used to build local problem
    for each considered contact.
    Local problem is built during call to update (which depends on the storage type for M).
  */

  /* localFC3D = localproblem; */
  /* globalFC3D = problem; */
  DEBUG_PRINTF("fc3d_AC_initialize starts with options->iparam[10] = %i\n", options->iparam[10]);
  if (options->iparam[10] == 0 )
  {
    Function = &(computeAlartCurnierSTD);
  }
  else if (options->iparam[10] == 1 )
  {
    Function = &(computeAlartCurnierJeanMoreau);
  }
  else if (options->iparam[10] == 2 )
  {
    Function = &(fc3d_AlartCurnierFunctionGenerated);
  }
  else if (options->iparam[10] == 3 )
  {
    Function = &fc3d_AlartCurnierJeanMoreauFunctionGenerated;;
  }
  else if (options->iparam[10] == 4 )
  {
    Function = NULL;
  }


/* #ifdef AC_STD */
/* computeNonsmoothFunction  Function = &(computeAlartCurnierSTD); */
/* #endif */
/* #ifdef AC_JeanMoreau */
/* computeNonsmoothFunction  Function = &(computeAlartCurnierJeanMoreau); */
/* #endif */

/* // computeAlartCurnier[JeanMoreau] == AC_Generated */

/* #ifdef AC_Generated */
/* computeNonsmoothFunction  Function = &(fc3d_AlartCurnierFunctionGenerated); */
/* #endif */

/* // HandMade not done */
/* #ifdef AC_HandMade */
/* computeNonsmoothFunction  Function = &(fc3d_AlartCurnierFunctionHandMade); */
/* #endif */

}

void fc3d_AC_free(void);

void fc3d_AC_free()
{
}
void fc3d_AC_post(int contact, double* reaction);
void fc3d_AC_post(int contact, double* reaction)
{
  /* This function is required in the interface but useless in Alart-Curnier case */
}

void fc3d_onecontact_nonsmooth_Newton_solvers_initialize(FrictionContactProblem* problem, FrictionContactProblem* localproblem,    SolverOptions * localsolver_options)
{

  /*
     Initialize solver (Connect F and its jacobian, set local size ...) according to the chosen formulation.
  */

  /* Alart-Curnier formulation */
  if (localsolver_options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC)
  {
    fc3d_AC_initialize(problem, localproblem,localsolver_options);
    /* Fsize = 3; */
    /* F = &F_AC; */
    /* jacobianF = &jacobianF_AC; */
    /*     updateSolver = &fc3d_AC_update; */
    postSolver = &fc3d_AC_post;
    freeSolver = (FreeSolverPtr)&fc3d_AC_free;

  }
  else if (localsolver_options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC_GP)
  {
    fc3d_AC_initialize(problem, localproblem,localsolver_options);
    /* Fsize = 3; */
    /* F = &F_AC; */
    /* jacobianF = &jacobianF_AC; */
    /*     updateSolver = &fc3d_AC_update; */
    postSolver = &fc3d_AC_post;
    freeSolver = (FreeSolverPtr)&fc3d_AC_free;

  }




  /* Glocker formulation - Fischer-Burmeister function used in Newton */
  else if (localsolver_options->solverId == SICONOS_FRICTION_3D_NCPGlockerFBNewton)
  {
    Fsize = 5;
    NCPGlocker_initialize(problem, localproblem);
    F = &F_GlockerFischerBurmeister;
    jacobianF = &jacobianF_GlockerFischerBurmeister;
    /*     updateSolver = &NCPGlocker_update; */
    postSolver = &NCPGlocker_post;
    freeSolver = (FreeSolverPtr)&NCPGlocker_free;
  }
  else
  {
    fprintf(stderr, "Numerics, fc3d_nsgs failed. Unknown formulation type.\n");
    exit(EXIT_FAILURE);
  }
}

int fc3d_onecontact_nonsmooth_Newton_solvers_solve(FrictionContactProblem* localproblem, double* reaction, SolverOptions * options)
{

  /*  (*updateSolver)(contact, reaction); */
  double * reactionBlock = reaction;

  int * iparam = options->iparam;
  double * dparam = options->dparam;

  int info;
  if (options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC)
  {
    info = fc3d_onecontact_nonsmooth_Newton_solvers_solve_direct(localproblem, reactionBlock, iparam, dparam);
  }
  else if (options->solverId == SICONOS_FRICTION_3D_ONECONTACT_NSN_AC_GP)
  {
    info = fc3d_onecontact_nonsmooth_Newton_solvers_solve_damped(localproblem, reactionBlock, iparam, dparam);
  }
  else
  {
    info = nonSmoothDirectNewton(Fsize, reactionBlock, &F, &jacobianF, iparam, dparam);
  }
  if (info > 0)
  {
    if (verbose > 0)
    {
      printf("Numerics, fc3d_onecontact_nonsmooth_Newton_solvers_solve, warning. reached max. number of iterations (%i) without convergence for contact %i. Residual = %12.8e\n", options->iparam[0], options->iparam[4], dparam[1]);
      /* note : exit on failure should be done in DefaultCheckSolverOutput */
    }
  }
  return info;
  /*  (*postSolver)(contact,reaction); */
}

void fc3d_onecontact_nonsmooth_Newton_solvers_free(FrictionContactProblem* localproblem)
{
  F = NULL;
  jacobianF = NULL;
  updateSolver = NULL;
  postSolver = NULL;
  (*freeSolver)();
}

{'fraction_non_alphanumeric': 0.10627177700348432, 'fraction_numerical': 0.013331313437357975, 'mean_word_length': 4.334545454545455, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, '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': '17986', 'n_tokens_mistral': 4761, 'n_tokens_neox': 4533, 'n_words': 1078}