<commit_before>#include "include/amici_solver.h"
#include "include/amici.h"
#include <cstdio>
#include <cstring>
#include <include/amici_model.h>
#include <include/rdata.h>
#include <include/tdata.h>
#include <include/udata.h>
#include <sundials/sundials_spgmr.h>
// TODO: don't use cvodes includes here
#include <cvodes/cvodes_spils.h>

/**
 * @brief setupAMIs initialises the ami memory object
 * @param[in] udata pointer to the user data object @type UserData
 * @param[in] tdata pointer to the temporary data object @type TempData
 * @param[in] model pointer to the model object @type Model
 * @return status flag indicating successful execution
 */
int Solver::setupAMI(const UserData *udata, TempData *tdata, Model *model) {
    int status;
    tdata->t = udata->tstart;

    if (model->initialize(udata, tdata) != AMICI_SUCCESS)
        goto freturn;

    /* Create AMIS object */
    if (udata->lmm != CV_ADAMS && udata->lmm != CV_BDF) {
        errMsgIdAndTxt("AMICI:mex:lmm", "Illegal value for lmm!");
        goto freturn;
    }
    if (udata->iter != CV_NEWTON && udata->iter != CV_FUNCTIONAL) {
        errMsgIdAndTxt("AMICI:mex:iter", "Illegal value for iter!");
        goto freturn;
    }
    ami_mem = AMICreate(udata->lmm, udata->iter);
    if (ami_mem == NULL)
        goto freturn;

    /* Initialize AMIS solver*/
    if (init(tdata->x, tdata->dx, udata->tstart) != AMICI_SUCCESS)
        goto freturn;

    /* Specify integration tolerances */
    if (AMISStolerances(RCONST(udata->rtol), RCONST(udata->atol)) !=
        AMICI_SUCCESS)
        goto freturn;

    /* Set optional inputs */
    if (AMISetErrHandlerFn() != AMICI_SUCCESS)
        goto freturn;

    /* attaches userdata*/
    if (AMISetUserData(tdata) != AMICI_SUCCESS)
        goto freturn;

    /* specify maximal number of steps */
    if (AMISetMaxNumSteps(udata->maxsteps) != AMICI_SUCCESS)
        goto freturn;

    /* activates stability limit detection */
    if (AMISetStabLimDet(udata->stldet) != AMICI_SUCCESS)
        goto freturn;

    if (rootInit(model->ne) != AMICI_SUCCESS)
        goto freturn;

    status = setLinearSolver(udata, model);
    if (status != AMICI_SUCCESS)
        goto freturn;

    if (udata->sensi >= AMICI_SENSI_ORDER_FIRST) {
        
        if (model->nx > 0) {

            /* initialise sensitivities, this can either be user provided or
             * come from the model definition */
            realtype *sx_tmp;

            if (!udata->sx0data) {
                if (model->fsx0(tdata->sx, tdata->x, tdata->dx, tdata) !=
                    AMICI_SUCCESS)
                    goto freturn;
            } else {
                for (int ip = 0; ip < udata->nplist; ip++) {
                    sx_tmp = NV_DATA_S(tdata->sx[ip]);
                    if (!sx_tmp)
                        goto freturn;
                    for (int ix = 0; ix < model->nx; ix++) {
                        sx_tmp[ix] =
                            (realtype)udata->sx0data[ix + model->nx * ip];
                    }
                }
            }

            if (model->fsdx0(tdata->sdx, tdata->x, tdata->dx, tdata) !=
                AMICI_SUCCESS)
                goto freturn;
            
            if (udata->sensi_meth == AMICI_SENSI_FSA) {
                
                /* Activate sensitivity calculations */
                if (sensInit1(tdata->sx, tdata->sdx, udata) != AMICI_SUCCESS)
                    goto freturn;

                /* Set sensitivity analysis optional inputs */
                if (AMISetSensParams(tdata->p, udata->pbar, udata->plist) !=
                    AMICI_SUCCESS)
                    goto freturn;

                if (AMISetSensErrCon(TRUE) != AMICI_SUCCESS)
                    goto freturn;

                if (AMISensEEtolerances() != AMICI_SUCCESS)
                    goto freturn;
            }
        }

        if (udata->sensi_meth == AMICI_SENSI_ASA) {
            if (model->nx > 0) {
                /* Allocate space for the adjoint computation */
                if (AMIAdjInit(udata->maxsteps, udata->interpType) !=
                    AMICI_SUCCESS)
                    goto freturn;
            }
        }
    }

    if (AMISetId(model) != AMICI_SUCCESS)
        goto freturn;

    if (AMISetSuppressAlg(TRUE) != AMICI_SUCCESS)
        goto freturn;

    return AMICI_SUCCESS;

freturn:
    if (ami_mem)
        AMIFree();
    return AMICI_ERROR_SETUP;
}

/**
 * setupAMIB initialises the AMI memory object for the backwards problem
 * @param[in] udata pointer to the user data object @type UserData
 * @param[in] tdata pointer to the temporary data object @type TempData
 * @param[in] model pointer to the model object @type Model<commit_msg>add code for IC calculation for DAEs but comment out for the moment<commit_after>#include "include/amici_solver.h"
#include "include/amici.h"
#include <cstdio>
#include <cstring>
#include <include/amici_model.h>
#include <include/rdata.h>
#include <include/tdata.h>
#include <include/udata.h>
#include <sundials/sundials_spgmr.h>
// TODO: don't use cvodes includes here
#include <cvodes/cvodes_spils.h>

/**
 * @brief setupAMIs initialises the ami memory object
 * @param[in] udata pointer to the user data object @type UserData
 * @param[in] tdata pointer to the temporary data object @type TempData
 * @param[in] model pointer to the model object @type Model
 * @return status flag indicating successful execution
 */
int Solver::setupAMI(const UserData *udata, TempData *tdata, Model *model) {
    int status;
    tdata->t = udata->tstart;

    if (model->initialize(udata, tdata) != AMICI_SUCCESS)
        goto freturn;

    /* Create AMIS object */
    if (udata->lmm != CV_ADAMS && udata->lmm != CV_BDF) {
        errMsgIdAndTxt("AMICI:mex:lmm", "Illegal value for lmm!");
        goto freturn;
    }
    if (udata->iter != CV_NEWTON && udata->iter != CV_FUNCTIONAL) {
        errMsgIdAndTxt("AMICI:mex:iter", "Illegal value for iter!");
        goto freturn;
    }
    ami_mem = AMICreate(udata->lmm, udata->iter);
    if (ami_mem == NULL)
        goto freturn;

    /* Initialize AMIS solver*/
    if (init(tdata->x, tdata->dx, udata->tstart) != AMICI_SUCCESS)
        goto freturn;

    /* Specify integration tolerances */
    if (AMISStolerances(RCONST(udata->rtol), RCONST(udata->atol)) !=
        AMICI_SUCCESS)
        goto freturn;

    /* Set optional inputs */
    if (AMISetErrHandlerFn() != AMICI_SUCCESS)
        goto freturn;

    /* attaches userdata*/
    if (AMISetUserData(tdata) != AMICI_SUCCESS)
        goto freturn;

    /* specify maximal number of steps */
    if (AMISetMaxNumSteps(udata->maxsteps) != AMICI_SUCCESS)
        goto freturn;

    /* activates stability limit detection */
    if (AMISetStabLimDet(udata->stldet) != AMICI_SUCCESS)
        goto freturn;

    if (rootInit(model->ne) != AMICI_SUCCESS)
        goto freturn;

    status = setLinearSolver(udata, model);
    if (status != AMICI_SUCCESS)
        goto freturn;

    if (udata->sensi >= AMICI_SENSI_ORDER_FIRST) {
        
        if (model->nx > 0) {

            /* initialise sensitivities, this can either be user provided or
             * come from the model definition */
            realtype *sx_tmp;

            if (!udata->sx0data) {
                if (model->fsx0(tdata->sx, tdata->x, tdata->dx, tdata) !=
                    AMICI_SUCCESS)
                    goto freturn;
            } else {
                for (int ip = 0; ip < udata->nplist; ip++) {
                    sx_tmp = NV_DATA_S(tdata->sx[ip]);
                    if (!sx_tmp)
                        goto freturn;
                    for (int ix = 0; ix < model->nx; ix++) {
                        sx_tmp[ix] =
                            (realtype)udata->sx0data[ix + model->nx * ip];
                    }
                }
            }

            if (model->fsdx0(tdata->sdx, tdata->x, tdata->dx, tdata) !=
                AMICI_SUCCESS)
                goto freturn;
            
            if (udata->sensi_meth == AMICI_SENSI_FSA) {
                
                /* Activate sensitivity calculations */
                if (sensInit1(tdata->sx, tdata->sdx, udata) != AMICI_SUCCESS)
                    goto freturn;

                /* Set sensitivity analysis optional inputs */
                if (AMISetSensParams(tdata->p, udata->pbar, udata->plist) !=
                    AMICI_SUCCESS)
                    goto freturn;

                if (AMISetSensErrCon(TRUE) != AMICI_SUCCESS)
                    goto freturn;

                if (AMISensEEtolerances() != AMICI_SUCCESS)
                    goto freturn;
            }
        }

        if (udata->sensi_meth == AMICI_SENSI_ASA) {
            if (model->nx > 0) {
                /* Allocate space for the adjoint computation */
                if (AMIAdjInit(udata->maxsteps, udata->interpType) !=
                    AMICI_SUCCESS)
                    goto freturn;
            }
        }
    }

    if (AMISetId(model) != AMICI_SUCCESS)
        goto freturn;

    if (AMISetSuppressAlg(TRUE) != AMICI_SUCCESS)
        goto freturn;
    
    /* calculate consistent DAE initial conditions (no effect for ODE) */
    //if(udata->nt>1){
    //    if (AMICalcIC(udata->ts[1]) != AMICI_SUCCESS)
    //        goto freturn;
    //}

    return AMICI_SUCCESS;

freturn:
    if (ami_mem)
        AMIFree();
    return AMICI_ERROR_SETUP;
}

/**
 * setupAMIB initialises the AMI memory object for the backwards problem
 * @param[in] udata pointer to the user data object @type UserData
 * @param[in] tdata pointer to the temporary data object @type TempData
 * @param[in] model pointer to the model object @type Model
{'fraction_non_alphanumeric': 0.11237113402061856, 'fraction_numerical': 0.002061855670103093, 'mean_word_length': 1.9567205120390125, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 114, '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': '74086', 'n_tokens_mistral': 3200, 'n_tokens_neox': 3036, 'n_words': 750}