Skip to content

Commit cb576f8

Browse files
committed
workiing with the input data matrix transposed
1 parent 8175635 commit cb576f8

File tree

7 files changed

+32
-31
lines changed

7 files changed

+32
-31
lines changed

modules/cuqdyn-c/src/cuqdyn.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "matlab.h"
1313
#include "ode_solver.h"
1414
#include "states_transformer.h"
15+
#include "sunmatrix/sunmatrix_dense.h"
1516
#ifdef MPI
1617
#include <mpi.h>
1718
#endif
@@ -65,7 +66,7 @@ CuqdynResult *cuqdyn_algo(const char *data_file, const char *sacess_conf_file, c
6566
CuqdynConf *config = get_cuqdyn_conf(get_cuqdyn_context());
6667

6768
N_Vector times = NULL;
68-
ObservedData observed_data = NULL;
69+
TransposedObservedData observed_data = NULL;
6970

7071
if (read_data_file(data_file, &times, &observed_data) != 0)
7172
{
@@ -78,16 +79,16 @@ CuqdynResult *cuqdyn_algo(const char *data_file, const char *sacess_conf_file, c
7879

7980
if (config->y0.len == 0)
8081
{
81-
initial_condition = copy_matrix_row(observed_data, 0, 0, SM_COLUMNS_D(observed_data));
82+
initial_condition = copy_matrix_column(observed_data, 0, 0, SM_ROWS_D(observed_data));
8283
}
8384
else
8485
{
8586
initial_condition = New_Serial(config->y0.len);
8687
memcpy(NV_DATA_S(initial_condition), config->y0.array, config->y0.len * sizeof(sunrealtype));
8788
}
8889

89-
const long m = SM_ROWS_D(observed_data);
90-
const long n = SM_COLUMNS_D(observed_data);
90+
const long n = SM_ROWS_D(observed_data);
91+
const long m = SM_COLUMNS_D(observed_data);
9192

9293
SUNMatrix resid_loo = NULL;
9394
MatrixArray media_matrix = create_matrix_array(m - 1);
@@ -156,7 +157,7 @@ CuqdynResult *cuqdyn_algo(const char *data_file, const char *sacess_conf_file, c
156157
LongArray indices_to_remove = create_array((long[]) {i + 1}, 1);
157158

158159
N_Vector texp = copy_vector_remove_indices(times, indices_to_remove);
159-
SUNMatrix yexp = copy_matrix_remove_rows(observed_data, indices_to_remove);
160+
SUNMatrix yexp = copy_matrix_remove_columns(observed_data, indices_to_remove);
160161

161162
N_Vector tmp_initial_condition = copy_vector_remove_indices(initial_condition, create_array((long[]) {}, 0));
162163

@@ -169,7 +170,7 @@ CuqdynResult *cuqdyn_algo(const char *data_file, const char *sacess_conf_file, c
169170

170171
for (int j = 0; j < n; ++j)
171172
{
172-
sunrealtype observed = SM_ELEMENT_D(observed_data, i, j);
173+
sunrealtype observed = SM_ELEMENT_D(observed_data, j, i);
173174
sunrealtype predicted = SM_ELEMENT_D(predicted_obs_states, j, i);
174175

175176
NV_Ith_S(residuals, j) = fabs(observed - predicted);

modules/cuqdyn-c/src/data_reader.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <string.h>
88

99
#include "../include/cuqdyn.h"
10+
#include "sundials/sundials_types.h"
11+
#include "sunmatrix/sunmatrix_dense.h"
1012

1113
int read_data_file(const char *data_file, N_Vector *t, ObservedData *y)
1214
{
@@ -25,7 +27,7 @@ int read_data_file(const char *data_file, N_Vector *t, ObservedData *y)
2527
return 1;
2628
}
2729

28-
int read_txt_data_file(const char *data_file, N_Vector *t, ObservedData *y)
30+
int read_txt_data_file(const char *data_file, N_Vector *t, TransposedObservedData *y)
2931
{
3032
const char *ext = strrchr(data_file, '.');
3133
if (ext && strcmp(ext, ".txt") != 0)
@@ -46,19 +48,19 @@ int read_txt_data_file(const char *data_file, N_Vector *t, ObservedData *y)
4648
*t = New_Serial(rows);
4749
sunrealtype *data_t = N_VGetArrayPointer(*t);
4850

49-
*y = NewDenseMatrix(rows, cols - 1);
50-
sunrealtype *data_y = ((SUNMatrixContent_Dense)(*y)->content)->data;
51+
*y = NewDenseMatrix(cols - 1, rows);
5152

5253
double tmp;
5354

5455
for (int i = 0; i < rows; ++i)
5556
{
5657
fscanf(f, "%lf", &tmp);
5758
data_t[i] = tmp;
59+
sunrealtype *data_yi = ((SUNMatrixContent_Dense)(*y)->content)->cols[i];
5860
for (int j = 0; j < cols - 1; ++j)
5961
{
6062
fscanf(f, "%lf", &tmp);
61-
data_y[j * rows + i] = tmp;
63+
data_yi[j] = tmp;
6264
}
6365
}
6466

modules/cuqdyn-c/src/functions.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ void *obj_func(double *x, void *data)
4444
const long cols = SM_COLUMNS_D(result);
4545
sunrealtype J = 0.0;
4646

47-
// We compare rows with cols because the result matrix is transposed
48-
if (SM_ROWS_D(exptotal->yexp) != cols)
47+
if (SM_ROWS_D(exptotal->yexp) != rows)
4948
{
5049
fprintf(stderr, "ERROR: The yexp rows don't match the ode result rows: %ld vs %ld\n", SM_ROWS_D(exptotal->yexp), rows);
5150
exit(-1);
5251
}
5352

54-
// We compare rows with cols because the result matrix is transposed
55-
if (SM_COLUMNS_D(exptotal->yexp) != rows)
53+
if (SM_COLUMNS_D(exptotal->yexp) != cols)
5654
{
5755
fprintf(stderr, "ERROR: The yexp cols don't match the ode result cols: %ld vs %ld\n", SM_COLUMNS_D(exptotal->yexp), cols - 1);
5856
exit(-1);
@@ -62,7 +60,7 @@ void *obj_func(double *x, void *data)
6260
{
6361
for (long j = 0; j < cols; ++j)
6462
{
65-
const sunrealtype diff = SM_ELEMENT_D(result, i, j) - SM_ELEMENT_D(exptotal->yexp, j, i);
63+
const sunrealtype diff = SM_ELEMENT_D(result, i, j) - SM_ELEMENT_D(exptotal->yexp, i, j);
6664
J += diff * diff;
6765
}
6866
}
-775 Bytes
Binary file not shown.

tests/test_data_reader.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define LOTKA_VOLTERRA_MAT "data/lotka_volterra_data_homoc_noise_0.10_size_30_data_1.mat"
21
#define DATA_TXT "data/test_data.txt"
32

43
#include <nvector/nvector_serial.h>
@@ -7,7 +6,6 @@
76

87

98
#include "data_reader.h"
10-
#include "cuqdyn.h"
119

1210
void test_read_data_txt();
1311

@@ -26,10 +24,13 @@ void test_read_data_txt()
2624

2725
assert(read_txt_data_file(DATA_TXT, &t, &y) == 0);
2826

27+
assert(y != NULL);
28+
assert(t != NULL);
29+
2930
for (int i = 0; i < SM_ROWS_D(y); ++i)
3031
{
3132
assert(NV_Ith_S(t, i) == i);
32-
assert(SM_ELEMENT_D(y, i, 0) == i);
33-
assert(SM_ELEMENT_D(y, i, 1) == i);
33+
assert(SM_ELEMENT_D(y, 0, i) == i);
34+
assert(SM_ELEMENT_D(y, 1, i) == i);
3435
}
3536
}

tests/test_ess_obj_function.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "matlab.h"
44
#include "config.h"
55
#include "method_module/structure_paralleltestbed.h"
6+
#include "sunmatrix/sunmatrix_dense.h"
67

78
#define CUQDYN_CONF "data/obj_function_cuqdyn_config.xml"
89
#define SACESS_CONF "data/obj_function_ess_config_nl2sol.dn2fb.xml"
@@ -16,7 +17,7 @@ int main()
1617
SUNMatrix yexp = NULL;
1718

1819
read_data_file(DATA, &texp, &yexp);
19-
N_Vector initial_values = copy_matrix_row(yexp, 0, 0, SM_COLUMNS_D(yexp));
20+
N_Vector initial_values = copy_matrix_row(yexp, 0, 0, SM_ROWS_D(yexp));
2021

2122
experiment_total *exp_total = malloc(sizeof(experiment_total));
2223
create_expetiment_struct(SACESS_CONF, &exp_total[0], 1, 0, "output", 1, texp, yexp, initial_values);

tests/test_ess_solver.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cuqdyn.h>
1616
#include <stdio.h>
1717
#include <sunmatrix/sunmatrix_dense.h>
18+
#include <math.h>
1819
#include "nvector/nvector_serial.h"
1920

2021
#include "data_reader.h"
@@ -64,7 +65,7 @@ void lotka_volterra_ess(char *conf_file)
6465

6566
read_data_file(LOTKA_VOLTERRA_DATA, &texp, &yexp);
6667

67-
N_Vector initial_values = copy_matrix_row(yexp, 0, 0, SM_COLUMNS_D(yexp));
68+
N_Vector initial_values = copy_matrix_column(yexp, 0, 0, SM_ROWS_D(yexp));
6869

6970
N_Vector xbest = execute_ess_solver(conf_file, OUPUT_PATH, texp, yexp, initial_values, NULL);
7071

@@ -73,8 +74,7 @@ void lotka_volterra_ess(char *conf_file)
7374
sunrealtype expected = expected_values[i];
7475
sunrealtype result = NV_Ith_S(xbest, i);
7576

76-
sunrealtype a = 6;
77-
// assert(fabs(result - expected) < 0.1);
77+
assert(fabs(result - expected) < 1);
7878
}
7979

8080
destroy_cuqdyn_context(context);
@@ -91,7 +91,7 @@ void alpha_pinene_ess(char *conf_file)
9191

9292
read_data_file(ALPHA_PINENE_DATA, &texp, &yexp);
9393

94-
N_Vector initial_values = copy_matrix_row(yexp, 0, 0, SM_COLUMNS_D(yexp));
94+
N_Vector initial_values = copy_matrix_column(yexp, 0, 0, SM_ROWS_D(yexp));
9595

9696
N_Vector xbest = execute_ess_solver(conf_file, OUPUT_PATH, texp, yexp, initial_values, NULL);
9797

@@ -100,8 +100,7 @@ void alpha_pinene_ess(char *conf_file)
100100
sunrealtype expected = expected_values[i];
101101
sunrealtype result = NV_Ith_S(xbest, i);
102102

103-
sunrealtype a = 6;
104-
// assert(fabs(result - expected) < 0.1);
103+
assert(fabs(result - expected) < 1);
105104
}
106105

107106
destroy_cuqdyn_context(context);
@@ -111,24 +110,23 @@ void logistic_model_ess(char *conf_file)
111110
{
112111
CuqDynContext context = init_cuqdyn_context_from_file("data/logistic_model_cuqdyn_config.xml");
113112

114-
sunrealtype expected_values[2] = {0.1, 100};
113+
sunrealtype expected_values[2] = {0.1, 102};
115114

116115
N_Vector texp = NULL;
117116
SUNMatrix yexp = NULL;
118117

119118
read_data_file(LOGISTIC_MODEL_DATA, &texp, &yexp);
120119

121-
N_Vector initial_values = copy_matrix_row(yexp, 0, 0, SM_COLUMNS_D(yexp));
120+
N_Vector initial_values = copy_matrix_column(yexp, 0, 0, SM_ROWS_D(yexp));
122121

123122
N_Vector xbest = execute_ess_solver(conf_file, OUPUT_PATH, texp, yexp, initial_values, NULL);
124123

125-
for (int i = 0; i < sizeof(expected_values); ++i)
124+
for (int i = 0; i < 2; ++i)
126125
{
127126
sunrealtype expected = expected_values[i];
128127
sunrealtype result = NV_Ith_S(xbest, i);
129128

130-
sunrealtype a = 6;
131-
// assert(fabs(result - expected) < 0.1);
129+
assert(fabs(result - expected) < 1);
132130
}
133131

134132
destroy_cuqdyn_context(context);

0 commit comments

Comments
 (0)