PyObject* AuxFunc(double t, double *x, double *p) { PyObject *OutObj = NULL; PyObject *AuxOut = NULL; double *ftemp = NULL; int i; import_array(); if( (gIData == NULL) || (gIData->isInitBasic == 0) || (gIData->nAuxVars == 0) ) { Py_INCREF(Py_None); return Py_None; } else if( (gIData->nExtInputs > 0) && (gIData->isInitExtInputs == 0) ) { Py_INCREF(Py_None); return Py_None; } else { OutObj = PyTuple_New(1); assert(OutObj); ftemp = (double *)PyMem_Malloc((gIData->nAuxVars)*sizeof(double)); assert(ftemp); if( gIData->nExtInputs > 0 ) { FillCurrentExtInputValues( gIData, t ); } auxvars(gIData->phaseDim, gIData->phaseDim, t, x, p, ftemp, gIData->extraSpaceSize, gIData->gExtraSpace, gIData->nExtInputs, gIData->gCurrentExtInputVals); npy_intp dims[1] = {gIData->nAuxVars}; AuxOut = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, ftemp); if(AuxOut) { PyArray_UpdateFlags((PyArrayObject *)AuxOut, NPY_ARRAY_CARRAY | NPY_ARRAY_OWNDATA); PyTuple_SetItem(OutObj, 0, AuxOut); return OutObj; } else { PyMem_Free(ftemp); Py_INCREF(Py_None); return Py_None; } return OutObj; } }
/* Performs calculation of auxilliary variables after integration, event finding are complete. This is the only place where memory is allocated outside of an "Init" function. */ void AuxVarCalc( IData *GS ) { int i; assert(GS); if( (GS->nAuxVars > 0) && (GS->pointsIdx > 0 ) ) { /* Allocate space for aux variable calculations if this is the first time we have done aux calculations */ if( GS->gAuxPoints == NULL ) { GS->gAuxPoints = (double **)PyMem_Malloc(GS->pointsIdx*sizeof(double *)); assert(GS->gAuxPoints); for( i = 0; i < GS->pointsIdx; i++ ) { GS->gAuxPoints[i] = (double *)PyMem_Malloc(GS->nAuxVars*sizeof(double)); assert(GS->gAuxPoints[i]); } } /* Otherwise, realloc space for the additional pointers, PyMem_Malloc space for the new aux points */ else { double **temp = NULL; temp = (double **)PyMem_Realloc(GS->gAuxPoints,GS->pointsIdx*sizeof(double *)); assert(temp); GS->gAuxPoints = temp; for( i = GS->auxIdx; i < GS->pointsIdx; i++ ) { GS->gAuxPoints[i] = (double *)PyMem_Malloc(GS->nAuxVars*sizeof(double)); assert(GS->gAuxPoints[i]); } } /* Perform calculation on each integrated point */ for( i = GS->auxIdx; i < GS->pointsIdx; i++ ) { if( GS->nExtInputs > 0 ) { FillCurrentExtInputValues(GS, GS->gTimeV[i]); } auxvars((unsigned) GS->phaseDim, (unsigned) GS->paramDim, GS->gTimeV[i], GS->gPoints[i], GS->gParams, GS->gAuxPoints[i], (unsigned) GS->extraSpaceSize, GS->gExtraSpace, (unsigned) GS->nExtInputs, GS->gCurrentExtInputVals); } GS->auxIdx = GS->pointsIdx; } }