forked from jburguete/mpcotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrator.c
1156 lines (1085 loc) · 29.3 KB
/
calibrator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Calibrator: a software to make calibrations of empirical parameters.
AUTHORS: Javier Burguete and Borja Latorre.
Copyright 2012-2013, AUTHORS.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
/**
* \file calibrator.c
* \brief Source file of the calibrator.
* \authors Javier Burguete and Borja Latorre.
* \copyright Copyright 2013, all rights reserved.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <alloca.h>
#include <gsl/gsl_rng.h>
#include <libxml/parser.h>
#include <glib.h>
#ifdef HAVE_MPI
#include <mpi.h>
#endif
/**
* \def DEBUG
* \brief Macro to debug.
*/
#define DEBUG 0
/**
* \enum CalibrateAlgorithm
* \brief Enum to define the calibration algorithm.
*/
enum CalibrateAlgorithm
{
CALIBRATE_ALGORITHM_MONTE_CARLO = 0,
CALIBRATE_ALGORITHM_SWEEP = 1,
CALIBRATE_ALGORITHM_GENETIC = 2
};
/**
* \struct Calibrate
* \brief Struct to define the calibration data.
*/
typedef struct
{
/**
* \var simulator
* \brief Name of the simulator program.
* \var evaluator
* \brief Name of the program to evaluate the objective function.
* \var template
* \brief Matrix of template names of input files.
* \var experiment
* \brief Array experimental data file names.
* \var label
* \brief Array of variable names.
* \var format
* \brief Array of variable formats.
* \var nvariables
* \brief Variables number.
* \var nexperiments
* \brief Experiments number.
* \var ninputs
* \brief Number of input files to the simulator.
* \var nsimulations
* \brief Simulations number per experiment.
* \var algorithm
* \brief Algorithm number
* \var nsweeps
* \brief Array of sweeps of the sweep algorithm.
* \var nstart
* \brief Beginning simulation number of the task.
* \var nend
* \brief Ending simulation number of the task.
* \var nthreads
* \brief Number of threads.
* \var thread
* \brief Array of simulation numbers to calculate on the thread.
* \var niterations
* \brief Number of algorithm iterations
* \var nbests
* \brief Number of best simulations.
* \var nsaveds
* \brief Number of saved simulations.
* \var simulation_best
* \brief Array of best simulation numbers.
* \var value
* \brief Array of variable values.
* \var rangemin
* \brief Array of minimum variable values.
* \var rangemax
* \brief Array of maximum variable values.
* \var error_best
* \brief Array of best minimum errors.
* \var tolerance
* \brief Algorithm tolerance.
* \var file
* \brief Matrix of input template files.
* \var mpi_rank
* \brief Number of MPI task.
* \var mpi_tasks
* \brief Total number of MPI tasks.
*/
char *simulator, *evaluator, **experiment, **template[4], **label, **format;
unsigned int nvariables, nexperiments, ninputs, nsimulations, algorithm,
*nsweeps, nstart, nend, nthreads, *thread, niterations, nbests, nsaveds,
*simulation_best;
double *value, *rangemin, *rangemax, *error_best, tolerance;
GMappedFile **file[4];
#ifdef HAVE_MPI
int mpi_rank, mpi_tasks;
#endif
} Calibrate;
/**
* \struct ParallelData
* \brief Struct to pass to the GThreads parallelized function.
*/
typedef struct
{
/**
* \var thread
* \brief Thread number.
* \var calibrate
* \brief Calibration data pointer.
*/
unsigned int thread;
Calibrate *calibrate;
} ParallelData;
/**
* \var rng
* \brief Pseudo-random numbers generator struct.
*/
gsl_rng *rng;
/**
* \var mutex
* \brief Mutex struct.
*/
GMutex mutex;
/**
* \fn void calibrate_input(Calibrate *calibrate, unsigned int simulation, \
* char *input, GMappedFile *template)
* \brief Function to write the simulation input file.
* \param calibrate
* \brief Calibration data.
* \param simulation
* \brief Simulation number.
* \param input
* \brief Input file name.
* \param template
* \brief Template of the input file name.
*/
void calibrate_input(Calibrate *calibrate, unsigned int simulation,
char *input, GMappedFile *template)
{
unsigned int i;
char buffer[32], value[32], *buffer2, *buffer3, *content;
FILE *file;
gsize length;
GRegex *regex;
#if DEBUG
printf("calibrate_input: start\n");
#endif
// Opening template
content = g_mapped_file_get_contents(template);
length = g_mapped_file_get_length(template);
#if DEBUG
printf("calibrate_input: length=%lu\ncontent:\n%s", length, content);
#endif
file = fopen(input, "w");
// Parsing template
for (i = 0; i < calibrate->nvariables; ++i)
{
#if DEBUG
printf("calibrate_input: variable=%u\n", i);
#endif
snprintf(buffer, 32, "@variable%u@", i + 1);
regex = g_regex_new(buffer, 0, 0, NULL);
if (i == 0)
{
buffer2 = g_regex_replace_literal(regex, content, length, 0,
calibrate->label[i], 0, NULL);
#if DEBUG
printf("calibrate_input: buffer2\n%s", buffer2);
#endif
}
else
{
length = strlen(buffer3);
buffer2 = g_regex_replace_literal(regex, buffer3, length, 0,
calibrate->label[i], 0, NULL);
g_free(buffer3);
}
g_regex_unref(regex);
length = strlen(buffer2);
snprintf(buffer, 32, "@value%u@", i + 1);
regex = g_regex_new(buffer, 0, 0, NULL);
snprintf(value, 32, calibrate->format[i],
calibrate->value[simulation * calibrate->nvariables + i]);
#if DEBUG
printf("calibrate_parse: value=%s\n", value);
#endif
buffer3 = g_regex_replace_literal(regex, buffer2, length, 0, value,
0, NULL);
g_free(buffer2);
g_regex_unref(regex);
}
// Saving input file
fwrite(buffer3, strlen(buffer3), sizeof(char), file);
g_free(buffer3);
fclose(file);
#if DEBUG
printf("calibrate_input: end\n");
#endif
}
/**
* \fn double calibrate_parse(Calibrate *calibrate, unsigned int simulation, \
* unsigned int experiment)
* \brief Function to parse input files, simulating and calculating the \
* objective function.
* \param calibrate
* \brief Calibration data.
* \param simulation
* \brief Simulation number.
* \param experiment
* \brief Experiment number.
* \return Objective function value.
*/
double calibrate_parse(Calibrate *calibrate, unsigned int simulation,
unsigned int experiment)
{
unsigned int i;
double e;
char buffer[512], input[4][32], output[32], result[32];
FILE *file_result;
#if DEBUG
printf("calibrate_parse: start\n");
printf("calibrate_parse: simulation=%u experiment=%u\n", simulation,
experiment);
#endif
// Opening input files
for (i = 0; i < calibrate->ninputs; ++i)
{
snprintf(&input[i][0], 32, "input-%u-%u-%u", i, simulation, experiment);
#if DEBUG
printf("calibrate_parse: i=%u input=%s\n", i, &input[i][0]);
#endif
calibrate_input(calibrate, simulation, &input[i][0],
calibrate->file[i][experiment]);
}
for (; i < 4; ++i) snprintf(&input[i][0], 32, "");
#if DEBUG
printf("calibrate_parse: parsing end\n");
#endif
// Performing the simulation
snprintf(output, 32, "output-%u-%u", simulation, experiment);
snprintf(result, 32, "result-%u-%u", simulation, experiment);
snprintf(buffer, 512, "./%s %s %s %s %s %s", calibrate->simulator,
&input[0][0], &input[1][0], &input[2][0], &input[3][0], output);
#if DEBUG
printf("calibrate_parse: %s\n", buffer);
#endif
system(buffer);
// Checking the objective value function
snprintf(buffer, 512, "./%s %s %s %s", calibrate->evaluator, output,
calibrate->experiment[experiment], result);
#if DEBUG
printf("calibrate_parse: %s\n", buffer);
#endif
system(buffer);
file_result = fopen(result, "r");
e = atof(fgets(buffer, 512, file_result));
fclose(file_result);
// Removing files
#if !DEBUG
snprintf(buffer, 512, "rm %s %s %s %s %s %s", &input[0][0], &input[1][0],
&input[2][0], &input[3][0], output, result);
system(buffer);
#endif
#if DEBUG
printf("calibrate_parse: end\n");
#endif
// Returning the objective function
return e;
}
/**
* \fn void calibrate_best_thread(Calibrate *calibrate, \
* unsigned int simulation, double value)
* \brief Function to save the bests simulations of a thread.
* \param calibrate
* \brief Calibration data.
* \param simulation
* \brief Simulation number.
* \param value
* \brief Objective function value.
*/
void calibrate_best_thread(Calibrate *calibrate, unsigned int simulation,
double value)
{
unsigned int i, j;
double e;
#if DEBUG
printf("calibrate_best_thread: start\n");
#endif
if (calibrate->nsaveds < calibrate->nbests
|| value < calibrate->error_best[calibrate->nsaveds - 1])
{
g_mutex_lock(&mutex);
if (calibrate->nsaveds < calibrate->nbests) ++calibrate->nsaveds;
calibrate->error_best[calibrate->nsaveds - 1] = value;
calibrate->simulation_best[calibrate->nsaveds - 1] = simulation;
for (i = calibrate->nsaveds; --i;)
{
if (calibrate->error_best[i] < calibrate->error_best[i - 1])
{
j = calibrate->simulation_best[i];
e = calibrate->error_best[i];
calibrate->simulation_best[i]
= calibrate->simulation_best[i - 1];
calibrate->error_best[i] = calibrate->error_best[i - 1];
calibrate->simulation_best[i - 1] = j;
calibrate->error_best[i - 1] = e;
}
else break;
}
g_mutex_unlock(&mutex);
}
#if DEBUG
printf("calibrate_best_thread: end\n");
#endif
}
/**
* \fn void calibrate_best_sequential(Calibrate *calibrate, \
* unsigned int simulation, double value)
* \brief Function to save the bests simulations.
* \param calibrate
* \brief Calibration data.
* \param simulation
* \brief Simulation number.
* \param value
* \brief Objective function value.
*/
void calibrate_best_sequential(Calibrate *calibrate, unsigned int simulation,
double value)
{
unsigned int i, j;
double e;
#if DEBUG
printf("calibrate_best_sequential: start\n");
#endif
if (calibrate->nsaveds < calibrate->nbests
|| value < calibrate->error_best[calibrate->nsaveds - 1])
{
if (calibrate->nsaveds < calibrate->nbests) ++calibrate->nsaveds;
calibrate->error_best[calibrate->nsaveds - 1] = value;
calibrate->simulation_best[calibrate->nsaveds - 1] = simulation;
for (i = calibrate->nsaveds; --i;)
{
if (calibrate->error_best[i] < calibrate->error_best[i - 1])
{
j = calibrate->simulation_best[i];
e = calibrate->error_best[i];
calibrate->simulation_best[i]
= calibrate->simulation_best[i - 1];
calibrate->error_best[i] = calibrate->error_best[i - 1];
calibrate->simulation_best[i - 1] = j;
calibrate->error_best[i - 1] = e;
}
else break;
}
}
#if DEBUG
printf("calibrate_best_sequential: end\n");
#endif
}
/**
* \fn void* calibrate_thread(ParallelData *data)
* \brief Function to calibrate on a thread.
* \param data
* \brief Function data.
* \return NULL
*/
void* calibrate_thread(ParallelData *data)
{
unsigned int i, j, thread;
double e;
Calibrate *calibrate;
#if DEBUG
printf("calibrate_thread: start\n");
#endif
thread = data->thread;
calibrate = data->calibrate;
#if DEBUG
printf("calibrate_thread: thread=%u start=%u end=%u\n", thread,
calibrate->thread[thread], calibrate->thread[thread + 1]);
#endif
for (i = calibrate->thread[thread]; i < calibrate->thread[thread + 1]; ++i)
{
e = 0.;
for (j = 0; j < calibrate->nexperiments; ++j)
e += calibrate_parse(calibrate, i, j);
calibrate_best_thread(calibrate, i, e);
#if DEBUG
printf("calibrate_thread: i=%u e=%lg\n", i, e);
#endif
}
#if DEBUG
printf("calibrate_thread: end\n");
#endif
g_thread_exit(NULL);
return NULL;
}
/**
* \fn void calibrate_sequential(Calibrate *calibrate)
* \brief Function to calibrate sequentially.
* \param calibrate
* \brief Calibration data pointer.
*/
void calibrate_sequential(Calibrate *calibrate)
{
unsigned int i, j;
double e;
#if DEBUG
printf("calibrate_sequential: start\n");
#endif
for (i = 0; i < calibrate->nsimulations; ++i)
{
e = 0.;
for (j = 0; j < calibrate->nexperiments; ++j)
e += calibrate_parse(calibrate, i, j);
calibrate_best_sequential(calibrate, i, e);
#if DEBUG
printf("calibrate_sequential: i=%u e=%lg\n", i, e);
#endif
}
#if DEBUG
printf("calibrate_sequential: end\n");
#endif
}
/**
* \fn void calibrate_sweep(Calibrate *calibrate)
* \brief Function to calibrate with the sweep algorithm.
* \param calibrate
* \brief Calibration data pointer.
*/
void calibrate_sweep(Calibrate *calibrate)
{
unsigned int i, j, k, l;
double e;
GThread *thread[calibrate->nthreads];
ParallelData data[calibrate->nthreads];
#if DEBUG
printf("calibrate_sweep: start\n");
#endif
for (i = 0; i < calibrate->nsimulations; ++i)
{
k = i;
for (j = 0; j < calibrate->nvariables; ++j)
{
l = k % calibrate->nsweeps[j];
k /= calibrate->nsweeps[j];
e = calibrate->rangemin[j];
if (calibrate->nsweeps[j] > 1)
e += l * (calibrate->rangemax[j] - calibrate->rangemin[j])
/ (calibrate->nsweeps[j] - 1);
calibrate->value[i * calibrate->nvariables + j] = e;
}
}
if (calibrate->nthreads <= 1)
calibrate_sequential(calibrate);
else
{
for (i = 0; i < calibrate->nthreads; ++i)
{
data[i].calibrate = calibrate;
data[i].thread = i;
thread[i] = g_thread_new(NULL, (void(*))calibrate_thread, &data[i]);
}
for (i = 0; i < calibrate->nthreads; ++i) g_thread_join(thread[i]);
}
#if DEBUG
printf("calibrate_sweep: end\n");
#endif
}
/**
* \fn void calibrate_MonteCarlo(Calibrate *calibrate)
* \brief Function to calibrate with the Monte-Carlo algorithm.
* \param calibrate
* \brief Calibration data pointer.
*/
void calibrate_MonteCarlo(Calibrate *calibrate)
{
unsigned int i, j;
GThread *thread[calibrate->nthreads];
ParallelData data[calibrate->nthreads];
#if DEBUG
printf("calibrate_MonteCarlo: start\n");
#endif
for (i = 0; i < calibrate->nsimulations; ++i)
for (j = 0; j < calibrate->nvariables; ++j)
calibrate->value[i * calibrate->nvariables + j] =
calibrate->rangemin[j] + gsl_rng_uniform(rng)
* (calibrate->rangemax[j] - calibrate->rangemin[j]);
if (calibrate->nthreads <= 1)
calibrate_sequential(calibrate);
else
{
for (i = 0; i < calibrate->nthreads; ++i)
{
data[i].calibrate = calibrate;
data[i].thread = i;
thread[i] = g_thread_new(NULL, (void(*))calibrate_thread, &data[i]);
}
for (i = 0; i < calibrate->nthreads; ++i) g_thread_join(thread[i]);
}
#if DEBUG
printf("calibrate_MonteCarlo: end\n");
#endif
}
/**
* \fn void calibrate_genetic(Calibrate *calibrate)
* \brief Function to calibrate with the Monte-Carlo algorithm.
* \param calibrate
* \brief Calibration data pointer.
*/
void calibrate_genetic(Calibrate *calibrate)
{
}
void calibrate_merge(Calibrate *calibrate, unsigned int nsaveds,
unsigned int *simulation_best, double *error_best)
{
unsigned int i, j, k, s[calibrate->nbests];
double e[calibrate->nbests];
i = j = k = 0;
do
{
if (i > calibrate->nsaveds
|| calibrate->error_best[i] > error_best[j])
{
s[k] = simulation_best[j];
e[k] = error_best[j];
++j;
}
else
{
s[k] = calibrate->simulation_best[i];
e[k] = calibrate->error_best[i];
++i;
}
++k;
}
while (k < calibrate->nbests && (i < calibrate->nsaveds || j < nsaveds));
calibrate->nsaveds = k;
for (i = 0; i < k; ++i)
{
calibrate->simulation_best[i] = s[i];
calibrate->error_best[i] = e[i];
}
}
/**
* \fn int calibrate_new(Calibrate *calibrate, char *filename)
* \brief Function to open and perform a calibration.
* \param calibrate
* \brief Calibration data pointer.
* \param filename
* \brief Input data file name.
* \return 1 on success, 0 on error.
*/
int calibrate_new(Calibrate *calibrate, char *filename)
{
unsigned int i, j;
char buffer2[512];
xmlChar *buffer;
xmlNode *node, *child;
xmlDoc *doc;
#if HAVE_MPI
unsigned int nsaveds, *simulation_best;
double *error_best;
MPI_Status mpi_stat;
#endif
static const xmlChar *template[4]=
{XML_TEMPLATE1, XML_TEMPLATE2, XML_TEMPLATE3, XML_TEMPLATE4};
#if DEBUG
printf("calibrate_new: start\n");
#endif
// Parsing the XML data file
doc = xmlParseFile(filename);
if (!doc)
{
printf("Unable to parse the data file %s\n", filename);
return 0;
}
// Obtaining the root XML node
node = xmlDocGetRootElement(doc);
if (!node)
{
printf("No XML nodes in the data file\n");
return 0;
}
if (xmlStrcmp(node->name, XML_CALIBRATE))
{
printf("Bad name of the XML root node in the data file\n");
return 0;
}
// Obtaining the simulator file
if (xmlHasProp(node, XML_SIMULATOR))
{
calibrate->simulator = (char*)xmlGetProp(node, XML_SIMULATOR);
}
else
{
printf("No simulator in the data file\n");
return 0;
}
// Obtaining the evaluator file
if (xmlHasProp(node, XML_EVALUATOR))
{
calibrate->evaluator = (char*)xmlGetProp(node, XML_EVALUATOR);
}
else
{
printf("No error in the data file\n");
return 0;
}
// Reading the algorithm
if (xmlHasProp(node, XML_ALGORITHM))
{
buffer = xmlGetProp(node, XML_ALGORITHM);
if (!xmlStrcmp(buffer, XML_SWEEP))
{
calibrate->algorithm = CALIBRATE_ALGORITHM_SWEEP;
}
else
{
calibrate->algorithm = CALIBRATE_ALGORITHM_GENETIC;
}
xmlFree(buffer);
}
else
{
calibrate->algorithm = CALIBRATE_ALGORITHM_MONTE_CARLO;
// Obtaining the simulations number
if (xmlHasProp(node, XML_SIMULATIONS))
{
buffer = xmlGetProp(node, XML_SIMULATIONS);
calibrate->nsimulations = strtoul((char*)buffer, NULL, 0);
xmlFree(buffer);
}
else
{
printf("No simulations number in the data file\n");
return 0;
}
}
// Reading the iterations number
if (xmlHasProp(node, XML_ITERATIONS))
{
buffer = xmlGetProp(node, XML_ITERATIONS);
calibrate->niterations = strtoul((char*)buffer, NULL, 0);
xmlFree(buffer);
if (!calibrate->niterations)
{
printf("Null iterations number in the data file\n");
return 0;
}
}
else calibrate->niterations = 1;
// Reading the best simulations number
if (xmlHasProp(node, XML_BESTS))
{
buffer = xmlGetProp(node, XML_BESTS);
calibrate->nbests = strtoul((char*)buffer, NULL, 0);
xmlFree(buffer);
if (!calibrate->nbests)
{
printf("Null bests number in the data file\n");
return 0;
}
}
else calibrate->nbests = 1;
calibrate->simulation_best
= (unsigned int*)alloca(calibrate->nbests * sizeof(unsigned int));
calibrate->error_best = (double*)alloca(calibrate->nbests * sizeof(double));
#if HAVE_MPI
simulation_best
= (unsigned int*)alloca(calibrate->nbests * sizeof(unsigned int));
error_best = (double*)alloca(calibrate->nbests * sizeof(double));
#endif
calibrate->nsaveds = 0;
// Reading the experimental data file names
calibrate->nexperiments = 0;
calibrate->experiment = NULL;
for (i = 0; i < 4; ++i)
{
calibrate->template[i] = NULL;
calibrate->file[i] = NULL;
}
for (child = node->children; child; child = child->next)
{
if (xmlStrcmp(child->name, XML_EXPERIMENT)) break;
#if DEBUG
printf("calibrate_new: nexperiments=%u\n", calibrate->nexperiments);
#endif
if (xmlHasProp(child, XML_NAME))
{
calibrate->experiment = realloc(calibrate->experiment,
(1 + calibrate->nexperiments) * sizeof(char*));
calibrate->experiment[calibrate->nexperiments] =
(char*)xmlGetProp(child, XML_NAME);
}
else
{
printf("No experiment %u file name\n", calibrate->nexperiments + 1);
return 0;
}
if (!calibrate->nexperiments) calibrate->ninputs = 0;
#if DEBUG
printf("calibrate_new: template[0]\n");
#endif
if (xmlHasProp(child, XML_TEMPLATE1))
{
calibrate->template[0] = realloc(calibrate->template[0],
(1 + calibrate->nexperiments) * sizeof(char*));
calibrate->template[0][calibrate->nexperiments] =
(char*)xmlGetProp(child, template[0]);
calibrate->file[0] = realloc(calibrate->file[0],
(1 + calibrate->nexperiments) * sizeof(GMappedFile*));
#if DEBUG
printf("calibrate_new: experiment=%u template1=%s\n", calibrate->nexperiments,
calibrate->template[0][calibrate->nexperiments]);
#endif
calibrate->file[0][calibrate->nexperiments] =
g_mapped_file_new
(calibrate->template[0][calibrate->nexperiments], 0, NULL);
if (!calibrate->nexperiments) ++calibrate->ninputs;
#if DEBUG
printf("calibrate_new: ninputs=%u\n", calibrate->ninputs);
#endif
}
else
{
printf("No experiment %u template1\n", calibrate->nexperiments + 1);
return 0;
}
for (j = 1; j < 4; ++j)
{
#if DEBUG
printf("calibrate_new: template%j\n", j + 1);
#endif
if (xmlHasProp(child, template[j]))
{
if (calibrate->nexperiments && calibrate->ninputs < 2)
{
printf("Experiment %u: bad templates number\n",
calibrate->nexperiments + 1);
return 0;
}
calibrate->template[j] = realloc(calibrate->template[j],
(1 + calibrate->nexperiments) * sizeof(char*));
calibrate->template[j][calibrate->nexperiments] =
(char*)xmlGetProp(child, template[j]);
calibrate->file[j] = realloc(calibrate->file[j],
(1 + calibrate->nexperiments) * sizeof(GMappedFile*));
#if DEBUG
printf("calibrate_new: experiment=%u template%u=%s\n", calibrate->nexperiments,
j + 1, calibrate->template[j][calibrate->nexperiments]);
#endif
calibrate->file[j][calibrate->nexperiments] =
g_mapped_file_new
(calibrate->template[j][calibrate->nexperiments], 0,
NULL);
if (!calibrate->nexperiments) ++calibrate->ninputs;
#if DEBUG
printf("calibrate_new: ninputs=%u\n", calibrate->ninputs);
#endif
}
else if (calibrate->nexperiments && calibrate->ninputs > 1)
{
printf("No experiment %u template%u\n",
calibrate->nexperiments + 1, j + 1);
return 0;
}
else break;
}
++calibrate->nexperiments;
#if DEBUG
printf("calibrate_new: nexperiments=%u\n", calibrate->nexperiments);
#endif
}
if (!calibrate->nexperiments)
{
printf("No calibration experiments\n");
return 0;
}
// Reading the variables data
calibrate->nvariables = 0;
calibrate->label = NULL;
calibrate->rangemin = NULL;
calibrate->rangemax = NULL;
calibrate->format = NULL;
calibrate->nsweeps = NULL;
if (calibrate->algorithm == CALIBRATE_ALGORITHM_SWEEP)
calibrate->nsimulations = 1;
for (; child; child = child->next)
{
if (xmlStrcmp(child->name, XML_VARIABLE))
{
printf("Bad XML node\n");
return 0;
}
if (xmlHasProp(child, XML_NAME))
{
calibrate->label = realloc(calibrate->label,
(1 + calibrate->nvariables) * sizeof(char*));
calibrate->label[calibrate->nvariables] =
(char*)xmlGetProp(child, XML_NAME);
}
else
{
printf("No variable %u name\n", calibrate->nvariables + 1);
return 0;
}
if (xmlHasProp(child, XML_MINIMUM))
{
calibrate->rangemin = realloc(calibrate->rangemin,
(1 + calibrate->nvariables) * sizeof(double));
buffer = xmlGetProp(child, XML_MINIMUM);
calibrate->rangemin[calibrate->nvariables] = atof((char*)buffer);
xmlFree(buffer);
}
else
{
printf("No variable %u minimum range\n", calibrate->nvariables + 1);
return 0;
}
if (xmlHasProp(child, XML_MAXIMUM))
{
calibrate->rangemax = realloc(calibrate->rangemax,
(1 + calibrate->nvariables) * sizeof(double));
buffer = xmlGetProp(child, XML_MAXIMUM);
calibrate->rangemax[calibrate->nvariables] = atof((char*)buffer);
xmlFree(buffer);
}
else
{
printf("No variable %u maximum range\n", calibrate->nvariables + 1);
return 0;
}
calibrate->format = realloc(calibrate->format,
(1 + calibrate->nvariables) * sizeof(char*));
if (xmlHasProp(child, XML_FORMAT))
{
calibrate->format[calibrate->nvariables] =
(char*)xmlGetProp(child, XML_FORMAT);
}
else
{
calibrate->format[calibrate->nvariables] =
(char*)xmlStrdup(DEFAULT_FORMAT);
}
if (calibrate->algorithm == CALIBRATE_ALGORITHM_SWEEP)
{
if (xmlHasProp(child, XML_SWEEPS))
{
calibrate->nsweeps = realloc(calibrate->nsweeps,
(1 + calibrate->nvariables) * sizeof(unsigned int));
buffer = xmlGetProp(child, XML_SWEEPS);
calibrate->nsweeps[calibrate->nvariables] =
strtoul((char*)buffer, NULL, 0);
xmlFree(buffer);
}
else
{
printf("No variable %u sweeps number\n",
calibrate->nvariables + 1);
return 0;
}
calibrate->nsimulations *=
calibrate->nsweeps[calibrate->nvariables];
#if DEBUG
printf("calibrate_new: nsweeps=%u nsimulations=%u\n",
calibrate->nsweeps[calibrate->nvariables], calibrate->nsimulations);
#endif
}
++calibrate->nvariables;
}
if (!calibrate->nvariables)
{
printf("No calibration variables\n");
return 0;
}
#if DEBUG
printf("calibrate_new: nvariables=%u\n", calibrate->nvariables);
#endif
// Allocating values
calibrate->value = (double*)alloca(calibrate->nsimulations *
calibrate->nvariables * sizeof(double));
// Calculating simulations to perform on each task
#ifdef HAVE_MPI
calibrate->nstart = calibrate->mpi_rank * calibrate->nsimulations
/ calibrate->mpi_tasks;
calibrate->nend = (1 + calibrate->mpi_rank) * calibrate->nsimulations
/ calibrate->mpi_tasks;
#else
calibrate->nstart = 0;
calibrate->nend = calibrate->nsimulations;
#endif
#if DEBUG
printf("calibrate_new: nstart=%u nend=%u\n", calibrate->nstart,
calibrate->nend);
#endif
// Calculating simulations to perform on each thread
calibrate->thread =
(unsigned int*)alloca((1 + calibrate->nthreads) * sizeof(unsigned int));
for (i = 0; i <= calibrate->nthreads; ++i)
calibrate->thread[i] = calibrate->nstart
+ i * (calibrate->nend - calibrate->nstart) / calibrate->nthreads;
// Performing the algorithm
switch (calibrate->algorithm)
{
// Sweep algorithm
case CALIBRATE_ALGORITHM_SWEEP:
calibrate_sweep(calibrate);
break;
// Genetic algorithm
case CALIBRATE_ALGORITHM_GENETIC:
calibrate_genetic(calibrate);
break;
// Default Monte-Carlo algorithm
default: