-
Notifications
You must be signed in to change notification settings - Fork 7
/
cosimulation_interface.c
881 lines (804 loc) · 33.1 KB
/
cosimulation_interface.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
///////////////////////////////////////////////////////////////////////////////
///
/// \file cosimulation_interface.c
///
/// \brief Functions for cosimulation
///
/// \author Wangda Zuo
/// University of Miami
/// W.Zuo@miami.edu
///
/// \date 8/3/2013
///
/// This file provides functions that are used for conducting the cosimulaiton
/// with Modelica
///
///////////////////////////////////////////////////////////////////////////////
#include "cosimulation_interface.h"
///////////////////////////////////////////////////////////////////////////////
/// Read the cosimulation parameters defined by Modelica
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX pointer to boudnary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int read_cosim_parameter(PARA_DATA *para, REAL **var, int **BINDEX) {
int i;
ffd_log("-------------------------------------------------------------------",
FFD_NORMAL);
ffd_log("read_cosim_parameter(): "
"Received the following cosimulation parameters:",
FFD_NORMAL);
/****************************************************************************
| Compare number of solid surface boundaries
| (Wall, Window Glass and Window Frame)
****************************************************************************/
if(para->cosim->para->nSur==para->bc->nb_wall) {
sprintf(msg, "\tnSur=%d", para->cosim->para->nSur);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"read_cosim_parameter(): Modelica(%d) and FFD(%d) "
"have different number of solid surfaces.",
para->cosim->para->nSur, para->bc->nb_wall);
ffd_log(msg, FFD_ERROR);
ffd_log("\tModleica Surfaces are:", FFD_NORMAL);
for(i=0; i<para->cosim->para->nSur; i++) {
sprintf(msg, "\t\t%s", para->cosim->para->name[i]);
ffd_log(msg, FFD_NORMAL);
}
ffd_log("\tFFD Surfaces are:", FFD_NORMAL);
for(i=0; i<para->bc->nb_wall; i++) {
sprintf(msg, "\t\t%s", para->bc->wallName[i]);
ffd_log(msg, FFD_NORMAL);
}
return 1;
}
/****************************************************************************
| Compare the number of fluid ports
****************************************************************************/
if(para->cosim->para->nPorts==para->bc->nb_port) {
sprintf(msg, "\tnPorts=%d", para->cosim->para->nPorts);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"read_cosim_parameter(): Modelica(%d) and FFD(%d) "
"have different number of fluid ports.",
para->cosim->para->nPorts, para->bc->nb_port);
ffd_log(msg, FFD_ERROR);
return 1;
}
/****************************************************************************
| Compare the number of sensors
****************************************************************************/
if(para->cosim->para->nSen==para->sens->nb_sensor) {
sprintf(msg, "\tnSen=%d", para->cosim->para->nSen);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"read_cosim_parameter(): Modelica(%d) and FFD(%d) "
"have different number of sensors.",
para->cosim->para->nSen, para->sens->nb_sensor);
ffd_log(msg, FFD_ERROR);
return 1;
}
/****************************************************************************
| Compare the number of trace species
****************************************************************************/
if(para->cosim->para->nXi==para->bc->nb_Xi) {
sprintf(msg, "\tnXi=%d", para->cosim->para->nXi);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"read_cosim_parameter(): Modelica(%d) and FFD(%d) "
"have different number of species.",
para->cosim->para->nXi, para->bc->nb_Xi);
ffd_log(msg, FFD_ERROR);
return 1;
}
/****************************************************************************
| Compare the number of substances
****************************************************************************/
if(para->cosim->para->nC==para->bc->nb_C) {
sprintf(msg, "\tnC=%d", para->cosim->para->nC);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"read_cosim_parameter(): Modelica(%d) and FFD(%d) "
"have different number of species.",
para->cosim->para->nC, para->bc->nb_C);
ffd_log(msg, FFD_ERROR);
return 1;
}
sprintf(msg, "\tnConExtWin=%d", para->cosim->para->nConExtWin);
ffd_log(msg, FFD_NORMAL);
sprintf(msg, "\tsha=%d", para->cosim->para->sha);
ffd_log(msg, FFD_NORMAL);
/****************************************************************************
| Print the information for surface boundaries
****************************************************************************/
for(i=0; i<para->cosim->para->nSur; i++) {
sprintf(msg, "\tSurface %d: %s", i, para->cosim->para->name[i]);
ffd_log(msg, FFD_NORMAL);
sprintf(msg, "\t\tArea:%f[m2],\t Tilt:%f[deg]",
para->cosim->para->are[i], para->cosim->para->til[i]);
ffd_log(msg, FFD_NORMAL);
switch (para->cosim->para->bouCon[i]) {
case 1:
ffd_log("\t\tThermal boundary: Fixed tempearture", FFD_NORMAL);
break;
case 2:
ffd_log("\t\tThermal boundary: Fixed heat flux", FFD_NORMAL);
break;
default:
sprintf(msg,
"Invalid value (%d) for thermal boundary condition. "
"1: Fixed T; 2: Fixed heat flux",
para->cosim->para->bouCon[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
}
for(i=0; i<para->cosim->para->nPorts; i++) {
sprintf(msg, "\tFluid Ports %d: %s", i, para->cosim->para->portName[i]);
ffd_log(msg, FFD_NORMAL);
}
for(i=0; i<para->cosim->para->nSen; i++) {
sprintf(msg, "\tSensor %d: %s", i, para->cosim->para->sensorName[i]);
ffd_log(msg, FFD_NORMAL);
}
/****************************************************************************
| Compare name and surface area of boundaries
****************************************************************************/
if(compare_boundary_names(para)!=0) {
ffd_log("read_cosim_parameter(): The boundary names were not consistent.",
FFD_ERROR);
return 1;
}
else if(compare_boundary_area(para, var, BINDEX)!=0) {
ffd_log("read_cosim_parameter(): The boundary areas were not consistent.",
FFD_ERROR);
return 1;
}
return 0;
} // End of read_cosim_parameter()
///////////////////////////////////////////////////////////////////////////////
/// Read the data from Modelica
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int read_cosim_data(PARA_DATA *para, REAL **var, int **BINDEX) {
int i;
ffd_log("-------------------------------------------------------------------",
FFD_NORMAL);
ffd_log("read_cosim_data(): Start to read data from Modelica.",
FFD_NORMAL);
/****************************************************************************
| Wait for data to be updated by the other program
****************************************************************************/
while(para->cosim->modelica->flag==0) {
sprintf(msg,
"read_cosim_data(): Data is not ready with "
"para->cosim->modelica->flag=%d",
para->cosim->modelica->flag);
ffd_log(msg, FFD_NORMAL);
Sleep(1000);
}
sprintf(msg,
"read_cosim_data(): Received the following data at t=%f[s]",
para->cosim->modelica->t);
ffd_log(msg, FFD_NORMAL);
/****************************************************************************
| Read and assign the thermal boundary conditions
****************************************************************************/
if(assign_thermal_bc(para,var,BINDEX)!=0) {
ffd_log("read_cosim_data(): Could not assign the Modelicathermal data to FFD",
FFD_ERROR);
return 1;
}
/****************************************************************************
| Read and assign the shading boundary conditions
| Warning: This is not been used in current version
****************************************************************************/
if(para->cosim->para->sha==1) {
ffd_log("Shading control signal and absorded radiation by the shade:",
FFD_NORMAL);
for(i=0; i<para->cosim->para->nConExtWin; i++) {
sprintf(msg, "Surface[%d]: %f,\t%f\n",
i, para->cosim->modelica->shaConSig[i],
para->cosim->modelica->shaAbsRad[i]);
ffd_log(msg, FFD_NORMAL);
}
}
else
ffd_log("\tNo shading devices.", FFD_NORMAL);
/****************************************************************************
| Read and assign the inlet conditions
****************************************************************************/
if(para->cosim->para->nPorts>0) {
if(assign_port_bc(para,var,BINDEX)!=0) {
ffd_log(" read_cosim_data(): Could not assign the Modelica inlet BC to FFD",
FFD_ERROR);
return 1;
}
}
else
ffd_log("\tNo fluid ports.", FFD_NORMAL);
/****************************************************************************
| Post-Process after reading the data
****************************************************************************/
// Change the flag to indicate that the data has been read
para->cosim->modelica->flag = 0;
printf("para->cosim->modelica->flag=%d\n", para->cosim->modelica->flag);
ffd_log("read_cosim_data(): Ended reading data from Modelica.",
FFD_NORMAL);
return 0;
} // End of read_cosim_data()
///////////////////////////////////////////////////////////////////////////////
/// Write the FFD data for Modelica
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int write_cosim_data(PARA_DATA *para, REAL **var) {
int i, j, id;
ffd_log("-------------------------------------------------------------------",
FFD_NORMAL);
ffd_log("writed_cosim_parameter(): "
"Start to write the following cosimulation data:",
FFD_NORMAL);
/****************************************************************************
| Wait if the previosu data has not been read by Modelica
****************************************************************************/
while(para->cosim->ffd->flag==1) {
ffd_log("write_cosim_data(): Wait since previosu data is not taken "
"by Modelica", FFD_NORMAL);
Sleep(1000);
}
/****************************************************************************
| Start to write new data
****************************************************************************/
para->cosim->ffd->t = para->mytime->t;
sprintf(msg, "write_cosim_data(): Start to update FFD data at t=%f[s]",
para->cosim->ffd->t);
ffd_log(msg, FFD_NORMAL);
/****************************************************************************
| Set the time and space averaged temperature of space
| Convert T from degC to K
****************************************************************************/
para->cosim->ffd->TRoo = average_volume(para, var, var[TEMPM]);
sprintf(msg, "\tAveraged room temperature %f[degC]", para->cosim->ffd->TRoo);
para->cosim->ffd->TRoo += 273.15;
ffd_log(msg, FFD_NORMAL);
/****************************************************************************
| Set temperature of shading devices
****************************************************************************/
if(para->cosim->para->sha==1) {
ffd_log("\tTemperature of the shade:", FFD_NORMAL);
for(i=0; i<para->cosim->para->nConExtWin; i++) {
//Fixme: The shade feature is to be implemented
para->cosim->ffd->TSha[i] = 20 + 273.15;
sprintf(msg, "\t\tSurface %d: %f[K]\n",
i, para->cosim->ffd->TSha[i]);
ffd_log(msg, FFD_NORMAL);
}
}
/****************************************************************************
| Set data for fluid ports
****************************************************************************/
ffd_log("\tFlow information at the ports:", FFD_NORMAL);
for(i=0; i<para->bc->nb_port; i++) {
// Get the corresponding ID in modelica
id = para->bc->portId[i];
// Assign the temperature
para->cosim->ffd->TPor[id] = para->bc->TPortMean[i]/para->bc->velPortMean[i]
+ 273.15;
sprintf(msg, "\t\t%s: %f[K]",
para->cosim->para->portName[id], para->cosim->ffd->TPor[id]);
ffd_log(msg, FFD_NORMAL);
// Assign the Xi
for(j=0; j<para->bc->nb_Xi; j++)
para->cosim->ffd->XiPor[id][j] = para->bc->XiPortMean[i][j]
/ para->bc->velPortMean[i];
// Assign the C
for(j=0; j<para->bc->nb_C; j++)
para->cosim->ffd->CPor[id][j] = para->bc->CPortMean[i][j]
/ para->bc->velPortMean[i];
}
/****************************************************************************
| Set data for solid surfaces
****************************************************************************/
ffd_log("\tInformation at solid surfaces:", FFD_NORMAL);
for(i=0; i<para->bc->nb_wall; i++) {
id = para->bc->wallId[i];
if(para->cosim->para->bouCon[id]==2) {
para->cosim->ffd->temHea[id] = para->bc->temHeaMean[i]
/ para->bc->AWall[i];
sprintf(msg, "\t\t%s: %f[K]",
para->cosim->para->name[id], para->cosim->ffd->temHea[id]);
}
else {
para->cosim->ffd->temHea[id] = para->bc->temHeaMean[i];
sprintf(msg, "\t\t%s: %f[W]",
para->cosim->para->name[id], para->cosim->ffd->temHea[id]);
}
ffd_log(msg, FFD_NORMAL);
}
/****************************************************************************
| Set data for sensors
****************************************************************************/
if (set_sensor_data(para, var)!=0) {
ffd_log("\tCould not get sensor data", FFD_ERROR);
return 1;
}
else
ffd_log("\tSensor Information:", FFD_NORMAL);
for(i=0; i<para->cosim->para->nSen; i++) {
para->cosim->ffd->senVal[i] = para->sens->senVal[i];
sprintf(msg, "\t\t%s: %f",
para->cosim->para->sensorName[i], para->cosim->ffd->senVal[i]);
ffd_log(msg, FFD_NORMAL);
}
/****************************************************************************
| Inform Modelica the data is updated
****************************************************************************/
para->cosim->ffd->flag = 1;
return 0;
} // End of write_cosim_data()
///////////////////////////////////////////////////////////////////////////////
/// Compare the names of boundaries and store the relationship
///
///\param para Pointer to FFD parameters
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int compare_boundary_names(PARA_DATA *para) {
int i, j, flag;
char **name1 = para->cosim->para->name;
char **name2 = para->bc->wallName;
char **name3 = para->cosim->para->portName;
char **name4 = para->bc->portName;
/****************************************************************************
| Compare the names of solid surfaces
****************************************************************************/
for(i=0; i<para->cosim->para->nSur; i++) {
/*-------------------------------------------------------------------------
| Assume we do not find the name
-------------------------------------------------------------------------*/
flag = 1;
/*-------------------------------------------------------------------------
| Check the wall names in FFD
-------------------------------------------------------------------------*/
for(j=0; j<para->bc->nb_wall&&flag!=0; j++) {
flag = strcmp(name1[i], name2[j]);
// If found the name
if(flag==0) {
// If the same name has been found before
if(para->bc->wallId[j]>0) {
sprintf(msg, "compare_boundary_names(): Modelica has "
"the same name \"%s\" for two BCs.", name1[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
// If no same name has been found before, use it
else {
sprintf(msg,
"compare_boundary_names(): Matched boundary name \"%s\".",
name1[i]);
ffd_log(msg, FFD_NORMAL);
para->bc->wallId[j] = i;
}
} // End of if(flag==0)
} // End of for(j=0; j<para->bc->nb_wall&&flag!=0; j++)
/*-------------------------------------------------------------------------
| Stop if name is not found
-------------------------------------------------------------------------*/
if(flag!=0) {
sprintf(msg, "compare_boundary_names(): Could not find the Modelica "
" wall boundary \"%s\" in FFD.", name1[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
} // Next Modelica Wall name
/****************************************************************************
| Compare the names of fluid ports
****************************************************************************/
ffd_log("Start to compare port names", FFD_NORMAL);
for(i=0; i<para->cosim->para->nPorts; i++) {
/*-------------------------------------------------------------------------
| Assume we do not find the name
-------------------------------------------------------------------------*/
flag = 1;
sprintf(msg, "name3[%d]=%s", i, name3[i]);
ffd_log(msg, FFD_NORMAL);
/*-------------------------------------------------------------------------
| Check the FFD inlet and outlet names
-------------------------------------------------------------------------*/
for(j=0; j<para->bc->nb_port&&flag!=0; j++) {
flag = strcmp(name3[i], name4[j]);
sprintf(msg, "name4[%d]=%s", j, name4[j]);
ffd_log(msg, FFD_NORMAL);
sprintf(msg, "portId[%d]=%d", j, para->bc->portId[j]);
ffd_log(msg, FFD_NORMAL);
// If found the name
if(flag==0) {
// If the same name has been found before
if(para->bc->portId[j]>0) {
sprintf(msg,
"compare_boundary_names(): Modelica has the same name \"%s\" for two BCs.",
name3[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
// If no same name has been found before, use it
else {
sprintf(msg,
"compare_boundary_names(): Matched boundary name \"%s\".",
name3[i]);
ffd_log(msg, FFD_NORMAL);
para->bc->portId[j] = i;
}
} // End of if(flag==0)
}
/*-------------------------------------------------------------------------
| Stop if name is not found
-------------------------------------------------------------------------*/
if(flag!=0) {
sprintf(msg, "compare_boundary_names(): Could not find"
"the Modelica fluid port boundary \"%s\" in FFD.", name3[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
} // Next Modelica port name
return 0;
} // End of compare_boundary_names()
///////////////////////////////////////////////////////////////////////////////
/// Compare the area of boundaries
///
///\param para Pointer to FFD parameters
///\param var Pointer to the FFD simulaiton variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int compare_boundary_area(PARA_DATA *para, REAL **var, int **BINDEX) {
int i, j;
REAL *A0 = para->bc->AWall, *A1 = para->cosim->para->are;
ffd_log("compare_boundary_area(): "
"Start to compare the area of solid surfaces.",
FFD_NORMAL);
for(i=0; i<para->bc->nb_wall; i++) {
j = para->bc->wallId[i];
if(fabs(A0[i]-A1[j])<SMALL) {
sprintf(msg, "\t%s has the same area of %f[m2]",
para->bc->wallName[i], A0[i]);
ffd_log(msg, FFD_NORMAL);
}
else {
sprintf(msg,
"compare_boundary_area(): Area of surface %s are different: "
"Modelica (%f[m2]) and FFD (%f[m2])",
para->bc->wallName[i], A1[j], A0[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
}
return 0;
} // End of compare_boundary_area()
///////////////////////////////////////////////////////////////////////////////
/// Assign the Modelica solid surface thermal boundary condition data to FFD
///
///\param para Pointer to FFD parameters
///\param var Pointer to the FFD simulaiton variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int assign_thermal_bc(PARA_DATA *para, REAL **var, int **BINDEX) {
int i, j, k, it, id, modelicaId;
int imax = para->geom->imax, jmax = para->geom->jmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL *temHea;
/****************************************************************************
| Assign the boundary conditon if there is a solid surface
****************************************************************************/
if(para->bc->nb_wall>0) {
ffd_log("assign_thermal_bc(): Thermal conditions for solid surfaces:",
FFD_NORMAL);
temHea = (REAL *) malloc(para->bc->nb_wall*sizeof(REAL));
if(temHea==NULL) {
ffd_log("assign_thermal_bc(): Could not allocate memory for temHea.",
FFD_ERROR);
return 1;
}
//-------------------------------------------------------------------------
// Convert the data from Modelica order to FFD order
//-------------------------------------------------------------------------
for(j=0; j<para->bc->nb_wall; j++) {
i = para->bc->wallId[j];
switch(para->cosim->para->bouCon[i]) {
case 1: // Temperature
temHea[j] = para->cosim->modelica->temHea[i] - 273.15;
sprintf(msg, "\t%s: T=%f[degC]",
para->bc->wallName[j], temHea[j]);
ffd_log(msg, FFD_NORMAL);
break;
case 2: // Heat flow rate
temHea[j] = para->cosim->modelica->temHea[i] / para->bc->AWall[j];
sprintf(msg, "\t%s: Q_dot=%f[W/m2]",
para->bc->wallName[j], temHea[j]);
ffd_log(msg, FFD_NORMAL);
break;
default:
sprintf(msg,
"Invalid value (%d) for thermal boundary condition. "
"Expected value are 1->Fixed T; 2->Fixed heat flux",
para->cosim->para->bouCon[i]);
ffd_log(msg, FFD_ERROR);
return 1;
}
}
//-------------------------------------------------------------------------
// Assign the BC
//-------------------------------------------------------------------------
for(it=0; it<para->geom->index; it++) {
i = BINDEX[0][it];
j = BINDEX[1][it];
k = BINDEX[2][it];
id = BINDEX[4][it];
modelicaId = para->bc->wallId[id];
if(var[FLAGP][IX(i,j,k)]==SOLID)
switch(para->cosim->para->bouCon[modelicaId]) {
case 1:
// Need to convert the T from K to degC
var[TEMPBC][IX(i,j,k)] = temHea[id];
BINDEX[3][it] = 1; // Specified temperature
break;
case 2:
var[QFLUXBC][IX(i,j,k)] = temHea[id];
BINDEX[3][it] = 0; // Specified heat flux
break;
default:
sprintf(msg,
"assign_thermal_bc(): Thermal bc value BINDEX[3][%d]=%d "
"at [%d,%d,%d] was not valid.",
it, BINDEX[3][it], i, j, k);
ffd_log(msg, FFD_ERROR);
return 1;
} // End of switch(BINDEX[3][it])
}
free(temHea);
} // End of if(para->bc->nb_wall>0)
/****************************************************************************
| No action since there is not a solid surface
****************************************************************************/
else
ffd_log("assign_thermal_bc(): No solid surfaces:", FFD_NORMAL);
return 0;
} // End of assign_thermal_bc()
///////////////////////////////////////////////////////////////////////////////
/// Assign the Modelica inlet and outlet boundary condition data to FFD
///
/// The inlet and outlet boundaries are not fixed and they can change during
/// the simulation. The reason is that the Modelica uses acausal modeling
/// and the flow direction can change during the simulation depending on the
/// pressure difference. As a result, the FFD has to change its inlet and outlet
/// boundry condition accordingly. The inlet or outlet boundary is decided
/// according to the flow rate para->cosim->modelica->mFloRarPor. The port is
/// inlet if mFloRarPor>0 and outlet if mFloRarPor<0. We will need to reset the
/// var[FLAGP][IX(i,j,k)] to apply the change of boundary conditions.
///
///\param para Pointer to FFD parameters
///\param var Pointer to the FFD simulaiton variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int assign_port_bc(PARA_DATA *para, REAL **var, int **BINDEX) {
int i, j, k, it, id;
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
ffd_log("assign_port_bc():", FFD_NORMAL);
/****************************************************************************
| Convert the data from Modelica to FFD for the Inlet
****************************************************************************/
for(j=0; j<para->bc->nb_port; j++) {
i = para->bc->portId[j];
/*-------------------------------------------------------------------------
| Convert for mass flow rate and temperature
-------------------------------------------------------------------------*/
para->bc->velPort[j] = para->cosim->modelica->mFloRatPor[i]
/ (para->prob->rho*para->bc->APort[j]);
para->bc->TPort[j] = para->cosim->modelica->TPor[i] - 273.15;
sprintf(msg, "\t%s: vel=%f[m/s], T=%f[degC]",
para->bc->portName[j], para->bc->velPort[j],
para->bc->TPort[j]);
ffd_log(msg, FFD_NORMAL);
/*-------------------------------------------------------------------------
| Convert nXi types of trace substance
-------------------------------------------------------------------------*/
for(k=0; k<para->cosim->para->nXi; k++) {
para->bc->XiPort[j][k] = para->cosim->modelica->XiPor[i][k];
sprintf(msg, "\tXi[%d]=%f", k, para->bc->XiPort[j][k]);
ffd_log(msg, FFD_NORMAL);
}
/*-------------------------------------------------------------------------
| Convert nC types of species
-------------------------------------------------------------------------*/
for(k=0; k<para->cosim->para->nC; k++) {
para->bc->CPort[j][k] = para->cosim->modelica->CPor[i][k];
sprintf(msg, "\tC[%d]=%f", k, para->bc->CPort[j][k]);
ffd_log(msg, FFD_NORMAL);
}
}
/****************************************************************************
| Assign the BC
****************************************************************************/
for(it=0; it<para->geom->index; it++) {
i = BINDEX[0][it];
j = BINDEX[1][it];
k = BINDEX[2][it];
id = BINDEX[4][it];
if(var[FLAGP][IX(i,j,k)]==INLET || var[FLAGP][IX(i,j,k)]==OUTLET) {
if(para->bc->velPort[id]>=0) {
var[FLAGP][IX(i,j,k)] = INLET;
var[TEMPBC][IX(i,j,k)] = para->bc->TPort[id];
if(i==0)
var[VXBC][IX(i,j,k)] = para->bc->velPort[id];
else if(i==imax+1)
var[VXBC][IX(i,j,k)] = -para->bc->velPort[id];
if(j==0)
var[VYBC][IX(i,j,k)] = para->bc->velPort[id];
else if(j==jmax+1)
var[VYBC][IX(i,j,k)] = -para->bc->velPort[id];
if(k==0)
var[VZBC][IX(i,j,k)] = para->bc->velPort[id];
else if(k==kmax+1)
var[VZBC][IX(i,j,k)] = -para->bc->velPort[id];
}
// Set it to outlet if flow out of room
else
var[FLAGP][IX(i,j,k)] = OUTLET;
}
}
return 0;
} // End of assign_inlet_outlet_bc()
///////////////////////////////////////////////////////////////////////////////
/// Integrate the cosimulation exchange data over the surfaces
///
/// Fluid port:
/// - T/Xi/C: sum(u*T*dA)
/// - m_dot: sum(u*dA)
///
/// Solid Surface Boundary:
/// - T: sum(T*dA)
/// - Q_dot: sum(q_dot*dA)
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX Pointer to the boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int surface_integrate(PARA_DATA *para, REAL **var, int **BINDEX) {
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int i, j, k, it, bcid;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL vel_tmp, A_tmp;
/****************************************************************************
| Set the variable to 0
****************************************************************************/
for(i=0; i<para->bc->nb_wall; i++)
para->bc->temHeaAve[i] = 0;
for(i=0; i<para->bc->nb_port; i++) {
para->bc->TPortAve[i] = 0;
para->bc->velPortAve[i] = 0;
for(j=0; j<para->bc->nb_Xi; j++)
para->bc->XiPortAve[i] = 0;
for(j=0; j<para->bc->nb_C; j++)
para->bc->CPortAve[i] = 0;
}
/****************************************************************************
| Go through all the boundary cells
****************************************************************************/
for(it=0; it<para->geom->index; it++) {
i = BINDEX[0][it];
j = BINDEX[1][it];
k = BINDEX[2][it];
bcid = BINDEX[4][it];
if(i==0 || i==imax+1) {
vel_tmp = var[VX][IX(i,j,k)];
A_tmp = area_yz(para, var, i, j, k);
}
else if(j==0 || j==jmax+1) {
vel_tmp = var[VY][IX(i,j,k)];
A_tmp = area_zx(para, var, i, j, k);
}
else if(k==0 || k==kmax+1) {
vel_tmp = var[VZ][IX(i,j,k)];
A_tmp = area_xy(para, var, i, j, k);
}
/*-------------------------------------------------------------------------
| Set the thermal conditions data for Modelica.
| In FFD simulation, the BINDEX[3][it] indicates: 1->T, 0->Heat Flux.
| Those BINDEX[3][it] will be reset according to the Modelica data
| para->comsim->para->bouCon (1->Heat Flux, 2->T).
| Here is to give the Modelica the missing data (For instance, if Modelica
| send FFD Temperature, FFD should then send Modelica Heat Flux).
-------------------------------------------------------------------------*/
if(var[FLAGP][IX(i,j,k)]==SOLID) {
switch(BINDEX[3][it]) {
// FFD uses heat flux as BC to compute temperature
// Then send Modelica the tempearture
case 0:
para->bc->temHeaAve[bcid] += var[TEMP][IX(i,j,k)] * A_tmp
/ para->bc->AWall[i];
break;
// FFD uses temperature as BC to compute heat flux
// Then send Modelica the heat flux
case 1:
para->bc->temHeaAve[bcid] += var[QFLUX][IX(i,j,k)]*A_tmp;
//sprintf(msg, "Cell(%d,%d,%d):\tQFLUX=%f,\tA=%f", i,j,k,var[QFLUX][IX(i,j,k)], A_tmp);
//ffd_log(msg, FFD_NORMAL);
break;
default:
sprintf(msg, "average_bc_area(): Thermal boundary (%d)"
"for cell (%d,%d,%d) was not defined",
BINDEX[3][it], i, j, k);
ffd_log(msg, FFD_ERROR);
return 1;
}
}
else if(var[FLAGP][IX(i,j,k)]==INLET||var[FLAGP][IX(i,j,k)]==OUTLET) {
para->bc->TPortAve[bcid] += var[TEMP][IX(i,j,k)] * A_tmp * vel_tmp;
para->bc->velPortAve[bcid] += vel_tmp * A_tmp;
// To be implemented
/*
for(j=0; j<para->bc->nb_Xi; j++)
para->bc->XiPortAve[bcid][j] += xi[j][IX(i,j,k)] * A_tmp * vel_tmp;
for(j=0, j<para->bc->nb_C; j++)
para->bc->CPortAve[bcid][j] = c[j][IX(i,j,k)] * A_tmp * vel_tmp;
*/
}
} // End of for(it=0; it<para->geom->index; it++)
// for(i=0; i<para->bc->nb_wall; i++) {
// sprintf(msg, "%s: para->bc->temHeaAve = %f", para->bc->wallName[i], para->bc->temHeaAve[i]);
// ffd_log(msg, FFD_NORMAL);
// }
return 0;
} // End of surface_integrate()
///////////////////////////////////////////////////////////////////////////////
/// Set sensor data
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD data
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int set_sensor_data(PARA_DATA *para, REAL **var) {
int imax = para->geom->imax, jmax = para->geom->jmax,
kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL u = var[VX][IX(imax/2,jmax/2,kmax/2)],
v = var[VY][IX(imax/2,jmax/2,kmax/2)],
w = var[VZ][IX(imax/2,jmax/2,kmax/2)];
// Averaged room temperature
para->sens->senVal[0] = para->cosim->ffd->TRoo;
//Velocity at the center of the space
para->sens->senVal[1] = sqrt(u*u + v*v + w*w);
return 0;
} // End of set_sensor_data