#include <math.h>
#include <stdio.h>
#include <time.h>
#include "lsolve.h"

#define M {{ variables|length }}    /* No of Blocks */
#define N {{ itcnt }}    /* No of Iterations */

/* BLOCK VARIABLE NAMES */
{% for var in variables %}
#define {{ var }} RESULT[{{ loop.index0 }}]
{% endfor %}

double RESULT[M][N];

/* EQUATIONS */
void _eq_init(const double time, const double delta) {
    {% for comp in components0 %}
        {% if comp[0]|length == 1 %}
    {{ comp[0][0] }}[0] = {{ comp[1] }};
        {% else %}

    /* Algebraic Loop */
    double _C{{ loop.index0 }}[{{comp[0]|length}}][{{comp[0]|length + 1}}] = {{ comp[1] }};
    agloop(&_C{{ loop.index0 }}, {{comp|length}}{% for block in comp[0] %}, &{{ block }}[0]{% endfor %});

        {% endif %}
    {% endfor %}
}

void _eq_iter(const unsigned int i, const double time, const double delta) {
    {% for comp in components %}
        {% if comp[0]|length == 1 %}
    {{ comp[0][0] }}[i] = {{ comp[1] }};
        {% else %}

    /* Algebraic Loop */
    double _C{{ loop.index0 }}[{{comp[0]|length}}][{{comp[0]|length + 1}}] = {{ comp[1] }};
    agloop(&_C{{ loop.index0 }}, {{comp|length}}{% for block in comp[0] %}, &{{ block }}[i]{% endfor %});

        {% endif %}
    {% endfor %}
}

/* SIMULATOR */
int main(int argc, char *args[]) {
    double delta = {{ delta }};
    double time = 0.0;

    _eq_init(0.0, delta);
    for(int i = 1; i < N; ++i) {
        time += delta;
        _eq_iter(i, time, delta);
    }

    printf("\t%-20s = %12.4f\n", "TIME", time);
    {% for var in variables %}
    printf("\t%-20s = %12.4f\n", "{{ var }}", {{ var }}[N - 1]);
    {% endfor %}
}
