YetAnotherCoupler 3.5.2
Loading...
Searching...
No Matches
test_events.c

This show how to work with events.

// Copyright (c) 2024 The YAC Authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tests.h"
#include "event.h"
int main (void) {
// basic setup
initCalendar(PROLEPTIC_GREGORIAN);
{
struct {
enum yac_time_unit_type time_unit;
char const * value;
} tests[] =
{{.time_unit = C_MILLISECOND,
.value = "3600000"},
{.time_unit = C_SECOND,
.value = "3600"},
{.time_unit = C_MINUTE,
.value = "60"},
{.time_unit = C_HOUR,
.value = "1"},
{.time_unit = C_ISO_FORMAT,
.value = "PT01H"}};
enum {NUM_TESTS = sizeof(tests) / sizeof(tests[0])};
for (size_t i = 0; i < NUM_TESTS; ++i)
if (strcmp(
tests[i].value, tests[i].time_unit), "PT01H"))
PUT_ERR("ERROR in yac_time_to_ISO");
}
{
struct event * event = yac_event_new ();
char const * model_time_step = "5000"; // 5s per time step
char const * coupling_time_step = "15000"; // 15s per coupling time step
int lag = 0;
// 60s runtime
char const * start_date = "1850-01-01T00:00:00";
char const * stop_date = "1850-01-01T00:01:00";
char const * model_time_step_iso =
char const * coupling_time_step_iso =
event, model_time_step_iso, coupling_time_step_iso,
lag, time_operation, start_date, stop_date);
char time_step_buffer[MAX_TIMEDELTA_STR_LEN];
if (strcmp(
coupling_time_step_iso))
PUT_ERR("ERROR in yac_get_event_coupling_timestep");
if (strcmp(
yac_get_event_model_timestep(event, time_step_buffer),
model_time_step_iso))
PUT_ERR("ERROR in yac_get_event_model_timestep");
PUT_ERR("ERROR in yac_get_event_lag");
free((void*)coupling_time_step_iso);
free((void*)model_time_step_iso);
enum {NUM_TIME_STEPS = 15};
enum yac_action_type ref_action[NUM_TIME_STEPS] =
for (int t = 0; t < NUM_TIME_STEPS; ++t) {
if (yac_event_check(event) != ref_action[t]) PUT_ERR("wrong action\n");
}
}
{
struct event * event = yac_event_new ();
char const * model_time_step = "5000"; // 5s per time step
char const * coupling_time_step = "15000"; // 15s per coupling time step
int lag = 0;
// 60s runtime
char const * start_date = "1850-01-01T00:00:00";
char const * stop_date = "1850-01-01T00:01:00";
char const * model_time_step_iso =
char const * coupling_time_step_iso =
event, model_time_step_iso, coupling_time_step_iso,
lag, time_operation, start_date, stop_date);
char time_step_buffer[MAX_TIMEDELTA_STR_LEN];
if (strcmp(
coupling_time_step_iso))
PUT_ERR("ERROR in yac_get_event_coupling_timestep");
if (strcmp(
yac_get_event_model_timestep(event, time_step_buffer),
model_time_step_iso))
PUT_ERR("ERROR in yac_get_event_model_timestep");
PUT_ERR("ERROR in yac_get_event_lag");
free((void*)coupling_time_step_iso);
free((void*)model_time_step_iso);
enum {NUM_TIME_STEPS = 15};
enum yac_action_type ref_action[NUM_TIME_STEPS] =
for (int t = 0; t < NUM_TIME_STEPS; ++t) {
if (yac_event_check(event) != ref_action[t]) PUT_ERR("wrong action\n");
}
}
{
struct event * event = yac_event_new ();
char * model_time_step = "PT30M";
char * coupling_time_step = "PT60M";
int lag = 0;
// 60s runtime
char const * start_date = "1850-01-01T00:00:00";
char const * stop_date = "1850-01-01T06:00:00";
char const * model_time_step_iso =
char const * coupling_time_step_iso =
event, model_time_step_iso, coupling_time_step_iso,
lag, time_operation, start_date, stop_date);
char time_step_buffer[MAX_TIMEDELTA_STR_LEN];
if (strcmp(
coupling_time_step_iso))
PUT_ERR("ERROR in yac_get_event_coupling_timestep");
if (strcmp(
yac_get_event_model_timestep(event, time_step_buffer),
model_time_step_iso))
PUT_ERR("ERROR in yac_get_event_model_timestep");
PUT_ERR("ERROR in yac_get_event_lag");
free((void*)coupling_time_step_iso);
free((void*)model_time_step_iso);
enum {NUM_TIME_STEPS = 16};
enum yac_action_type ref_action[NUM_TIME_STEPS] =
for (int t = 0; t < NUM_TIME_STEPS; ++t) {
if (yac_event_check(event) != ref_action[t]) PUT_ERR("wrong action\n");
}
}
{
char const * model_time_step = "5000"; // 5s per time step
char const * coupling_time_step = "15000"; // 15s per coupling time step
// 60s runtime
char const * start_date = "1850-01-01T00:00:00";
char const * stop_date = "1850-01-01T00:01:00";
enum {MIN_LAG = -6, MAX_LAG = 3};
enum {NUM_LAGS = MAX_LAG - MIN_LAG + 1};
enum {NUM_TIME_STEPS = 15};
enum yac_action_type ref_action[NUM_TIME_STEPS - MIN_LAG] =
for (int lag = MIN_LAG; lag <= MAX_LAG; ++lag) {
struct event * event = yac_event_new ();
char const * model_time_step_iso =
char const * coupling_time_step_iso =
event, model_time_step_iso, coupling_time_step_iso,
lag, time_operation, start_date, stop_date);
char time_step_buffer[MAX_TIMEDELTA_STR_LEN];
if (strcmp(
coupling_time_step_iso))
PUT_ERR("ERROR in yac_get_event_coupling_timestep");
if (strcmp(
yac_get_event_model_timestep(event, time_step_buffer),
model_time_step_iso))
PUT_ERR("ERROR in yac_get_event_model_timestep");
PUT_ERR("ERROR in yac_get_event_lag");
free((void*)coupling_time_step_iso);
free((void*)model_time_step_iso);
for (int t = lag; t < NUM_TIME_STEPS; ++t) {
if (action != ref_action[t - MIN_LAG])
PUT_ERR("wrong action\n");
}
}
}
freeCalendar();
return TEST_EXIT_CODE;
}
char const * yac_time_to_ISO(char const *time, enum yac_time_unit_type time_unit)
Definition event.c:329
@ COUPLING
yac_reduction_type
@ TIME_NONE
@ TIME_AVERAGE
yac_time_unit_type
@ C_SECOND
@ C_HOUR
@ C_MILLISECOND
@ C_MINUTE
@ C_ISO_FORMAT
int main(int argc, char **argv)
char * yac_get_event_model_timestep(struct event *event, char *timedelta_str)
Definition event.c:304
char * yac_get_event_coupling_timestep(struct event *event, char *timedelta_str)
Definition event.c:291
int yac_get_event_lag(struct event *event)
Definition event.c:277
void yac_event_delete(struct event *event)
Definition event.c:258
struct event * yac_event_new()
Definition event.c:36
void yac_event_add(struct event *event, char const *delta_model_time, char const *delta_coupling_time, int lag, enum yac_reduction_type time_operation, const char *startdate, const char *stopdate)
Definition event.c:55
enum yac_action_type yac_event_check(struct event *event)
Definition event.c:182
void yac_event_update(struct event *event)
Definition event.c:170
yac_action_type
Definition event.h:17
@ REDUCTION
Definition event.h:19
@ RESTART
Definition event.h:21
@ OUT_OF_BOUND
Definition event.h:26
@ NONE
Definition event.h:18
struct @7::@8 value
Definition event.c:18
enum yac_time_unit_type time_unit
Definition event.c:26
enum yac_reduction_type time_operation
Definition event.c:24
enum yac_action_type action
Definition event.c:25
struct _timedelta * model_time_step
Definition event.c:20
int lag
Definition event.c:23
struct _timedelta * coupling_time_step
Definition event.c:21