-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate files for weekly schedule includes for #1891
- Loading branch information
Showing
5 changed files
with
130 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
This code implements a weekly schedule. | ||
Changelog: | ||
April 9, 2024 by Filip Jorissen, Builtwins | ||
Revisions for #1869 to remove a header requirement that contains the number of rows/columns. | ||
March 30, 2024 by Filip Jorissen, Builtwins | ||
Revisions for #1860 to avoid memory leaks when calling ModelicaFormatError. | ||
May 25, 2022 by Michael Wetter, LBNL | ||
Refactored to comply with C89. | ||
March 9, 2022 by Filip Jorissen, KU Leuven | ||
Initial version. | ||
April 10, 2022 by Filip Jorissen, KU Leuven | ||
Added tableOnFile option. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include "WeeklySchedule.h" | ||
#include "ModelicaUtilities.h" | ||
|
||
#ifndef WEEKCALFREE_c | ||
#define WEEKCALFREE_c | ||
|
||
|
||
void weeklyScheduleFree(void * ID) { | ||
int i; | ||
WeeklySchedule* scheduleID = (WeeklySchedule*)ID; | ||
|
||
if (ID == NULL) /* Otherwise OM segfaults when IBPSA.Utilities.IO.Files.Examples.WeeklySchedule triggers an error */ | ||
return; | ||
|
||
for (i = 0; i < scheduleID->n_allocatedRulesData; ++i) { | ||
free(scheduleID->rules[i]->data); | ||
} | ||
|
||
for (i = 0; i < scheduleID->n_allocatedRules; ++i) { | ||
free(scheduleID->rules[i]); | ||
} | ||
|
||
if (scheduleID->rules != NULL) | ||
free(scheduleID->rules); | ||
|
||
free(ID); | ||
ID = NULL; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
This code implements a weekly schedule. | ||
Changelog: | ||
April 9, 2024 by Filip Jorissen, Builtwins | ||
Revisions for #1869 to remove a header requirement that contains the number of rows/columns. | ||
March 30, 2024 by Filip Jorissen, Builtwins | ||
Revisions for #1860 to avoid memory leaks when calling ModelicaFormatError. | ||
May 25, 2022 by Michael Wetter, LBNL | ||
Refactored to comply with C89. | ||
March 9, 2022 by Filip Jorissen, KU Leuven | ||
Initial version. | ||
April 10, 2022 by Filip Jorissen, KU Leuven | ||
Added tableOnFile option. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include "WeeklySchedule.h" | ||
#include "ModelicaUtilities.h" | ||
|
||
#ifndef WEEKCALGET_c | ||
#define WEEKCALGET_c | ||
|
||
|
||
/* Get a column value. Cache the last used row internally to speed up lookup. */ | ||
double getScheduleValue(void * ID, const int column, const double modelicaTime) { | ||
WeeklySchedule* scheduleID = (WeeklySchedule*)ID; | ||
/* extrapolation for weeks that are outside of the user-defined range */ | ||
double t = modelicaTime - scheduleID->t_offset; | ||
const double weekLen = 7 * 24 * 3600; | ||
double time = fmod(t - weekLen * floor(t / weekLen), weekLen); | ||
int i; | ||
const int columnIndex = column - 1; /* Since we do not store the time indices in the data table */ | ||
|
||
/* Not calling weeklyScheduleFreeInit() or weeklyScheduleFree() since weeklyScheduleFreeInit() has already been called at the end of the | ||
initialization and Modelica will call weeklyScheduleFree() upon a call of ModelicaFormatError) */ | ||
if (column < 0 || column > scheduleID->n_data_cols) { | ||
ModelicaFormatError("The requested column index '%i' is outside of the table range '%i'.", column + 1, scheduleID->n_data_cols); | ||
} | ||
if (column == 0 ) { | ||
ModelicaFormatError("The column index 1 is not a data column and is reserved for 'time'. It should not be read."); | ||
} | ||
|
||
|
||
if (time == scheduleID->previousTimestamp) { | ||
i = scheduleID->previousIndex; | ||
} else if (time > scheduleID->rules[scheduleID->previousIndex]->time) { | ||
for (i = scheduleID->previousIndex; i < scheduleID->n_allocatedRules - 1; i ++) { | ||
if (scheduleID->rules[i + 1]->time > time) { | ||
break; | ||
} | ||
} | ||
} else { | ||
for (i = scheduleID->previousIndex; i > 0; i--) { | ||
if (scheduleID->rules[i - 1]->time < time) { | ||
i = i - 1; | ||
break; | ||
} | ||
} | ||
/* if time is smaller than the first row, wrap back to the end of the week */ | ||
if (i == 0 && scheduleID->rules[0]->time > time) { | ||
i = scheduleID->n_allocatedRules - 1; | ||
} | ||
} | ||
scheduleID->previousIndex = i; | ||
scheduleID->previousTimestamp = time; | ||
|
||
return scheduleID->rules[i]->data[columnIndex]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters