mbs_buffer.c 12 KB
Newer Older
1

2
 /** \file mbs_buffer.c
3
4
5
6
7
8
9
10
  *
  * Implements function defined in mbs_buffer.h
  * 
  * @author Nicolas Docquier 
  *
  * (c) Universite catholique de Louvain
  */

11
#include "mbs_buffer.h"
12
#include "stdlib.h"
13
#include "stdio.h"
14
#include "string.h"
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
15
#include "set_output.h"
16
#include "mbs_message.h"
17
18
19
20
21
22

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

 /** Macro to get the id in tx array corresponding to index (assuming b->nx exist)*/
#define TIME_ID(i)   ((i)*(b->nx+1))

23
24
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
25
MbsBuffer* mbs_new_buffer(char* filename, char* anim_name, int nx, int size, int id, int save_anim, int save_visu, double anim_period){
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    
    MbsBuffer* b;
    FILE* fid;
    
    
    b = (MbsBuffer*) malloc(sizeof(MbsBuffer));
    
    
    // initialize name of associated text file
    b->filename = strdup(filename);
    
    // (re)-initialize the text file
    fid = fopen(b->filename, "w"); 
    if(fid == NULL)
    {
41
        mbs_error_msg("error in mbs_buffer_save: cannot open file '%s'\n", b->filename);
42
43
44
45
46
47
48
49
50
51
52
    }else{
        fclose(fid);
    }    
    
    // initialize index to 0
    b->index = 0;
    
    // initialize sizes
    b->nx   = nx;
    b->size = size;
    
53
54
55
56
57
    // initialize time vector and value vector
    // those 2 vector are allocated together to ensure memory
    // contiguity ==> this is useful for the python interface
    // so as to initialize a numpy array with an empty column at index 0.   
    b->tx = (double*)malloc(b->size*(b->nx+1)*sizeof(double));
58
59
    

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    // ID
    b->id = id;

    // first time flag
    b->flag_no_print = 1;

    // save last values
    b->last_x = (double*)malloc(b->nx*sizeof(double));
    b->last_t = 0.0;

    // anim info
    b->anim = (AnimInfo*)malloc(sizeof(AnimInfo));

    b->anim->save_anim   = save_anim;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
74
    b->anim->save_visu   = save_visu;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
75
76
77
78
79
80
81
    b->anim->anim_period = anim_period;
    b->anim->last_print_t = 0.0;

    // initialize name of associated text file
    b->anim->filename = strdup(anim_name);
    
    // (re)-initialize the text file
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
82
    if ((save_anim && (id == BUFFER_Q)) || (save_visu && (id == BUFFER_VISU)))
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
83
84
85
86
    {
        fid = fopen(b->anim->filename, "w"); 
        if(fid == NULL)
        {
87
            mbs_error_msg("in mbs_buffer_save: cannot open file '%s'\n", b->anim->filename);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
88
89
90
91
        }else{
            fclose(fid);
        }
    }
92
93
94
95
    
    return b;
}

96
97
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
98
MbsGrowingBuffer* mbs_new_growing_buffer(int max_nx, int size, const char *respath)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{
    int i;

    MbsGrowingBuffer* b;

    b = (MbsGrowingBuffer*) malloc(sizeof(MbsGrowingBuffer));

    // initialize index to 0
    b->index = 0;

    // initialize sizes
    b->size   = size;
    b->max_nx = max_nx;
    b->nx     = 0;

    // initialize time vector    
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
115
    b->t = (double*) malloc(size*sizeof(double));
116
117
118
119
120
121
122

    for(i=0; i<size; i++)
    {
        b->t[i] = 0.0;
    }

    // initialize value vector
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
123
    b->X = (double**) malloc(max_nx*sizeof(double*));
124
125
126
127
128
129
130

    for(i=0; i<max_nx; i++)
    {
        b->X[i] = NULL; // initially: no user input to save
    }

    // initialize name of associated text file
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
131
132
133
    b->names = (char**)malloc(max_nx*sizeof(char*));

    // path for the results
134
    b->respath = strdup(respath);
135
136
137
138
139

    return b;
}


140
141
142
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void mbs_delete_buffer(MbsBuffer* b){
143
144
145
146
    
    // free t and value vector together since thay were allocated together
    free(b->tx);  // free(b->x) is not necessary
    
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
147
    free(b->last_x);
148
    free(b->filename);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
149
150
151
152

    free(b->anim->filename);
    free(b->anim);

153
154
155
    free(b);
}

156
157
158
159
160
161
162
163
164
165
166
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void mbs_delete_growing_buffer(MbsGrowingBuffer *b)
{
    int i;

    free(b->t);

    for(i=0; i<b->nx; i++)
    {
        free(b->X[i]);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
167
        free(b->names[i]);
168
169
    }
    free(b->X);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
170
    free(b->names);
171

172
173
    free(b->respath);

174
175
    free(b);
}
176
177

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
178

179
int mbs_buffer_save(MbsBuffer* b, double t, double *x){
180
    
181
    int i, err =0;
182
    
183
    // save time
184
    b->tx[TIME_ID(b->index)] = t;
185
    
186
187
    // copy values to the buffer
    for (i=0; i < b->nx ; i++){
188
        b->tx[b->index*(b->nx+1) + 1 + i] = x[i];
189
190
191
192
193
194
195
    }
    
    // increment the buffer index for next call
    b->index++;
    
    // check if buffer is full
    if(b->index == b->size){
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
196

197
        // write the buffer to file
198
199
200
201
202
203
        err = mbs_buffer_write(b);

        if (err < 0)
        {
            return err;
        }
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
204
205
206
207
208
209
        // save last values added
        b->last_t = t;

        for (i=0; i < b->nx ; i++){
            b->last_x[i] = x[i];
        }
210
211
212
213
      
        // reset the buffer index to start of the buffer (next save will overide old values)
        b->index = 0;
    }
Your Name's avatar
Your Name committed
214
    return err;
215
216
}

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
217

218
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
219

220
int mbs_growing_buffer_save(MbsGrowingBuffer* b, double t)
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
221
222
223
224
225
226
{
    int i, j;
    int index, nb_new, cur_nx;
    char* f;
    FILE* fid;

Your Name's avatar
Your Name committed
227
228
    int err = 0;

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    // add new user labels and allocate memory
    nb_new = get_output_nb_new();

    if (nb_new > 0)
    {
        cur_nx = b->nx;

        for(i=cur_nx; i < cur_nx + nb_new; i++)
        {
            if (i >= b->max_nx)
            {
                break;
            }

            b->names[i] = strdup(get_output_label(i));

            // (re)-initialize the text file
            f = (char*) malloc((strlen(b->respath)+strlen(b->names[i])+10) * sizeof(char));

248
            sprintf(f, "%s_%s.res", b->respath, b->names[i]);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
249
250
251
252
253
            fid = fopen(f, "w");
            free(f);

            if(fid == NULL)
            {
254
                mbs_msg("\t >BUFFER SAVE>  Error: in mbs_growing_buffer_save: cannot open file '%s'\n", b->names[i]);
255
                return -9;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
            }
            else
            {
                fclose(fid);
            }

            b->X[i] = (double*) malloc(b->size*sizeof(double));

            for(j=0; j<b->size; j++)
            {
                b->X[i][j] = 0.0;
            }

            b->nx++;
        }

        set_output_nb_new(0);

        if (b->nx != get_output_nb())
        {
276
            mbs_msg("\t >BUFFER SAVE> %d, %d\n", b->nx, get_output_nb());
277
            mbs_msg("\t >BUFFER SAVE>  Error in mbs_growing_buffer_save : number of user inputs not the same as in the auto-output structure! \n");
278
            return -9;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
        }
    }

    index = b->index;

    // save time
    b->t[index] = t;

    // copy values to the buffer
    for(i=0; i<b->nx; i++)
    {
        b->X[i][index] = get_output_value(i);
    }

    // increment the buffer index for next call
    b->index++;

    // check if buffer is full
    if(b->index >= b->size)
    {
        // write the buffer to file
        mbs_growing_buffer_write(b);

        // reset the buffer index to start of the buffer (next save will overide old values)
        b->index = 0;
    }
Your Name's avatar
Your Name committed
305
    return err;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
306
307
308
309
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

310
int mbs_buffer_write(MbsBuffer* b){
311
    
312
    int i, j, err =0;
313
314
315
316
317
318
    FILE* fid;
    
    // Open the file associated to the buffer
    fid = fopen(b->filename, "a"); 
    if(fid == NULL)
    {
319
        mbs_msg("\t >BUFFER WRITE> in mbs_buffer_write: cannot open file '%s'\n", b->filename);
320
        return -9;
321
322
323
    }
    
    for (i=0; i<b->index; i++){
324
325
        // write time and array
        for (j=0; j<b->nx+1 ; j++){
326
            fprintf(fid, "  %e", b->tx[i*(b->nx+1) + j]);
327
328
329
        }
        
        // go to new line 
330
        fprintf(fid, "\n");
331
332
333
    }
    
    // Close file
334
    fclose(fid);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
335
336

    // special case for .anim file
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
337
    if ( (b->anim->save_anim && (b->id == BUFFER_Q)) || (b->anim->save_visu && (b->id == BUFFER_VISU)) )
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
338
    {
339
       err = mbs_anim_write(b); 
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
340
341
342
343
    }

    // first print done
    b->flag_no_print = 0;
344
    return err;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
345
346
347
348
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

349
int mbs_anim_write(MbsBuffer* b)
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
350
351
352
353
354
355
356
357
358
359
360
361
362
{
    int i;
    int next_index;
    double cur_t, prev_t, next_t, anim_period;
    double alpha, alpha_comp;
    double print_t, print_x;
    FILE* fid;

    // open the file associated to the buffer
    fid = fopen(b->anim->filename, "a"); 

    if(fid == NULL)
    {
363
        mbs_error_msg("\t >ANIM WRITE> in mbs_anim_write: cannot open file '%s'\n", b->anim->filename);
364
        return -9;
365
    }
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
366
367
368
369
370

    // safety
    if (b->index < 1)
    {
        fclose(fid);
371
        return 0;
372
    } 
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
373
374
375
376
377
378
379
380

    // period
    anim_period = b->anim->anim_period;

    // first time .anim is printed
    if (b->flag_no_print)
    {
        // first print
381
382
        for (i=0; i<b->nx+1 ; i++){
            fprintf(fid, "  %g", b->tx[i]);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
383
384
385
        }
        fprintf(fid, "\n");

386
        b->anim->last_print_t = b->tx[TIME_ID(0)];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
387
388
389
390
391

        // safety
        if (b->index < 2)
        {
            fclose(fid);
392
            return 0;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
393
394
        }

395
        cur_t = b->tx[TIME_ID(0)] + anim_period;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
396

397
398
        prev_t = b->tx[TIME_ID(0)];
        next_t = b->tx[TIME_ID(1)];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
399
400
401
402
403
404
405
406

        next_index = 1;
    }
    else
    {
        cur_t = b->anim->last_print_t + anim_period;

        prev_t = b->anim->last_print_t;
407
        next_t = b->tx[TIME_ID(0)];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
408
409
410
411

        next_index = 0;
    }

412
413
    print_t = b->anim->last_print_t;

414
    while (cur_t <= b->tx[TIME_ID(b->index-1)])
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
415
416
417
418
419
420
421
    {
        if ((prev_t <= cur_t) && (cur_t <= next_t))
        {
            // safety
            if (prev_t == next_t)
            {
                // write time
422
                print_t = b->tx[TIME_ID(next_index)];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
423
424
                
                // write array
425
                for (i=0; i<b->nx+1 ; i++)
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
426
                {
427
                    fprintf(fid, "  %g", b->tx[TIME_ID(next_index) + i]);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
                }
                
                // go to new line 
                fprintf(fid, "\n");
            }
            else
            {
                // factors
                alpha = (cur_t - prev_t) / (next_t - prev_t);
                alpha_comp = 1.0 - alpha;

                // write time
                print_t = alpha_comp * prev_t + alpha * next_t;

                fprintf(fid, "%g", print_t);

                // write array
                for (i=0; i<b->nx ; i++)
                {
                    if (next_index > 0)
                    {
449
                        print_x = alpha_comp * b->tx[TIME_ID(next_index-1)+1 + i] + alpha * b->tx[TIME_ID(next_index)+1 + i];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
450
451
452
                    }
                    else
                    {
453
                        print_x = alpha_comp * b->last_x[i] + alpha * b->tx[TIME_ID(next_index)+1 + i];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
                    }

                    fprintf(fid, "  %g", print_x);
                }

                // go to new line 
                fprintf(fid, "\n");
            }
            
            cur_t += anim_period;
        }
        else
        {
            next_index++;

            if (next_index >= b->index)
            {
                b->anim->last_print_t = print_t;
                fclose(fid);
473
                return 0;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
474
475
476
477
            }

            prev_t = next_t;

478
            next_t = b->tx[TIME_ID(next_index)];
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
479
480
481
482
483
484
485
        }
    }

    b->anim->last_print_t = print_t;

    // close file
    fclose(fid);
486
    return 0;
487
}
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
488
489
490

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

491
int mbs_growing_buffer_write(MbsGrowingBuffer* b)
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
492
{
Your Name's avatar
Your Name committed
493
494
    int err = 0;

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
495
496
497
498
499
500
501
502
    int i, j;
    char* f;
    FILE* fid;

    for(i=0; i<b->nx; i++)
    {
        // open the file associated to the current arrow of the buffer
        f = (char*) malloc((strlen(b->respath)+strlen(b->names[i])+10) * sizeof(char));
503
        sprintf(f, "%s_%s.res", b->respath, b->names[i]);
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
504
505
506
507
508
        fid = fopen(f, "a");
        free(f);

        if(fid == NULL)
        {
509
            mbs_msg("\t >BUFFER WRITE> in mbs_growing_buffer_write: cannot open file '%s'\n", b->names[i]);
510
            return -9;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
511
512
513
514
515
516
517
518
519
520
521
        }

        for(j=0; j<b->index; j++)
        {
            // write line
            fprintf(fid, "%g  %g\n", b->t[j], b->X[i][j]);
        }

        // close file
        fclose(fid);
    }
Your Name's avatar
Your Name committed
522
    return err;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
523
}