Index: palm/trunk/SOURCE/indoor_model_mod.f90
===================================================================
--- palm/trunk/SOURCE/indoor_model_mod.f90	(revision 3757)
+++ palm/trunk/SOURCE/indoor_model_mod.f90	(revision 3759)
@@ -26,4 +26,9 @@
 ! -----------------
 ! $Id$
+! - Calculation of total building volume
+! - Several bugfixes
+! - Calculation of building height revised
+! 
+! 3745 2019-02-15 18:57:56Z suehring
 ! - remove building_type from module
 ! - initialize parameters for each building individually instead of a bulk 
@@ -98,11 +103,11 @@
     TYPE build
 
-       INTEGER(iwp) ::  id                             !< building ID
-       INTEGER(iwp) ::  kb_min                         !< lowest vertical index of a building
-       INTEGER(iwp) ::  kb_max                         !< highest vertical index of a building
-       INTEGER(iwp) ::  num_facades_per_building_h     !< total number of horizontal facades elements
-       INTEGER(iwp) ::  num_facades_per_building_h_l   !< number of horizontal facade elements on local subdomain
-       INTEGER(iwp) ::  num_facades_per_building_v     !< total number of vertical facades elements
-       INTEGER(iwp) ::  num_facades_per_building_v_l   !< number of vertical facade elements on local subdomain
+       INTEGER(iwp) ::  id                                !< building ID
+       INTEGER(iwp) ::  kb_min                            !< lowest vertical index of a building
+       INTEGER(iwp) ::  kb_max                            !< highest vertical index of a building
+       INTEGER(iwp) ::  num_facades_per_building_h = 0    !< total number of horizontal facades elements
+       INTEGER(iwp) ::  num_facades_per_building_h_l = 0  !< number of horizontal facade elements on local subdomain
+       INTEGER(iwp) ::  num_facades_per_building_v = 0    !< total number of vertical facades elements
+       INTEGER(iwp) ::  num_facades_per_building_v_l = 0  !< number of vertical facade elements on local subdomain
 
        INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  l_v            !< index array linking surface-element orientation index
@@ -121,5 +126,5 @@
        LOGICAL ::  on_pe = .FALSE.   !< flag indicating whether a building with certain ID is on local subdomain
        
-       
+       REAL(wp) ::  building_height   !< building height
        REAL(wp) ::  lambda_layer3     !< [W/(m*K)] Thermal conductivity of the inner layer  
        REAL(wp) ::  s_layer3          !< [m] half thickness of the inner layer (layer_3)
@@ -143,4 +148,5 @@
        REAL(wp) ::  height_storey     !< [m] storey heigth
        REAL(wp) ::  height_cei_con    !< [m] ceiling construction heigth
+       REAL(wp) ::  vol_tot           !< total building volume
 
        REAL(wp), DIMENSION(:), ALLOCATABLE ::  t_in       !< mean building indoor temperature, height dependent
@@ -165,9 +171,4 @@
 !
 !-- Declare all global variables within the module
-
-!     INTEGER(iwp) ::  building_type = 1       !< namelist parameter with 
-                                             !< X1=construction year (cy) 1950, X2=cy 2000, X3=cy 2050
-                                             !< R=Residental building, O=Office, RW=Enlarged Windows, P=Panel type (Plattenbau) WBS 70, H=Hospital (in progress), I=Industrial halls (in progress), S=Special Building (in progress)
-                                             !< (0=R1, 1=R2, 2=R3, 3=O1, 4=O2, 5=O3,...)
     INTEGER(iwp) ::  cooling_on              !< Indoor cooling flag (0=off, 1=on)
     INTEGER(iwp) ::  heating_on              !< Indoor heating flag (0=off, 1=on)
@@ -227,5 +228,4 @@
     REAL(wp) ::  f_cei                       !< [-] ceiling reduction factor
     REAL(wp) ::  ngs                         !< [m2] netto ground surface
-    REAL(wp) ::  building_height
     
     REAL(wp), PARAMETER ::  params_f_f               = 0.3_wp      !< [-] frame ratio chap. 8.3.2.1.1 for buildings with mostly cooling 2.0_wp
@@ -283,9 +283,4 @@
 !        MODULE PROCEDURE im_header
 !     END INTERFACE im_header
-
-!-- Data Output
-!    INTERFACE im_data_output
-!       MODULE PROCEDURE im_data_output
-!    END INTERFACE im_data_output
 !
 !-- Calculations for indoor temperatures  
@@ -564,5 +559,5 @@
        buildings(nb)%id = build_ids_final(nb)
 
-       IF ( ANY( building_id_f%var == buildings(nb)%id ) )                      &
+       IF ( ANY( building_id_f%var(nys:nyn,nxl:nxr) == buildings(nb)%id ) )    &
           buildings(nb)%on_pe = .TRUE.
     ENDDO 
@@ -577,5 +572,5 @@
        DO  j = nys, nyn
           IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
-             nb = MINLOC( ABS( buildings(:)%id - building_id_f%var(j,i) ),   &
+             nb = MINLOC( ABS( buildings(:)%id - building_id_f%var(j,i) ),    &
                          DIM = 1 )
              DO  k = nzb+1, nzt+1
@@ -608,4 +603,13 @@
     DEALLOCATE( k_max_l )
 !
+!-- Calculate building height.
+    DO  nb = 1, num_build
+       buildings(nb)%building_height = 0.0_wp
+       DO  k = buildings(nb)%kb_min, buildings(nb)%kb_max
+          buildings(nb)%building_height = buildings(nb)%building_height        &
+                                        + dzw(k)
+       ENDDO
+    ENDDO
+!
 !-- Calculate building volume
     DO  nb = 1, num_build
@@ -625,10 +629,11 @@
           buildings(nb)%vol_frac = 0.0_wp
           
-          IF ( ANY( building_id_f%var == buildings(nb)%id ) )  THEN
+          IF ( ANY( building_id_f%var(nys:nyn,nxl:nxr) == buildings(nb)%id ) ) &
+          THEN
              DO  i = nxl, nxr
                 DO  j = nys, nyn
                    DO  k = buildings(nb)%kb_min, buildings(nb)%kb_max
                       IF ( building_id_f%var(j,i) /= building_id_f%fill )      &
-                         volume_l(k) = dx * dy * dzw(k)
+                         volume_l(k) = volume_l(k) + dx * dy * dzw(k)
                    ENDDO
                 ENDDO
@@ -651,5 +656,9 @@
 !--    Determine fraction of local on total building volume
        IF ( buildings(nb)%on_pe )  buildings(nb)%vol_frac = volume_l / volume
-
+!
+!--    Calculate total building volume
+       IF ( ALLOCATED( buildings(nb)%volume ) )                                &
+          buildings(nb)%vol_tot = SUM( buildings(nb)%volume )
+       
        DEALLOCATE( volume   )
        DEALLOCATE( volume_l )
@@ -827,6 +836,8 @@
        nb = MINLOC( ABS( buildings(:)%id - building_id_f%var(j,i) ), DIM = 1 )
 
-       buildings(nb)%m_h(n_fa(nb)) = m
-       n_fa(nb) = n_fa(nb) + 1      
+       IF ( buildings(nb)%on_pe )  THEN
+          buildings(nb)%m_h(n_fa(nb)) = m
+          n_fa(nb) = n_fa(nb) + 1  
+       ENDIF
     ENDDO
 
@@ -839,7 +850,9 @@
           nb = MINLOC( ABS( buildings(:)%id - building_id_f%var(j,i) ), DIM = 1 )
 
-          buildings(nb)%l_v(n_fa(nb)) = l
-          buildings(nb)%m_v(n_fa(nb)) = m
-          n_fa(nb) = n_fa(nb) + 1      
+          IF ( buildings(nb)%on_pe )  THEN
+             buildings(nb)%l_v(n_fa(nb)) = l
+             buildings(nb)%m_v(n_fa(nb)) = m
+             n_fa(nb) = n_fa(nb) + 1   
+          ENDIF
        ENDDO
     ENDDO
@@ -1070,7 +1083,4 @@
              window_area_per_facade   = surf_usm_h%frac(ind_wat_win,m)  * facade_element_area  !< [m2] window area per facade element
              
-!              building_height          = buildings(nb)%num_facades_per_building_v_l * 0.1 * dzw(kk)
-             building_height          = buildings(nb)%kb_max * dzw(kk)
-             
 ! print*, "building_height", building_height
 ! print*, "num_facades_v_l", buildings(nb)%num_facades_per_building_v_l
@@ -1079,5 +1089,6 @@
 ! print*, "dzw kk", dzw(kk), kk
 
-             f_cei                    = building_height/(buildings(nb)%height_storey-buildings(nb)%height_cei_con) !< [-] factor for ceiling redcution
+             f_cei                    = buildings(nb)%building_height /        &
+                                       (buildings(nb)%height_storey-buildings(nb)%height_cei_con) !< [-] factor for ceiling redcution
              ngs                      = buildings(nb)%vpf(kk)/f_cei                    !< [m2] calculation of netto ground surface
              f_sr                     = ngs/floor_area_per_facade                      !< [-] factor for surface reduction
@@ -1274,7 +1285,7 @@
              indoor_volume_per_facade = buildings(nb)%vpf(kk)               !< [m3] indoor air volume per facade element           
              window_area_per_facade   = surf_usm_v(l)%frac(ind_wat_win,m)  * facade_element_area  !< [m2] window area per facade element
-             
-             building_height          = buildings(nb)%kb_max * dzw(kk)
-             f_cei                    = building_height/(buildings(nb)%height_storey-buildings(nb)%height_cei_con) !< [-] factor for ceiling redcution
+
+             f_cei                    = buildings(nb)%building_height /        &
+                                        (buildings(nb)%height_storey-buildings(nb)%height_cei_con) !< [-] factor for ceiling redcution
              ngs                      = buildings(nb)%vpf(kk)/f_cei                    !< [m2] calculation of netto ground surface
              f_sr                     = ngs/floor_area_per_facade                      !< [-] factor for surface reduction
@@ -1482,10 +1493,11 @@
            buildings(nb)%t_in = t_in_recv
 #else
-       buildings(nb)%t_in = buildings(nb)%t_in_l
+       IF ( ALLOCATED( buildings(nb)%t_in ) )                                  &
+          buildings(nb)%t_in = buildings(nb)%t_in_l
 #endif
-
-       buildings(nb)%t_in =   buildings(nb)%t_in /                             &
-                            ( buildings(nb)%num_facade_h +                     &
-                              buildings(nb)%num_facade_v )
+       IF ( ALLOCATED( buildings(nb)%t_in ) )                                  &
+          buildings(nb)%t_in =   buildings(nb)%t_in /                          &
+                               ( buildings(nb)%num_facade_h +                  &
+                                 buildings(nb)%num_facade_v )
 !
 !--    Deallocate dummy arrays
@@ -1647,12 +1659,14 @@
                        nb = MINLOC( ABS( buildings(:)%id - building_id_f%var(j,i) ),   &
                                     DIM = 1 )
-!
-!--                    Write mean building temperature onto output array. Please note,
-!--                    in contrast to many other loops in the output, the vertical 
-!--                    bounds are determined by the lowest and hightest vertical index
-!--                    occupied by the building. 
-                       DO  k = buildings(nb)%kb_min, buildings(nb)%kb_max
-                          local_pf(i,j,k) = buildings(nb)%t_in(k)
-                       ENDDO
+                       IF ( buildings(nb)%on_pe )  THEN
+!
+!--                       Write mean building temperature onto output array. Please note,
+!--                       in contrast to many other loops in the output, the vertical 
+!--                       bounds are determined by the lowest and hightest vertical index
+!--                       occupied by the building. 
+                          DO  k = buildings(nb)%kb_min, buildings(nb)%kb_max
+                             local_pf(i,j,k) = buildings(nb)%t_in(k)
+                          ENDDO
+                       ENDIF
                     ENDIF
                  ENDDO
