Skip to content

Commit

Permalink
- make list usersave ;)
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10435 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 11, 2011
1 parent e647c8d commit 25cab05
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 56 deletions.
61 changes: 50 additions & 11 deletions SimulationRuntime/c/math-support/list.c
Expand Up @@ -39,7 +39,30 @@
#include <stdlib.h>
#include <assert.h>

void list_push_front(int data, List *list)
typedef struct list_node {
int data;
struct list_node *next;
} List_Node;

typedef struct list_list {
struct list_node *first;
struct list_node *last;
} List;

List* list_init()
{
return (List*)calloc(1,sizeof(List));
}

void list_deinit(List *list)
{
if (list)
{
free(list);
}
}

void list_push_front(List *list, int data)
{
List_Node *new_node = 0;
assert(list);
Expand All @@ -52,7 +75,7 @@ void list_push_front(int data, List *list)
}
}

void list_push_back(int data, List *list)
void list_push_back(List *list, int data)
{
List_Node *new_node = 0;
assert(list);
Expand All @@ -68,21 +91,24 @@ void list_push_back(int data, List *list)
list->last = new_node;
}

int list_empty(List list)
int list_empty(List *list)
{
return list.first==0?1:0;
assert(list);
return list->first==0?1:0;
}

int list_front(List list)
int list_front(List *list)
{
assert(list.first);
return list.first->data;
assert(list);
assert(list->first);
return list->first->data;
}

int list_last(List list)
int list_last(List *list)
{
assert(list.last);
return list.last->data;
assert(list);
assert(list->last);
return list->last->data;
}

void list_pop_front(List *list)
Expand All @@ -109,10 +135,23 @@ void list_clear(List *list)
}
}

List_Node* list_next(List_Node* node)
List_Node *list_first(List *list)
{
assert(list);
return list->first;
}

List_Node *list_next(List_Node *node)
{
assert(node);
if (node==0) {
return 0;
}
return node->next;
}

int list_node_data(List_Node *node)
{
assert(node);
return node->data;
}
33 changes: 17 additions & 16 deletions SimulationRuntime/c/math-support/list.h
Expand Up @@ -42,33 +42,34 @@
extern "C" {
#endif

typedef struct list_node {
int data;
struct list_node *next;
} List_Node;
/* Prototypes for Structs */
struct list_node;
struct list_list;
typedef struct list_node List_Node;
typedef struct list_list List;

typedef struct list_list {
struct list_node *first;
struct list_node *last;
} List;
List* list_init();
void list_deinit(List *list);

#define EMPTYLIST {0,0}
void list_push_front(List *list, int data);

void list_push_front(int data, List *list);
void list_push_back(List *list, int data);

void list_push_back(int data, List *list);
int list_empty(List *list);

int list_empty(List list);
int list_front(List *list);

int list_front(List list);

int list_last(List list);
int list_last(List *list);

void list_pop_front(List *list);

void list_clear(List *list);

List_Node* list_next(List_Node* node);
List_Node *list_first(List *list);

List_Node *list_next(List_Node *node);

int list_node_data(List_Node *node);

#ifdef __cplusplus
}
Expand Down
62 changes: 34 additions & 28 deletions SimulationRuntime/c/math-support/simulation_events.c
Expand Up @@ -36,7 +36,7 @@
#include <string.h>


static List EventList = EMPTYLIST;
static List* EventList=NULL;

/*vectors with saved values used by pre(v)*/
#define x_saved globalData->states_saved
Expand Down Expand Up @@ -65,6 +65,7 @@ initializeEventData()
/*
* Re-Initialization is important because the variables are global and used in every solving step
*/
EventList = list_init();
globalData->helpVars_saved = 0;
globalData->states_saved = 0;
globalData->statesDerivatives_saved = 0;
Expand Down Expand Up @@ -119,6 +120,7 @@ deinitializeEventData()
free(gout_old);
free(backuprelations);
free(zeroCrossingEnabled);
list_deinit(EventList);
}


Expand Down Expand Up @@ -661,15 +663,15 @@ CheckForNewEvent(int* sampleactived)
{
fprintf(stdout, "| info LOG_EVENTS | adding event %d at time: %f\n",i, globalData->timeValue); fflush(NULL);
}
list_push_front(i,&EventList);
list_push_front(EventList,i);
}
else if (gout[i] < 0 && zeroCrossingEnabled[i] >= 1)
{
if (sim_verbose >= LOG_EVENTS)
{
fprintf(stdout, "| info LOG_EVENTS | adding event %d at time: %f\n",i, globalData->timeValue); fflush(NULL);
}
list_push_front(i,&EventList);
list_push_front(EventList,i);
}
}
if ((gout[i] <= 0 && gout_old[i] > 0) ||
Expand All @@ -679,7 +681,7 @@ CheckForNewEvent(int* sampleactived)
{
fprintf(stdout, "| info LOG_EVENTS | adding event %d at time: %f\n",i, globalData->timeValue); fflush(NULL);
}
list_push_front(i,&EventList);
list_push_front(EventList,i);
}
}
if (list_empty(EventList) != 1)
Expand Down Expand Up @@ -730,7 +732,7 @@ EventHandle(int flag)
while (list_empty(EventList) != 1)
{
event_id = list_front(EventList);
list_pop_front(&EventList);
list_pop_front(EventList);
if (sim_verbose >= LOG_EVENTS)
{
fprintf(stdout, "%d", event_id);
Expand Down Expand Up @@ -909,23 +911,27 @@ FindRoot(double *EventTime)

int event_id;
List_Node* it;
static List tmpEventList;
fortran_integer i=0;

static List *tmpEventList = 0;
double *states_right = (double*)calloc(globalData->nStates,sizeof(double));
double *states_left = (double*)calloc(globalData->nStates,sizeof(double));

double time_left = globalData->oldTime;
double time_right = globalData->timeValue;

if (!tmpEventList)
{
tmpEventList = list_init();
}

assert(states_right);
assert(states_left);

for ( it=EventList.first ; it != 0; it=list_next(it) )
for ( it=list_first(EventList) ; it != 0; it=list_next(it) )
{
if (sim_verbose >= LOG_ZEROCROSSINGS)
{
fprintf(stdout, "| info LOG_ZEROCROSSINGS | Search for current event. Events in list: %d\n",it->data); fflush(NULL);
fprintf(stdout, "| info LOG_ZEROCROSSINGS | Search for current event. Events in list: %d\n",list_node_data(it)); fflush(NULL);
}
}

Expand All @@ -944,31 +950,31 @@ FindRoot(double *EventTime)
if (list_empty(tmpEventList) == 1)
{
double value = fabs(gout[list_front(EventList)]);
for ( it=EventList.first ; it != 0; it=list_next(it) )
for ( it=list_first(EventList) ; it != 0; it=list_next(it) )
{
if (value > fabs(gout[it->data]))
if (value > fabs(gout[list_node_data(it)]))
{
value = fabs(gout[it->data]);
value = fabs(gout[list_node_data(it)]);
}
}
if (sim_verbose >= LOG_ZEROCROSSINGS)
{
fprintf(stdout, "| info LOG_ZEROCROSSINGS | Minimum value: %g\n",value); fflush(NULL);
}
for ( it=EventList.first ; it != 0; it=list_next(it) )
for ( it=list_first(EventList) ; it != 0; it=list_next(it) )
{
if (value == fabs(gout[it->data]))
if (value == fabs(gout[list_node_data(it)]))
{
list_push_back(it->data,&tmpEventList);
list_push_back(tmpEventList,list_node_data(it));
if (sim_verbose >= LOG_ZEROCROSSINGS)
{
fprintf(stdout, "| info LOG_ZEROCROSSINGS | added tmp event : %d\n",*it); fflush(NULL);
fprintf(stdout, "| info LOG_ZEROCROSSINGS | added tmp event : %d\n",list_node_data(it)); fflush(NULL);
}
}
}
}

list_clear(&EventList);
list_clear(EventList);

if (list_empty(tmpEventList) != 1)
{
Expand All @@ -987,7 +993,7 @@ FindRoot(double *EventTime)
while (list_empty(tmpEventList) != 1)
{
event_id = list_front(tmpEventList);
list_pop_front(&tmpEventList);
list_pop_front(tmpEventList);
if (sim_verbose >= LOG_EVENTS)
{
fprintf(stdout, "%d ", event_id); fflush(NULL);
Expand All @@ -999,7 +1005,7 @@ FindRoot(double *EventTime)
fprintf(stdout, ", "); fflush(NULL);
}
}
list_push_front(event_id,&EventList);
list_push_front(EventList,event_id);
}
if (sim_verbose >= LOG_EVENTS)
{
Expand Down Expand Up @@ -1041,7 +1047,7 @@ FindRoot(double *EventTime)
*/
double
BiSection(double* a, double* b, double* states_a, double* states_b,
List tmpEventList)
List *tmpEventList)
{

/*double TTOL = DBL_EPSILON*fabs((*b - *a))*100; */
Expand Down Expand Up @@ -1085,7 +1091,7 @@ BiSection(double* a, double* b, double* states_a, double* states_b,
functionAlgebraics();

function_onlyZeroCrossings(gout, &globalData->timeValue);
if (CheckZeroCrossings(&tmpEventList))
if (CheckZeroCrossings(tmpEventList))
{ /*If Zerocrossing in left Section */

for (i = 0; i < globalData->nStates; i++)
Expand Down Expand Up @@ -1166,23 +1172,23 @@ CheckZeroCrossings(List *tmpEventList)

List_Node *it;
list_clear(tmpEventList);
for ( it=EventList.first ; it != 0; it=list_next(it) )
for ( it=list_first(EventList) ; it != 0; it=list_next(it) )
{
if (sim_verbose >= LOG_ZEROCROSSINGS)
{
fprintf(stdout, "| info LOG_ZEROCROSSINGS | ZeroCrossing ID: %d \t old = %g \t current = %g \t Direction = %li\n",
*it,gout_old[it->data], gout[it->data],zeroCrossingEnabled[it->data]); fflush(NULL);
list_node_data(it),gout_old[list_node_data(it)], gout[list_node_data(it)],zeroCrossingEnabled[list_node_data(it)]); fflush(NULL);
}
/*Found event in left section*/
if ((gout[it->data] <= 0 && gout_old[it->data] > 0) || (gout[it->data] >= 0 && gout_old[it->data] < 0)
|| (gout[it->data] > 0 && zeroCrossingEnabled[it->data] <= -1) || (gout[it->data] < 0
&& zeroCrossingEnabled[it->data] >= 1))
if ((gout[list_node_data(it)] <= 0 && gout_old[list_node_data(it)] > 0) || (gout[list_node_data(it)] >= 0 && gout_old[list_node_data(it)] < 0)
|| (gout[list_node_data(it)] > 0 && zeroCrossingEnabled[list_node_data(it)] <= -1) || (gout[list_node_data(it)] < 0
&& zeroCrossingEnabled[list_node_data(it)] >= 1))
{
list_push_front(it->data,tmpEventList);
list_push_front(tmpEventList,list_node_data(it));
}
}
/*Found event in left section*/
if (list_empty(*tmpEventList) != 1)
if (list_empty(tmpEventList) != 1)
{
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/math-support/simulation_events.h
Expand Up @@ -53,7 +53,7 @@ void
deinitializeEventData();

double
BiSection(double*, double*, double*, double*, List);
BiSection(double*, double*, double*, double*, List*);

int
CheckZeroCrossings(List *list);
Expand Down

0 comments on commit 25cab05

Please sign in to comment.