/* Main Simulation File */
#include "Sum_model.h"


/* dummy VARINFO and FILEINFO */
const FILE_INFO dummyFILE_INFO = omc_dummyFileInfo;
const VAR_INFO dummyVAR_INFO = omc_dummyVarInfo;
#if defined(__cplusplus)
extern "C" {
#endif

int Sum_input_function(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  data->localData[0]->integerVars[0] /* u variable */ = data->simulationInfo->inputVars[0];
  data->localData[0]->integerVars[1] /* u1 variable */ = data->simulationInfo->inputVars[1];
  
  TRACE_POP
  return 0;
}

int Sum_input_function_init(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  data->simulationInfo->inputVars[0] = data->modelData->integerVarsData[0].attribute.start;
  data->simulationInfo->inputVars[1] = data->modelData->integerVarsData[1].attribute.start;
  
  TRACE_POP
  return 0;
}

int Sum_input_function_updateStartValues(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  data->modelData->integerVarsData[0].attribute.start = data->simulationInfo->inputVars[0];
  data->modelData->integerVarsData[1].attribute.start = data->simulationInfo->inputVars[1];
  
  TRACE_POP
  return 0;
}

int Sum_inputNames(DATA *data, char ** names){
  TRACE_PUSH

  names[0] = (char *) data->modelData->integerVarsData[0].info.name;
  names[1] = (char *) data->modelData->integerVarsData[1].info.name;
  
  TRACE_POP
  return 0;
}

int Sum_output_function(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  data->simulationInfo->outputVars[0] = data->localData[0]->integerVars[2] /* y DISCRETE */;
  
  TRACE_POP
  return 0;
}


/*
 equation index: 2
 type: SIMPLE_ASSIGN
 y = sum1.k[1] * u + sum1.k[2] * u1
 */
void Sum_eqFunction_2(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH
  const int equationIndexes[2] = {1,2};
  data->localData[0]->integerVars[2] /* y DISCRETE */ = ((modelica_integer)data->simulationInfo->integerParameter[0]) * ((modelica_integer)data->localData[0]->integerVars[0] /* u variable */) + ((modelica_integer)data->simulationInfo->integerParameter[1]) * ((modelica_integer)data->localData[0]->integerVars[1] /* u1 variable */);
  TRACE_POP
}


int Sum_functionDAE(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH
  int equationIndexes[1] = {0};
  
  data->simulationInfo->needToIterate = 0;
  data->simulationInfo->discreteCall = 1;
  Sum_functionLocalKnownVars(data, threadData);
  Sum_eqFunction_2(data, threadData);
  data->simulationInfo->discreteCall = 0;
  
  TRACE_POP
  return 0;
}


int Sum_functionLocalKnownVars(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  
  TRACE_POP
  return 0;
}


int Sum_functionODE(DATA *data, threadData_t *threadData)
{
  TRACE_PUSH

  
  data->simulationInfo->callStatistics.functionODE++;
  
  Sum_functionLocalKnownVars(data, threadData);
  /* no ODE systems */

  
  TRACE_POP
  return 0;
}

#ifdef FMU_EXPERIMENTAL
#endif
/* forward the main in the simulation runtime */
extern int _main_SimulationRuntime(int argc, char**argv, DATA *data, threadData_t *threadData);

#include "Sum_12jac.h"
#include "Sum_13opt.h"

struct OpenModelicaGeneratedFunctionCallbacks Sum_callback = {
   NULL,
   NULL,
   NULL,
   Sum_callExternalObjectDestructors,
   NULL,
   NULL,
   NULL,
   #if !defined(OMC_NO_STATESELECTION)
   Sum_initializeStateSets,
   #else
   NULL,
   #endif
   Sum_initializeDAEmodeData,
   Sum_functionODE,
   Sum_functionAlgebraics,
   Sum_functionDAE,
   Sum_functionLocalKnownVars,
   Sum_input_function,
   Sum_input_function_init,
   Sum_input_function_updateStartValues,
   Sum_output_function,
   Sum_function_storeDelayed,
   Sum_updateBoundVariableAttributes,
   Sum_functionInitialEquations,
   0, /* useHomotopy - 0: no homotopy or local homotopy, 1: global homotopy, 2: new global homotopy approach */
   Sum_functionInitialEquations_lambda0,
   Sum_functionRemovedInitialEquations,
   Sum_updateBoundParameters,
   Sum_checkForAsserts,
   Sum_function_ZeroCrossingsEquations,
   Sum_function_ZeroCrossings,
   Sum_function_updateRelations,
   Sum_checkForDiscreteChanges,
   Sum_zeroCrossingDescription,
   Sum_relationDescription,
   Sum_function_initSample,
   Sum_INDEX_JAC_A,
   Sum_INDEX_JAC_B,
   Sum_INDEX_JAC_C,
   Sum_INDEX_JAC_D,
   Sum_initialAnalyticJacobianA,
   Sum_initialAnalyticJacobianB,
   Sum_initialAnalyticJacobianC,
   Sum_initialAnalyticJacobianD,
   Sum_functionJacA_column,
   Sum_functionJacB_column,
   Sum_functionJacC_column,
   Sum_functionJacD_column,
   Sum_linear_model_frame,
   Sum_linear_model_datarecovery_frame,
   Sum_mayer,
   Sum_lagrange,
   Sum_pickUpBoundsForInputsInOptimization,
   Sum_setInputData,
   Sum_getTimeGrid,
   Sum_symbolicInlineSystem,
   Sum_function_initSynchronous,
   Sum_function_updateSynchronous,
   Sum_function_equationsSynchronous,
   Sum_read_input_fmu,
   #ifdef FMU_EXPERIMENTAL
   Sum_functionODE_Partial,
   Sum_functionFMIJacobian,
   #endif
   Sum_inputNames
};

void Sum_setupDataStruc(DATA *data, threadData_t *threadData)
{
  assertStreamPrint(threadData,0!=data, "Error while initialize Data");
  data->callback = &Sum_callback;
  data->modelData->modelName = "Sum";
  data->modelData->modelFilePrefix = "Sum";
  data->modelData->resultFileName = NULL;
  data->modelData->modelDir = "/home/spravce/Documents/OpenModelica/Sum";
  data->modelData->modelGUID = "{1c302156-9f89-4e47-989e-82f4cd9069f8}";
  data->modelData->initXMLData = NULL;
  data->modelData->modelDataXml.infoXMLData =
  #if defined(OMC_MINIMAL_METADATA)
    NULL;
  #else
  #include "Sum_info.c"
  #endif
  ;
  
  data->modelData->nStates = 0;
  data->modelData->nVariablesReal = 0;
  data->modelData->nDiscreteReal = 0;
  data->modelData->nVariablesInteger = 3;
  data->modelData->nVariablesBoolean = 0;
  data->modelData->nVariablesString = 0;
  data->modelData->nParametersReal = 0;
  data->modelData->nParametersInteger = 3;
  data->modelData->nParametersBoolean = 0;
  data->modelData->nParametersString = 0;
  data->modelData->nInputVars = 2;
  data->modelData->nOutputVars = 1;
  
  data->modelData->nAliasReal = 0;
  data->modelData->nAliasInteger = 3;
  data->modelData->nAliasBoolean = 0;
  data->modelData->nAliasString = 0;
  
  data->modelData->nZeroCrossings = 0;
  data->modelData->nSamples = 0;
  data->modelData->nRelations = 0;
  data->modelData->nMathEvents = 0;
  data->modelData->nExtObjs = 0;
  data->modelData->modelDataXml.fileName = "Sum_info.json";
  data->modelData->modelDataXml.modelInfoXmlLength = 0;
  data->modelData->modelDataXml.nFunctions = 0;
  data->modelData->modelDataXml.nProfileBlocks = 0;
  data->modelData->modelDataXml.nEquations = 5;
  data->modelData->nMixedSystems = 0;
  data->modelData->nLinearSystems = 0;
  data->modelData->nNonLinearSystems = 0;
  data->modelData->nStateSets = 0;
  data->modelData->nJacobians = 4;
  data->modelData->nOptimizeConstraints = 0;
  data->modelData->nOptimizeFinalConstraints = 0;
  
  data->modelData->nDelayExpressions = 0;
  
  data->modelData->nClocks = 0;
  data->modelData->nSubClocks = 0;
  
  data->modelData->nSensitivityVars = 0;
  data->modelData->nSensitivityParamVars = 0;
}

#ifdef __cplusplus
}
#endif

static int rml_execution_failed()
{
  fflush(NULL);
  fprintf(stderr, "Execution failed!\n");
  fflush(NULL);
  return 1;
}

