-
Notifications
You must be signed in to change notification settings - Fork 0
/
iceStorageFunc.f90
1985 lines (1431 loc) · 80 KB
/
iceStorageFunc.f90
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
!> --------------------------------------------------------------------------
!> @file: iceStorageFunc.f90
!> @brief: functions to calculate the geometry of the storage tank
!> and immersed heat exchangers
!> @author SPF institut fur SolarTechnik
!> @author D.Carbonell,W.Lodge
!> @date 10/08/2012.
!> @todo change HX newton solver for a gauss seidel. I thinks in this case it may be faster. Check it
!> --------------------------------------------------------------------------
module iceStoreFunc
use physProp
use TrnsysFunctions
implicit none
!Specification. Write here global parameters that can be used inside the module
contains
! ===========================================================================
!>@brief: calculateGeo : calculate geometry of the storage tank and of
!>the immersed heat exchanger.
!>@param iceStore : ice Storage structure
!>@return iceStore with filled geometry
! ===========================================================================
subroutine calculateGeo(iceStore,immersedHx)
use spfGlobalConst, only: PI
use iceStoreConst
use iceStoreDef
use hxModule
implicit none
type(iceStoreStruct), intent(inout),target :: iceStore
type(hxStruct), intent(inout) :: immersedHx(nIHX)
integer :: nCv, nHx
double precision, pointer:: LTank, VTank, HTank, WTank, rhoWater, dy
double precision , pointer :: H(:), M(:), A(:), areaTop(:), areaBot(:)
double precision :: totalExternalArea,crossSection
character (len=maxMessageLength) :: MyMessage
!double precision :: minV,maxV
!>------------------------------
!> internal data
!>------------------------------
integer :: i,j,k,count,iHx
double precision :: diameter,aboveHx,belowHx
!>---------------------------------
!> access to struct ice Store data
!>---------------------------------
nCv = iceStore%nCv; nHx = iceStore%nHx
LTank => iceStore%LTank
VTank => iceStore%VTank; HTank => iceStore%HTank; WTank => iceStore%WTank
dy => iceStore%dy
rhoWater => iceStore%rhoWater
H => iceStore%H; M => iceStore%M; A => iceStore%A;
areaBot => iceStore%areaBot; areaTop => iceStore%areaTop
!>-------------------------------------------------------------
!>Compute geometryFields: height, mass, and surface areas
!>-------------------------------------------------------------
dy = HTank / nCv
iceStore%vcly(1) = 0
do i=2,nCv+1
!y(i) = HTank - ((nCv - i + 1) * dy)
iceStore%vcly(i) = iceStore%vcly(i-1)+dy
!print *,' y(',i,')= ',y(i),' dy ',dy
enddo
!as a consequence y(nCv+1) = HTank
do i=1,nCv
H(i) = iceStore%vcly(i+1) - iceStore%vcly(i)
iceStore%nody(i) = 0.5*(iceStore%vcly(i+1) + iceStore%vcly(i))
!Automatic (equal) size nodes [m]
!iceStore%Htank / iceStore%nCv
enddo
! iceStore%nCvInHxs = 0
do i=1,iceStore%nCv
! if the lower part of the CV is above ice heat exchanger
if(iceStore%vcly(i)>=iceStore%heightIceHx) then
iceStore%iceFrac(i) = iceStore%maxIceFrac
iceStore%isHxLayerRatio(i) = 0.0d0
! if the upper part of the CV is below the ice heat exchanger
else if(iceStore%vcly(i+1)<=iceStore%heightIceHx) then
iceStore%iceFrac(i) = iceStore%maxIceFracIceLayer
! iceStore%nCvInHxs = iceStore%nCvInHxs +1
iceStore%isHxLayerRatio(i) = 1.0d0
!iceStore%iceFrac(i) = iceStore%maxIceFrac
! the heat exchanger is inside the CV
else
!aboveHx = (iceStore%vcly(i+1) - iceStore%heightIceHx)/H(i)
!belowHx = (H(i)-aboveHx)/H(i)
aboveHx = (iceStore%vcly(i+1) - iceStore%heightIceHx)/H(i)
belowHx = (iceStore%heightIceHx- iceStore%vcly(i))/H(i)
iceStore%iceFrac(i) = iceStore%maxIceFrac*aboveHx + iceStore%maxIceFracIceLayer*belowHx
! iceStore%nCvInHxs = iceStore%nCvInHxs + belowHx !is not an integer then !!!
iceStore%isHxLayerRatio(i) = belowHx
endif
enddo
if(iceStore%tankGeometry==0) then
LTank = VTank / (HTank * WTank)
iceStore%iceThickMax = LTank/2.d0 ! because we use 2 times the area DC ERROR Jan 2015
totalExternalArea = 2.0d0*WTank*HTank + 2.0d0*LTank*HTank + 2.0d0*LTank*WTank
!> Area exchange with the surrounding for heat losses/gains calculation
!DO FOR ONLY 1CV !!!!
iceStore%areaTopSurface = WTank * LTank
iceStore%areaBotSurface = WTank * LTank
do i = 1,nCv
A(i)=((2 * WTank) + (2 * LTank)) * H(i)
! Area between store layers = cross sectional area
areaBot(i) = WTank*LTank
areaTop(i) = WTank*LTank
iceStore%vTankCv(i) = WTank * LTank * H(i)
enddo
areaBot(1) = 0.d0
if(nCv>1) then
areaTop(nCv) = 0.d0
endif
!so now A(1) and A(nCV) do not account for top and bottom losses
!A(nCv) = A(nCv) + WTank * LTank
!A(1) = A(1) + WTank * LTank
elseif(iceStore%tankGeometry==1) then
! HTank is the height and V the volume
diameter = sqrt(4*VTank/(pi*HTank))
crossSection = pi*diameter**2/4.0d0
!VTank = crossSection*HTank
iceStore%iceThickMax = diameter
totalExternalArea = 2*crossSection + pi*diameter*HTank
if(nCv==1) then
areaBot(1) = 0.0 ! we need this for oneDimensionalCalculation areaRef.
areaTop(1) = crossSection
iceStore%vTankCv(1) = VTank
A(1) =pi*diameter*HTank + 2.0*crossSection
else
do i=1,nCv
A(i) = pi*diameter*dy
areaBot(i) = crossSection
areaTop(i) = crossSection
iceStore%vTankCv(i) = VTank/nCv
enddo
areaBot(1) = 0.d0
areaTop(nCv) = 0.d0
iceStore%areaTopSurface=crossSection
iceStore%areaBotSurface=crossSection
! so now A(1) and A(nCV) do not account for top and bottom losses
! A(1) = A(1) + crossSection ! bottom surface exchange with ambient
! A(nCv) = A(nCv) + crossSection ! top surface exchange with ambient
endif
else
write(MyMessage,'("calculateGe TANK GEOMETRY NOT IMPLEMENTED")') iceStore%tankGeometry
call Messages(-1,Trim(MyMessage),'FATAL', iceStore%iUnit,iceStore%iType)
endif
if(iceStore%verboseLevel>=1) then
if(abs(sum(A(1:nCv))+iceStore%areaTopSurface+iceStore%areaBotSurface-totalExternalArea)>1e-10) then
write(MyMessage,'("calculateGeo ERROR IN EXTERNAL AREAS sumA=",f" ExtArea=",f)') (sum(A(1:nCv))+iceStore%areaTopSurface+iceStore%areaBotSurface,totalExternalArea)
call Messages(-1,Trim(MyMessage),'NOTICE', iceStore%iUnit,iceStore%iType)
!write(myScreenUnit,*) ' ERROR IN EXTERNAL AREAS sumA=',sum(A(1:nCv))+iceStore%areaTopSurface+iceStore%areaBotSurface,' extArea=',totalExternalArea
!call FoundBadParameter(1,'Fatal','ERROR IN EXTERNAL AREAS sumA=',sum(A(1:nCv))+iceStore%areaTopSurface+iceStore%areaBotSurface,' extArea=',totalExternalArea)
endif
endif
!<--------------Calculation of UA--------------------------
do i=1,nCv
iceStore%UALoss(i) = iceStore%ULoss(i)*A(i)
if(i==1) then
iceStore%UALoss(i) = iceStore%UALoss(i)+iceStore%ULossBot*iceStore%areaBotSurface
else if(i==nCv) then
iceStore%UALoss(i) = iceStore%UALoss(i)+iceStore%ULossTop*iceStore%areaTopSurface
endif
enddo
iceStore%volumeTop = VTank *(1d0-iceStore%heightIceHx/HTank) ! the volume on top of the heat exchangers
iceStore%volumeBot = VTank - iceStore%volumeTop
! calculate the vol of water for each Cv
call calculateMassOfWater(iceStore,immersedHx)
iceStore%VTankEff = sum(iceStore%volWater(1:nCv))
end subroutine calculateGeo
subroutine initializeMassOfIce(iceStore,immersedHx)
use spfGlobalConst, only: PI
use iceStoreConst
use iceStoreDef
use hxModule
implicit none
type(iceStoreStruct), intent(inout),target :: iceStore
type(hxStruct), intent(inout) :: immersedHx(nIHX)
integer :: iHx,k,j,cvWithHx
double precision :: maxIceCv,iceMassHxCv,massLeft,sumFactorsAllHx, iceAdded,&
iceLeft,r,usedIcePerCv,rTotalShare
double precision, allocatable :: factorCv(:)
integer :: error,nCvWithHx,nHxInsideCv,hxUsed
! Distribute the inital kg of ice in the layers
if(iceStore%iceFloatingIni>0.0) then
!only floating ice.
if(iceStore%meltCrit<1) then
iceStore%iceFloating = iceStore%iceFloatingIni
iceStore%iceFloatingOld = iceStore%iceFloating
call reDistributeIce(iceStore)
else
! ice on hx.
!count = 0
!do iHx=1,iceStore%nHx
! if(immersedHx(iHx)%numberOfCv>0) then
! count = count+1
! endif
!enddo
allocate(factorCv(iceStore%nCv),stat=error)
iceLeft = iceStore%iceFloatingIni
nCvWithHx = 0
!Loop for all hx and Cv to see how many Cv have a hx in
do j=1,iceStore%nCv
cvWithHx=0
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%numberOfCv>0) then
do k=1,immersedHx(iHx)%numberOfCv
if(immersedHx(iHx)%factorHxToTank(k,j)>0.) then
cvWithHx=1 !we could break the loop here
endif
end do
end if
end do
nCvWithHx=nCvWithHx+cvWithHx
end do
usedIcePerCv = iceLeft/nCvWithHx
do j=1,iceStore%nCv
maxIceCv = iceStore%rhoWater*iceStore%maxIceFrac*(iceStore%VTank/iceStore%nCv-iceStore%vHxCv(j))
cvWithHx = 0
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%numberOfCv>0) then
do k=1,immersedHx(iHx)%numberOfCv
if(immersedHx(iHx)%factorHxToTank(k,j)>0.) then
cvWithHx=1 !we could break the loop here
endif
end do
end if
end do
if(cvWithHx) then
nHxInsideCv = 0
do iHx=1,iceStore%nHx
!hxUsed = 0
!if(immersedHx(iHx)%numberOfCv>0) then
! do k=1,immersedHx(iHx)%numberOfCv
! hxUsed=1
! enddo
!endif
!nHxInsideCv=nHxInsideCv+hxUsed
if(immersedHx(iHx)%numberOfCv>0) then
nHxInsideCv=nHxInsideCv+1
endif
enddo
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%numberOfCv>0) then
do k=1,immersedHx(iHx)%numberOfCv
!total share of 1 all Cv of Hx that hit the Cv of the storage
rTotalShare = sum(immersedHx(iHx)%factorHxToTank(1:immersedHx(iHx)%numberOfCv,j))
if(immersedHx(iHx)%factorHxToTank(k,j) > 0.) then
!r = immersedHx(iHx)%factorHxToTank(k,j)/nHxInsideCv
r = 1.0/nHxInsideCv
iceAdded = min(iceLeft,usedIcePerCv*r) ! I divide equally by hx in each Cv.
immersedHx(iHx)%iceMassCv(k) = immersedHx(iHx)%iceMassCv(k) + iceAdded
immersedHx(iHx)%volIceCv(k) = immersedHx(iHx)%iceMassCv(k)/iceStore%rhoIce/immersedHx(iHx)%nParallelHx
iceLeft = iceLeft - iceAdded
endif
enddo
endif
enddo
endif
enddo
! initialize thickness from mass in each Hx cv
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%numberOfCv>0) then
do k=1,immersedHx(iHx)%numberOfCv
if(immersedHx(iHx)%geometry==PLATE) then
immersedHx(iHx)%iceThickCv(k) = immersedHx(iHx)%iceMassCv(k)/(immersedHx(iHx)%dA*iceStore%rhoIce)
elseif(immersedHx(iHx)%geometry==COIL) then
immersedHx(iHx)%dOutIce(k) = sqrt(4.0*immersedHx(iHx)%volIceCv(k)/pi/immersedHx(iHx)%dL+immersedHx(iHx)%dOut**2)
endif
enddo
immersedHx(iHx)%iceMass = sum(immersedHx(iHx)%iceMassCv(1:immersedHx(iHx)%numberOfCv))
endif
enddo
massLeft = iceStore%iceFloatingIni - sum(immersedHx(1:iceStore%nHx)%iceMass)
if(massLeft>1e-4) then
write(iceStore%MyMessage,'("initializeMassOfIce. massLeft > 0 =")') massLeft
!This is dangerous. We should not have floating ice at all
!from top to bottom
do j=iceStore%nCv,1,-1
if(massLeft/(iceStore%rhoIce*iceStore%volWater(j))> iceStore%maxIceFrac) then
iceStore%iceFloatMass(j) = iceStore%maxIceFrac*iceStore%vTankMinusHxCv(j)*iceStore%rhoIce
massLeft = massLeft - iceStore%iceFloatMass(j)
else
iceStore%iceFloatMass(j) = massLeft
massLeft = 0.0d0
endif
enddo
else if (massLeft < -1e-6) then
write(iceStore%MyMessage,'("initializeMassOfIce. massLeft < 0 =")') massLeft
call Messages(-1,Trim(iceStore%MyMessage),'FATAL', iceStore%iUnit,iceStore%iType)
endif
call calculateQFromHxToStorage(iceStore,immersedHx)
deallocate(factorCv,stat=error)
endif
call calculateMassOfIce(iceStore)
call calculateMassOfWater(iceStore,immersedHx) !readjust
call checkStorageStatus(iceStore,immersedHx)
endif
end subroutine initializeMassOfIce
! ===========================================================================
!> @brief: Calculates the geomertry of the storage and immersed heat exchanger
!> @param iceStore : ice Storage structure
!> @param Tinit : initial temperature
!> @param nCv : number of tank CV
!> @return iceStore with initialized variables
! ===========================================================================
subroutine initialize(iceStore,Tinit)
use iceStoreConst
use iceStoreDef
implicit none
type(iceStoreStruct), intent(inout), target :: iceStore
double precision, intent(in) :: Tinit(:)
integer :: i, j, nCv
nCv = iceStore%nCv
! iceStore%nCvInHxs = 0.0d0
! Here the inputs still not readed, so we do not know nHx
!nCv = iceStore%nCv; nHx = iceStore%nHx
!if (simtime.lt.(time0+dt/2)) then DO THIS OUTSIDE THE FUNCTION
! Set initial conditions:
! iceStore%addNodalLossCoef = 0
iceStore%noticeFound = 0
iceStore%itIceStore = 1
iceStore%itStore = 1
iceStore%itTrnsys = 1
iceStore%itDtTrnsys = 1
do i=1,nCv
iceStore%T(i) = Tinit(i)
iceStore%Told(i) = Tinit(i)
if(Tinit(i)<0.0) then
write(iceStore%MyMessage,'("Initial water temperature below 0=",f" for Cv=",d)') Tinit(i),i
call Messages(-1,Trim(iceStore%MyMessage),'FATAL', iceStore%iUnit,iceStore%iType)
end if
enddo
iceStore%sumQIce = 0.0d0
iceStore%qFused = 0.0d0
iceStore%fusedIceFloatMass = 0.0d0
iceStore%imbalance = 0.0d0
iceStore%iceFloatingOld = 0.0d0
iceStore%iceTotalMass = 0.0d0
iceStore%iceTotalMassOld = 0.0d0
iceStore%iceFloating = 0.0d0
! iceStore%hxMode = 0
iceStore%tFreeze = 0.0d0
iceStore%tSubcool = 0.0d0
iceStore%tBoil = 100.0d0
iceStore%tStoreAv = 0.0d0
iceStore%dy = 0.0d0
iceStore%e0 = 0.0d0
iceStore%e1 = 0.0d0
iceStore%timeInHours = 0.0d0
iceStore%dtsec = 0.0d0
iceStore%tankGeometry = 0 ! 0 is a box, 1 a cilynder
! iceStore%arrangmentHx = 0
iceStore%checkParameters = 1
iceStore%iceIsReleased = 0
iceStore%tEnvTop = 0.0d0
iceStore%tEnvBot = 0.0d0
iceStore%UlossTop = 0.0d0
iceStore%UlossBot = 0.0d0
iceStore%areaTopSurface = 0.0d0
iceStore%areaBotSurface = 0.0d0
iceStore%storeFull = 0
iceStore%storeTotallyFull = 0
iceStore%iceThickMax = 0d0
iceStore%volumeTop = 0d0
iceStore%heightIceHx = 0d0
iceStore%maxIceFracIceLayer = 0.0d0
iceStore%errorStorageLoop = 0.0d0
iceStore%errorIceStorageLoop = 0.0d0
iceStore%RaMeltingFloat = 0.0d0
iceStore%NuMeltingFloat = 0.0d0
iceStore%deIceIsPossible = 1 !
iceStore%iceHx = 0.0
iceStore%xBetweenPipes = 0.0
iceStore%yBetweenPipes = 0.0
iceStore%iceBlockGrowingMode = 0
iceStore%meltCrit=100. ! leave it. Not used in this TYPE, but changing to 0 will affect.
iceStore%counterMassFlowZero=0
iceStore%counterMassFlowZeroOld=0
iceStore%iceTotalMassFromQLat=0.0
iceStore%iceTotalMassFromQLatOld=0.0
end subroutine initialize
subroutine useOldTimeStep(iceStore,immersedHx)
use iceStoreConst
use iceStoreDef
use hxModule
use hxFunc
implicit none
type(iceStoreStruct), intent(inout), target :: iceStore
type(hxStruct), intent(inout), target :: immersedHx(nIHX)
integer :: nCv, nHx, i, iHx !, iceModeIsActive, nCvHx, iceOnHxCv
nCv = iceStore%nCv; nHx = iceStore%nHx
iceStore%iceTotalMass = iceStore%iceTotalMassOld
iceStore%iceFloating = iceStore%iceFloatingOld
do i=1,nCv
iceStore%T(i) = iceStore%Told(i)
iceStore%iceFloatMass(i) = iceStore%iceFloatMassOld(i)
iceStore%volWater(i) = iceStore%volWaterOld(i)
iceStore%iceMassHxCv(i) = iceStore%iceMassHxCvOld(i)
enddo
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%isUsed) then
call resetHxToOldTimeStep(immersedHx(iHx))
endif
enddo
end subroutine useOldTimeStep
! ===========================================================================
!> @brief: called once at the final time step for each iteration
!> @param iceStore iceStoreStruct
!> @return iceStore with udated time step variables
! ===========================================================================
subroutine updateTimeStep(iceStore,immersedHx)
use iceStoreConst
use iceStoreDef
use hxModule
use hxFunc
implicit none
type(iceStoreStruct), intent(inout), target :: iceStore
type(hxStruct), intent(inout), target :: immersedHx(nIHX)
integer :: nCv, nHx, i, iHx,notpossible !, iceModeIsActive, nCvHx, iceOnHxCv
double precision :: dummy=0.0
nCv = iceStore%nCv; nHx = iceStore%nHx
iceStore%iceTotalMassOld = iceStore%iceTotalMass
iceStore%iceFloatingOld = iceStore%iceFloating
do i=1,nCv
iceStore%Told(i) = iceStore%T(i)
iceStore%iceFloatMassOld(i) = iceStore%iceFloatMass(i)
iceStore%volWaterOld(i) = iceStore%volWater(i)
iceStore%iceMassHxCvOld(i) =iceStore%iceMassHxCv(i)
enddo
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%isUsed) then
immersedHx(iHx)%iceThickMeltOld = immersedHx(iHx)%iceThickMelt
immersedHx(iHx)%iceThickOld = immersedHx(iHx)%iceThick
immersedHx(iHx)%iceThickInOld = immersedHx(iHx)%iceThickIn
immersedHx(iHx)%iceMassOld = immersedHx(iHx)%iceMass
do i=1,immersedHx(iHx)%numberOfCv
immersedHx(iHx)%tIce0(i) = immersedHx(iHx)%tIce(i)
immersedHx(iHx)%t0(i) = 0.5*(immersedHx(iHx)%t(i)+immersedHx(iHx)%t(i+1)) ! T is at the cv faces, while t0 is at the center
immersedHx(iHx)%volIceCvOld(i) = immersedHx(iHx)%volIceCv(i)
immersedHx(iHx)%iceMassCvOld(i) = immersedHx(iHx)%iceMassCv(i)
immersedHx(iHx)%tWall0(i) = immersedHx(iHx)%tWall(i)
immersedHx(iHx)%tStore0(i) = immersedHx(iHx)%tStore(i)
if(immersedHx(iHx)%geometry==PLATE) then
!if(immersedHx(iHx)%iceThickCv(i)<=1e-8 .and. immersedHx(iHx)%iceThickInCv(i)>1e-8) then
! notpossible=1
!endif
!RESET STAFF IS MOVED WHEN THE TIME STEP HAS CONVERGED !!
!If inner ice layer has reacehd the melted one, then we reset them
if(immersedHx(iHx)%resetIceThickInCv(i) == 1) immersedHx(iHx)%iceThickInCv(i) = 0.0d0 !reset because of melting
if(immersedHx(iHx)%resetIceThickInAndMeltCv(i) == 1) then !reset becasue IceThicCvIn gets in contact with melted ice.
!if(immersedHx(iHx)%iceThickInCv(i)>=immersedHx(iHx)%iceThickMeltCv(i)) then
!immersedHx(iHx)%iceThickCv(i) = immersedHx(iHx)%iceThickCv(i) + immersedHx(iHx)%iceThickInCv(i) !DC-21
immersedHx(iHx)%iceThickInCv(i) = 0.0
immersedHx(iHx)%iceThickMeltCv(i) = 0.0
endif
immersedHx(iHx)%iceThickCvOld(i) = immersedHx(iHx)%iceThickCv(i)
immersedHx(iHx)%iceThickInCvOld(i) = immersedHx(iHx)%iceThickInCv(i)
immersedHx(iHx)%iceThickMeltCvOld(i) = immersedHx(iHx)%iceThickMeltCv(i)
!if(immersedHx(iHx)%iceThickMeltCv(i)>0 .and. immersedHx(iHx)%iceThickCv(i)==0) immersedHx(iHx)%iceThickMeltCv(i)=0
elseif(immersedHx(iHx)%geometry==COIL) then
immersedHx(iHx)%dEqCvOld(i) = immersedHx(iHx)%dEqCv(i)
!This is true when we melt and the DinIce>=, then we add dIn to dOut and
!we have to reset dIn to zero here.
if(immersedHx(iHx)%resetDInIceToZero(i) == 1) then
immersedHx(iHx)%dInIce(i) = immersedHx(iHx)%dOut
endif
!CHECK THIS !!!!!!!!!!!!!!!!!
if(abs(immersedHx(iHx)%dInIce(i)-immersedHx(iHx)%dOutMeltIce(i))<1e-15 .and. immersedHx(iHx)%dOutIce(i)>(immersedHx(iHx)%dOut+1e-10)) then
immersedHx(iHx)%dInIce(i) = immersedHx(iHx)%dOut
immersedHx(iHx)%dOutMeltIce(i) = immersedHx(iHx)%dOut
endif
!if(immersedHx(iHx)%myCase(i)==11 .or. immersedHx(iHx)%myCase(i)==13) then !dInIce was limited to dOutMeltIce, we move from case 1 to status 4
! immersedHx(iHx)%dInIce(i) = immersedHx(iHx)%dOut
! immersedHx(iHx)%dOutMeltIce(i) = immersedHx(iHx)%dOut
!endif
!If melted ice layer has reached the outer one, then we reset them
if(abs(immersedHx(iHx)%dOutIce(i)-immersedHx(iHx)%dOutMeltIce(i))<1e-15 .and. immersedHx(iHx)%dOutIce(i)>(immersedHx(iHx)%dOut+1e-10)) then
immersedHx(iHx)%dOutIce(i) = immersedHx(iHx)%dInIce(i) ! Melt reaches dOut and we set Dout to Din if exist
immersedHx(iHx)%dOutMeltIce(i) = immersedHx(iHx)%dOut
immersedHx(iHx)%dInIce(i) = immersedHx(iHx)%dOut
immersedHx(iHx)%myCase(i) = immersedHx(iHx)%myCase(i)+100
endif
!If inner ice layer has reached the melted one, then we reset them
immersedHx(iHx)%dOutIceOld(i) = immersedHx(iHx)%dOutIce(i)
immersedHx(iHx)%dInIceOld(i) = immersedHx(iHx)%dInIce(i)
immersedHx(iHx)%dOutMeltIceOld(i) = immersedHx(iHx)%dOutMeltIce(i)
!
if(immersedHx(iHx)%dInIce(i)>immersedHx(iHx)%dOutIce(i)) then
iceStore%dummy=1
endif
if(immersedHx(iHx)%dOutMeltIce(i)>immersedHx(iHx)%dOutIce(i)) then
iceStore%dummy=1
endif
if(immersedHx(iHx)%dInIce(i)>immersedHx(iHx)%dOutMeltIce(i)) then
iceStore%dummy=1
endif
endif
! call checkHeatExchangerMass(iceStore,immersedHx(iHx),dummy,i,200)
immersedHx(iHx)%myCaseOld(i) = immersedHx(iHx)%myCase(i)
enddo
endif
end do
if(iceStore%counterMassFlowZero>=100) then
iceStore%counterMassFlowZeroOld = 0
else
iceStore%counterMassFlowZeroOld = iceStore%counterMassFlowZero
endif
iceStore%iceTotalMassFromQLatOld = iceStore%iceTotalMassFromQLat
if(iceStore%sumQHx>0. .and. iceStore%iceFloating>iceStore%iceFloatingOld .and. iceStore%iceIsReleased ==0) then
write(iceStore%MyMessage,'("Heating Store and floating ice growing !!!")')
call Messages(-1,Trim(iceStore%MyMessage),'NOTICE', iceStore%iUnit,iceStore%iType)
endif
end subroutine updateTimeStep
! ===========================================================================
!> @brief mix : Mix the storage tank CV when the density gradient is negative
! ------------------------------------------------------------------
!> @param n : number of CV
!> @param M : mass flow vector [kg/s]
!> @param T : temperature vector [K]
! ------------------------------------------------------------------
!> @return: T : Mixed T storage tank CV temperatures [K]
! ===========================================================================
subroutine mixUp(iceStore,ii)
use iceStoreDef
integer :: nCv,i,j,iteMax
integer, intent(in) :: ii
double precision,pointer :: T(:),M(:)
type(iceStoreStruct), intent(inout), target :: iceStore
double precision :: ts,xs
T => iceStore%T
M => iceStore%M
nCv = iceStore%nCv
j=ii
ts=T(j)*M(j)
xs=M(j)
do while((ts/xs)>T(j+1))
ts=ts+T(j+1)*M(j+1)
xs=M(j+1)+xs
if (j==(nCv-1)) then
j=j+1
goto 22
endif
j = j+1
enddo
22 do i=ii,j
T(i)=ts/xs
enddo
end subroutine mixUp
subroutine mixDown(iceStore,ii)
use iceStoreDef
integer :: nCv,i,j
integer, intent(in) :: ii
double precision,pointer :: T(:),M(:)
type(iceStoreStruct), intent(inout), target :: iceStore
double precision :: ts,xs,tsOverXs
T => iceStore%T
M => iceStore%M
nCv = iceStore%nCv
j = ii
ts=T(j)*M(j)
xs=M(j)
tsOverXs = ts/xs
do while(tsOverXs>T(j-1))
ts=ts+T(j-1)*M(j-1)
xs=M(j-1)+xs
if (j==2) then
j=j-1
goto 11
endif
j = j-1
enddo
11 do i=ii,j,-1
T(i)=ts/xs
enddo
end subroutine mixDown
subroutine reversionEliminationAlgorithm(iceStore)
use iceStoreDef
use physProp
use Trnsysfunctions
type(iceStoreStruct), intent(inout), target :: iceStore
integer :: nCv
double precision,pointer :: T(:)
double precision :: tFlip,precision
integer :: i,iteMax,ite
logical isTinverted
T => iceStore%T
!M => iceStore%M
nCv = iceStore%nCv
isTinverted=.true.
iteMax = 100
ite = 0
tFlip = iceStore%tFlip
precision = 1e-4
!if(iceStore%verboseLevel==4) write (myScreenUnit,*) 'DEGUB TYPE 860 ICE STORAGE : MIXING TIME ',iceStore%timeInSeconds/3600.
do while (isTinverted == .true. .and. ite<=iteMax)
isTinverted=.false.
do i=1,nCv-1
if((T(i)-precision)>T(i+1) .and. T(i)>tFlip) then
isTinverted= .true.
call mixUp(iceStore,i)
exit
else if ((T(i)+precision)<T(i+1) .and. T(i)<tFlip) then
isTinverted= .true.
call mixDown(iceStore,i+1)
exit
endif
enddo
ite = ite+1
enddo
!check
do i=1,nCv-1
if((T(i)-precision)>T(i+1) .and. T(i)>tFlip) then
!write(myScreenUnit,*) ' Mixing algorithm is not working in upstream mode'
write(iceStore%MyMessage,'("Mixing algorithm is not working in upstream mode")')
call Messages(-1,Trim(iceStore%MyMessage),'NOTICE', iceStore%iUnit,iceStore%iType)
else if ((T(i)+precision)<T(i+1) .and. T(i)<tFlip) then
!write(myScreenUnit,*) ' Mixing algorithm is not working in downstream mode'
write(iceStore%MyMessage,'("Mixing algorithm is not working in downstream mode")')
call Messages(-1,Trim(iceStore%MyMessage),'NOTICE', iceStore%iUnit,iceStore%iType)
endif
enddo
end subroutine reversionEliminationAlgorithm
subroutine calculateQFromHxToStorage(iceStore,immersedHx)
use hxModule
use iceStoreDef
implicit none
type(hxStruct), intent(inout) :: immersedHx(nIHX)
type(iceStoreStruct), intent(inout) :: iceStore
double precision :: sumQsen,qSumSenHx,sumQlat,qSumLatHx,& ! to check heat flows
qSen,qLat,iceMass,sumIceMassHx,iceMassHx,sumQIceAcum,qIceAcum,qIceAcumHx
integer :: iHx,n, j
!< Sensible heat
!call calculateQvFromHX(immersedHx,iceStore) ! qv from all Hx. Calculated iceStore%qhx
sumQsen = 0.0
sumQLat = 0.0
sumIceMassHx = 0.0
sumQIceAcum = 0.0
do iHx=1,iceStore%nHx
if(immersedHx(iHx)%isUsed) then
do n=1,iceStore%nCv
qSen = 0.0
qLat = 0.0
iceMass = 0.0
qIceAcum = 0.0
do j=1,immersedHx(iHx)%numberOfCv
qSen = qSen - (immersedHx(iHx)%qHxCv(j)+immersedHx(iHx)%qAcumIceCv(j))*immersedHx(iHx)%factorHxToTank(j,n) !including all parallel hx
qLat = qLat + immersedHx(iHx)%qIceCv(j)*immersedHx(iHx)%factorHxToTank(j,n)
iceMass = iceMass + immersedHx(iHx)%iceMassCv(j)*immersedHx(iHx)%factorHxToTank(j,n)
!We should check here is the Cv of water is already full of ice. In principle this should be considered by blocking the Cv of the hx, but
!it could be that part of a hx is inside this Cv of water and it is not blocked becasue it is only a part of the whole hx cv
qIceAcum = qIceAcum + immersedHx(iHx)%qAcumIceCv(j)
end do
!This includes all parallel Hx's
iceStore%qhx(iHx,n) = qSen
iceStore%qIceCv(iHx,n) = qLat
iceStore%iceMassHx(iHx,n) = iceMass
iceStore%qIceAcumCv(iHx,n) = qIceAcum
sumQsen = sumQsen + iceStore%qhx(iHx,n)
sumQlat = sumQlat + iceStore%qIceCv(iHx,n)
sumIceMassHx = sumIceMassHx + iceStore%iceMassHx(iHx,n)
sumQIceAcum = iceStore%qIceAcumCv(iHx,n)
enddo
endif
end do
if(1) then
qSumSenHx = 0.0
qSumLatHx = 0.0
qIceAcumHX = 0.0
iceMassHX = 0.0
do iHx=1,iceStore%nHx
qSumSenHx = qSumSenHx + immersedHx(iHx)%qHxToTnk - immersedHx(iHx)%qAcumIce
qSumLatHx = qSumLatHx + immersedHx(iHx)%qIce
iceMassHx = iceMassHx + immersedHx(iHx)%iceMass
qIceAcumHX = qIceAcumHX + immersedHx(iHx)%qAcumIce
enddo
if(abs(qIceAcumHx-sumQIceAcum)>1e-8) then
write(iceStore%myMessage,*) 'ERROR in passing sensible Qice Diff=',abs(qIceAcumHx-sumQIceAcum),' qIceAcumHx=',qIceAcumHx,' sumQIceAcum=',sumQIceAcum
call messages(-1,trim(iceStore%myMessage),'warning',iceStore%iUnit,iceStore%iType)
endif
if(abs(iceMassHx-sumIceMassHx)>1e-8) then
write(iceStore%myMessage,*) 'ERROR in passing ice mass Diff=',abs(iceMassHx-sumIceMassHx),' iceMassHx=',iceMassHx,' sumIceMassHx=',sumIceMassHx
call messages(-1,trim(iceStore%myMessage),'warning',iceStore%iUnit,iceStore%iType)
endif
! In theory this should be exactly zero. We should double check why is low but not 0
if(abs(qSumSenHx-sumQsen)>1e-8) then
write(iceStore%myMessage,*) 'ERROR in passing sensible heat Diff=',abs(qSumSenHx-sumQsen),' QsenTank=',sumQsen,' sumQHxSen=',qSumSenHx
call messages(-1,trim(iceStore%myMessage),'warning',iceStore%iUnit,iceStore%iType)
endif
if(abs(qSumLatHx-sumQlat)>1e-8) then
write(iceStore%myMessage,*) 'ERROR in passing latent heat Diff=',abs(qSumLatHx-sumQlat),' QlatTank=',sumQlat,' sumQHxLat=',qSumLatHx
call messages(-1,trim(iceStore%myMessage),'warning',iceStore%iUnit,iceStore%iType)
endif
endif
end subroutine calculateQFromHxToStorage
subroutine reCalculateMIceFromOutsideMelting(iceStore,immersedHx)
use spfGlobalConst, only: PI
use hxModule
use iceStoreDef
use TrnsysConstants
implicit none
type(hxStruct), intent(inout) :: immersedHx(nIHX)
type(iceStoreStruct), intent(inout) :: iceStore
double precision :: iceMassHxToCvStore,qFusedHxCv, volInner, volOuter, iceMassMelted, iceMassInner,iceMassOuter,AhxCv
integer :: j,k,i,correct
correct = 0
do j=1,iceStore%nHx
immersedhx(j)%iceMassMeltedOutside = 0.0
iceMassHxToCvStore = 0.0
if(immersedHx(j)%isUsed) then
do k=1,immersedHx(j)%numberOfCv
iceMassMelted = sum(immersedhx(j)%iceMassMeltedOutsideCv(k,1:iceStore%nCv))
immersedhx(j)%iceMassMeltedOutside = immersedhx(j)%iceMassMeltedOutside + iceMassMelted
if(iceMassMelted>0.) then
correct = 1
immersedhx(j)%iceMassCv(k) = max(immersedhx(j)%iceMassCv(k)-iceMassMelted,0.0)
immersedHx(j)%volIceCv(k) = immersedHx(j)%iceMassCv(k)/(immersedHx(j)%nParallelHx*iceStore%rhoIce)
if(immersedHx(j)%geometry==PLATE) then
! I use iceOld because if I use ice it is changed for each iteration in one time step
! and iceOld = ice in this case because no ice formed or melted is on.
AhxCv = immersedHx(j)%area/immersedHx(j)%numberOfCv
volInner = immersedHx(j)%iceThickInCv(k)*AhxCv
volOuter = immersedHx(j)%iceThickCv(k)*AhxCv