crm_module.F90.extrae 81.2 KB
Newer Older
Youngsung Kim's avatar
Youngsung Kim committed
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
!KGEN-generated Fortran source file 
  
!Generated at : 2020-05-29 19:47:47 
!KGEN version : 0.9.0 
  


module crm_module
    USE abcoefs_mod, ONLY: abcoefs 
    USE kurant_mod, ONLY: kurant 
    USE boundaries_mod, ONLY: boundaries 
    USE forcing_mod, ONLY: forcing 
    USE advect_mom_mod, ONLY: advect_mom 
    USE adams_mod, ONLY: adams 
    USE advect_all_scalars_mod, ONLY: advect_all_scalars 
    USE crm_surface_mod 
    USE zero_mod 
    USE buoyancy_mod 
    USE pressure_mod 
    USE uvw_mod 
    USE diagnose_mod 
    USE damping_mod 
    USE ice_fall_mod 
    USE coriolis_mod 
    USE crm_output_module 

!---------------------------------------------------------------
!  Super-parameterization's main driver
!  Marat Khairoutdinov, 2001-2009
!---------------------------------------------------------------
    USE kgen_utils_mod, ONLY: kgen_dp, kgen_array_sumcheck 
    USE tprof_mod, ONLY: tstart, tstop, tnull, tprnt 
    PUBLIC crm 
#ifdef _MPI 
    include "mpif.h" 
#endif 
      

contains

SUBROUTINE crm(kgen_unit, kgen_measure, kgen_isverified, kgen_filepath, ncrms, plev) 
    !-----------------------------------------------------------------------------------------------
    !-----------------------------------------------------------------------------------------------

    USE EXTRAE_MODULE

    USE shr_kind_mod, ONLY: r8 => shr_kind_r8 
    USE vars 
    USE microphysics 
    USE sgs 
    USE scalar_momentum_mod 
    USE crmdims, ONLY: crm_nx_rad, crm_ny_rad 
    USE accelerate_crm_mod, ONLY: use_crm_accel, accelerate_crm 
    USE kgen_utils_mod, ONLY: kgen_dp, kgen_array_sumcheck 
    USE kgen_utils_mod, ONLY: kgen_perturb_real 
    USE vars, ONLY: kr_externs_out_vars 
    USE grid, ONLY: kr_externs_out_grid 
    USE sgs, ONLY: kr_externs_out_sgs 
    USE params, ONLY: kr_externs_out_params 
    USE microphysics, ONLY: kr_externs_out_microphysics 
    USE micro_params, ONLY: kr_externs_out_micro_params 
    USE crm_output_module, ONLY: kr_externs_out_crm_output_module 
    USE accelerate_crm_mod, ONLY: kr_externs_out_accelerate_crm_mod 
    USE kgen_utils_mod, ONLY: check_t, kgen_init_check, kgen_init_verify, kgen_tolerance, kgen_minvalue, kgen_verboselevel, &
    &CHECK_IDENTICAL, CHECK_IN_TOL, CHECK_OUT_TOL 
    USE vars, ONLY: kv_externs_vars 
    USE grid, ONLY: kv_externs_grid 
    USE params, ONLY: kv_externs_params 
    USE microphysics, ONLY: kv_externs_microphysics 
    USE micro_params, ONLY: kv_externs_micro_params 
    USE crm_output_module, ONLY: kv_externs_crm_output_module 

    IMPLICIT NONE 
    !-----------------------------------------------------------------------------------------------
    ! Interface variable declarations
    !-----------------------------------------------------------------------------------------------


    INTEGER, INTENT(INOUT) :: ncrms 
    INTEGER, INTENT(INOUT) :: plev 
    !-----------------------------------------------------------------------------------------------
    ! Local variable declarations
    !-----------------------------------------------------------------------------------------------


    real(r8),       parameter :: wmin = 2.                      ! minimum up/downdraft velocity for stat
    real(crm_rknd), parameter :: cwp_threshold = 0.001          ! threshold for cloud condensate for shaded fraction calculation
    REAL(KIND=crm_rknd) :: tmp1, tmp 
    INTEGER :: i, j, k, l, nn, icyc, icrm 
    INTEGER :: kx 
    REAL(KIND=crm_rknd) :: qsat 
    REAL(KIND=crm_rknd), allocatable :: bflx(:) 
    ! These should all be inputs

    ! variables for radiation grouping method
    INTEGER :: i_rad 
    INTEGER :: j_rad 
    LOGICAL :: crm_accel_ceaseflag 
    ! Arrays

    REAL(KIND=crm_rknd), allocatable :: cwp         (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cwph       (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cwpm       (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cwpl       (:,:,:) 
    LOGICAL, allocatable :: flag_top(:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cltemp   (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cmtemp   (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: chtemp   (:,:,:) 
    REAL(KIND=crm_rknd), allocatable :: cttemp   (:,:,:) 

    REAL(KIND=r8), allocatable :: mui_crm(:,:) 
    REAL(KIND=r8), allocatable :: mdi_crm(:,:) 

    REAL(KIND=crm_rknd), pointer :: crm_rad_temperature   (:,:,:,:) 
    REAL(KIND=crm_rknd), pointer :: crm_rad_qv                     (:,:,:,:) 
    REAL(KIND=crm_rknd), pointer :: crm_rad_qc                     (:,:,:,:) 
    REAL(KIND=crm_rknd), pointer :: crm_rad_qi                     (:,:,:,:) 
    REAL(KIND=crm_rknd), pointer :: crm_rad_cld                   (:,:,:,:) 
    REAL(KIND=crm_rknd), pointer :: crm_rad_qrad                 (:,:,:,:) 
  !-----------------------------------------------------------------------------------------------
  !-----------------------------------------------------------------------------------------------
    INTEGER, INTENT(IN) :: kgen_unit 
    REAL(KIND=kgen_dp), INTENT(OUT) :: kgen_measure 
    LOGICAL, INTENT(OUT) :: kgen_isverified 
    CHARACTER(LEN=*), INTENT(IN) :: kgen_filepath 
    LOGICAL :: kgen_istrue 
    REAL(KIND=8) :: kgen_array_sum 
    INTEGER :: kgen_intvar, kgen_ierr 
    INTEGER :: kgen_mpirank, kgen_openmptid, kgen_kernelinvoke 
    LOGICAL :: kgen_evalstage, kgen_warmupstage, kgen_mainstage 
    COMMON / state / kgen_mpirank, kgen_openmptid, kgen_kernelinvoke, kgen_evalstage, kgen_warmupstage, kgen_mainstage 
    INTEGER, PARAMETER :: KGEN_MAXITER = 10
      
    TYPE(check_t) :: check_status 
    INTEGER*8 :: kgen_start_clock, kgen_stop_clock, kgen_rate_clock 
    REAL(KIND=kgen_dp) :: gkgen_measure 
    REAL(KIND=crm_rknd) :: kgenref_tmp1 
    REAL(KIND=crm_rknd) :: kgenref_tmp 
    INTEGER :: kgenref_icrm 
    INTEGER :: kgenref_icyc 
    INTEGER :: kgenref_k 
    INTEGER :: kgenref_j 
    INTEGER :: kgenref_i 
    INTEGER :: kgenref_nn 
    INTEGER :: kgenref_l 
    INTEGER :: kgenref_kx 
    REAL(KIND=crm_rknd) :: kgenref_qsat 
    INTEGER :: kgenref_i_rad 
    INTEGER :: kgenref_j_rad 
    LOGICAL :: kgenref_crm_accel_ceaseflag 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cwp 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cwph 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cwpm 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cwpl 
    LOGICAL, allocatable, dimension(:,:,:) :: kgenref_flag_top 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cltemp 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cmtemp 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_chtemp 
    REAL(KIND=crm_rknd), allocatable, dimension(:,:,:) :: kgenref_cttemp 
    REAL(KIND=r8), allocatable, dimension(:,:) :: kgenref_mui_crm 
    REAL(KIND=r8), allocatable, dimension(:,:) :: kgenref_mdi_crm 
    REAL(KIND=crm_rknd), pointer, dimension(:,:,:,:) :: kgenref_crm_rad_temperature 
    REAL(KIND=crm_rknd), pointer, dimension(:,:,:,:) :: kgenref_crm_rad_qv 
    REAL(KIND=crm_rknd), pointer, dimension(:,:,:,:) :: kgenref_crm_rad_qc 
    REAL(KIND=crm_rknd), pointer, dimension(:,:,:,:) :: kgenref_crm_rad_qi 
    REAL(KIND=crm_rknd), pointer, dimension(:,:,:,:) :: kgenref_crm_rad_cld 
      
    !parent block preprocessing 
      
#ifdef _MPI 
    call mpi_comm_rank(mpi_comm_world, kgen_mpirank, kgen_ierr) 
#else 
    kgen_mpirank = 0 
#endif 
      
      
    !local input variables 
    READ (UNIT = kgen_unit) tmp1 
    READ (UNIT = kgen_unit) tmp 
    READ (UNIT = kgen_unit) icrm 
    READ (UNIT = kgen_unit) icyc 
    READ (UNIT = kgen_unit) i 
    READ (UNIT = kgen_unit) j 
    READ (UNIT = kgen_unit) k 
    READ (UNIT = kgen_unit) nn 
    READ (UNIT = kgen_unit) l 
    READ (UNIT = kgen_unit) kx 
    READ (UNIT = kgen_unit) qsat 
    CALL kr_crm_real__crm_rknd_dim1(bflx, kgen_unit, "bflx", .FALSE.) 
    READ (UNIT = kgen_unit) i_rad 
    READ (UNIT = kgen_unit) j_rad 
    READ (UNIT = kgen_unit) crm_accel_ceaseflag 
    CALL kr_crm_real__crm_rknd_dim3(cwp, kgen_unit, "cwp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cwph, kgen_unit, "cwph", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cwpm, kgen_unit, "cwpm", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cwpl, kgen_unit, "cwpl", .FALSE.) 
    CALL kr_crm_logical___dim3(flag_top, kgen_unit, "flag_top", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cltemp, kgen_unit, "cltemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cmtemp, kgen_unit, "cmtemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(chtemp, kgen_unit, "chtemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(cttemp, kgen_unit, "cttemp", .FALSE.) 
    CALL kr_crm_real__r8_dim2(mui_crm, kgen_unit, "mui_crm", .FALSE.) 
    CALL kr_crm_real__r8_dim2(mdi_crm, kgen_unit, "mdi_crm", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_temperature, kgen_unit, "crm_rad_temperature", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_qv, kgen_unit, "crm_rad_qv", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_qc, kgen_unit, "crm_rad_qc", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_qi, kgen_unit, "crm_rad_qi", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_cld, kgen_unit, "crm_rad_cld", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(crm_rad_qrad, kgen_unit, "crm_rad_qrad", .FALSE.) 
      
    !extern output variables 
    CALL kr_externs_out_vars(kgen_unit) 
    CALL kr_externs_out_grid(kgen_unit) 
    CALL kr_externs_out_sgs(kgen_unit) 
    CALL kr_externs_out_params(kgen_unit) 
    CALL kr_externs_out_microphysics(kgen_unit) 
    CALL kr_externs_out_micro_params(kgen_unit) 
    CALL kr_externs_out_crm_output_module(kgen_unit) 
    CALL kr_externs_out_accelerate_crm_mod(kgen_unit) 
      
    !local output variables 
    READ (UNIT = kgen_unit) kgenref_tmp1 
    READ (UNIT = kgen_unit) kgenref_tmp 
    READ (UNIT = kgen_unit) kgenref_icrm 
    READ (UNIT = kgen_unit) kgenref_icyc 
    READ (UNIT = kgen_unit) kgenref_k 
    READ (UNIT = kgen_unit) kgenref_j 
    READ (UNIT = kgen_unit) kgenref_i 
    READ (UNIT = kgen_unit) kgenref_nn 
    READ (UNIT = kgen_unit) kgenref_l 
    READ (UNIT = kgen_unit) kgenref_kx 
    READ (UNIT = kgen_unit) kgenref_qsat 
    READ (UNIT = kgen_unit) kgenref_i_rad 
    READ (UNIT = kgen_unit) kgenref_j_rad 
    READ (UNIT = kgen_unit) kgenref_crm_accel_ceaseflag 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cwp, kgen_unit, "kgenref_cwp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cwph, kgen_unit, "kgenref_cwph", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cwpm, kgen_unit, "kgenref_cwpm", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cwpl, kgen_unit, "kgenref_cwpl", .FALSE.) 
    CALL kr_crm_logical___dim3(kgenref_flag_top, kgen_unit, "kgenref_flag_top", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cltemp, kgen_unit, "kgenref_cltemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cmtemp, kgen_unit, "kgenref_cmtemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_chtemp, kgen_unit, "kgenref_chtemp", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim3(kgenref_cttemp, kgen_unit, "kgenref_cttemp", .FALSE.) 
    CALL kr_crm_real__r8_dim2(kgenref_mui_crm, kgen_unit, "kgenref_mui_crm", .FALSE.) 
    CALL kr_crm_real__r8_dim2(kgenref_mdi_crm, kgen_unit, "kgenref_mdi_crm", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(kgenref_crm_rad_temperature, kgen_unit, "kgenref_crm_rad_temperature", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(kgenref_crm_rad_qv, kgen_unit, "kgenref_crm_rad_qv", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(kgenref_crm_rad_qc, kgen_unit, "kgenref_crm_rad_qc", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(kgenref_crm_rad_qi, kgen_unit, "kgenref_crm_rad_qi", .FALSE.) 
    CALL kr_crm_real__crm_rknd_dim4_ptr(kgenref_crm_rad_cld, kgen_unit, "kgenref_crm_rad_cld", .FALSE.) 


  !$omp target enter data map(alloc: t00      )
  !$omp target enter data map(alloc: tln      )
  !$omp target enter data map(alloc: qln      )
  !$omp target enter data map(alloc: qccln    )
  !$omp target enter data map(alloc: qiiln    )
  !$omp target enter data map(alloc: uln      )
  !$omp target enter data map(alloc: vln      )
  !$omp target enter data map(alloc: cwp      )
  !$omp target enter data map(alloc: cwph     )
  !$omp target enter data map(alloc: cwpm     )
  !$omp target enter data map(alloc: cwpl     )
  !$omp target enter data map(alloc: flag_top )
  !$omp target enter data map(alloc: cltemp   )
  !$omp target enter data map(alloc: cmtemp   )
  !$omp target enter data map(alloc: chtemp   )
  !$omp target enter data map(alloc: cttemp   )
  !$omp target enter data map(alloc: dd_crm   )
  !$omp target enter data map(alloc: mui_crm  )
  !$omp target enter data map(alloc: mdi_crm  )
  !$omp target enter data map(alloc: ustar    )
  !$omp target enter data map(alloc: bflx     )
  !$omp target enter data map(alloc: wnd      )
  !$omp target enter data map(alloc: qtot     )
  !$omp target enter data map(alloc: colprec  )
  !$omp target enter data map(alloc: colprecs )

  !$omp target enter data map(alloc: crm_rad_temperature )
  !$omp target enter data map(alloc: crm_rad_qv )
  !$omp target enter data map(alloc: crm_rad_qc )
  !$omp target enter data map(alloc: crm_rad_qi )
  !$omp target enter data map(alloc: crm_rad_cld )
  !$omp target enter data map(alloc: crm_rad_qrad )

  !$omp target enter data map(alloc: crm_state_u_wind )
  !$omp target enter data map(alloc: crm_state_v_wind )
  !$omp target enter data map(alloc: crm_state_w_wind )
  !$omp target enter data map(alloc: crm_state_temperature )
  !$omp target enter data map(alloc: crm_state_qt )
  !$omp target enter data map(alloc: crm_state_qp )
  !$omp target enter data map(alloc: crm_state_qn )


  !Loop over "vector columns"


!-----------------------------------------------


!-----------------------------------------


  ! update data at device
  !$omp taskwait
  !  Initialize CRM fields:

  !$omp target teams distribute parallel do collapse(4)

  ! limit the velocity at the very first step:


  ! Populate microphysics array from crm_state


  !$omp target teams distribute parallel do collapse(4)


  ! initialize sgs fields


  !$omp target teams distribute parallel do 

  !$omp target teams distribute parallel do collapse(2)

  !$omp target teams distribute parallel do collapse(4)

  !$omp target teams distribute parallel do collapse(2)

  !$omp target teams distribute parallel do 

!---------------------------------------------------

  !$omp target teams distribute parallel do collapse(2)

  !$omp target teams distribute parallel do 

!--------------------------------------------------


  !========================================================================================
  !----------------------------------------------------------------------------------------
  !   Main time loop
  !----------------------------------------------------------------------------------------
  !========================================================================================

  !$omp taskwait

    IF (kgen_evalstage) THEN 
    END IF   
    IF (kgen_warmupstage) THEN 
    END IF   
    IF (kgen_mainstage) THEN 
    END IF   
      
    !Uncomment following call statement to turn on perturbation experiment. 
    !Adjust perturbation value and/or kind parameter if required. 
    !CALL kgen_perturb_real( your_variable, 1.0D-15 ) 
      
      
    !call to kgen kernel 
    nstep = nstep + 1
    !$omp target teams distribute parallel do 
    do icrm = 1 , ncrms
      crm_output_timing_factor(icrm) = crm_output_timing_factor(icrm)+1
    enddo

    !------------------------------------------------------------------
    !  Check if the dynamical time step should be decreased
    !  to handle the cases when the flow being locally linearly unstable
    !------------------------------------------------------------------
    call kurant(ncrms)
    !$omp taskwait

    do icyc=1,ncycle
      icycle = icyc
      dtn = dt/ncycle
      dt3(na) = dtn
      dtfactor = dtn/dt

      !---------------------------------------------
      !  	the Adams-Bashforth scheme in time
      call abcoefs(ncrms)

      !---------------------------------------------
      !  	initialize stuff:
      call zero(ncrms)

      !-----------------------------------------------------------
      !       Buoyancy term:
      call buoyancy(ncrms)

      !------------------------------------------------------------
      !       Large-scale and surface forcing:
      call forcing(ncrms)

      ! Apply radiative tendency
      !$omp target teams distribute parallel do collapse(4)
      do k=1,nzm
        do j=1,ny
          do i=1,nx
            do icrm = 1 , ncrms
              i_rad = (i-1) / (nx/crm_nx_rad) + 1
              j_rad = (j-1) / (ny/crm_ny_rad) + 1
              !$omp atomic update
              t(icrm,i,j,k) = t(icrm,i,j,k) + crm_rad_qrad(icrm,i_rad,j_rad,k)*dtn
            enddo
          enddo
        enddo
      enddo

      !----------------------------------------------------------
      !   	suppress turbulence near the upper boundary (spange):
      if (dodamping) call damping(ncrms)

      !---------------------------------------------------------
      !   Ice fall-out
      if(docloud) then
        call ice_fall(ncrms)
      endif

      !----------------------------------------------------------
      !     Update scalar boundaries after large-scale processes:
      call boundaries(ncrms,3)

      !---------------------------------------------------------
      !     Update boundaries for velocities:
      call boundaries(ncrms,0)

      !-----------------------------------------------
      !     surface fluxes:
      if (dosurface) call crm_surface(ncrms,bflx)

      !-----------------------------------------------------------
      !  SGS physics:
      if (dosgs) call sgs_proc(ncrms)

      !----------------------------------------------------------
      !     Fill boundaries for SGS diagnostic fields:
      call boundaries(ncrms,4)

      !-----------------------------------------------
      !       advection of momentum:
      call advect_mom(ncrms)

      !----------------------------------------------------------
      !	SGS effects on momentum:
      if(dosgs) call sgs_mom(ncrms)

      !-----------------------------------------------------------
      !       Coriolis force:
      if (docoriolis) call coriolis(ncrms)

      !---------------------------------------------------------
      !       compute rhs of the Poisson equation and solve it for pressure.
      call pressure(ncrms)

      !---------------------------------------------------------
      !       find velocity field at n+1/2 timestep needed for advection of scalars:
      !  Note that at the end of the call, the velocities are in nondimensional form.
      call adams(ncrms)

      !----------------------------------------------------------
      !     Update boundaries for all prognostic scalar fields for advection:
      call boundaries(ncrms,2)

      !---------------------------------------------------------
      !      advection of scalars :
      call advect_all_scalars(ncrms)

      !-----------------------------------------------------------
      !    Convert velocity back from nondimensional form:
      call uvw(ncrms)

      !----------------------------------------------------------
      !     Update boundaries for scalars to prepare for SGS effects:
      call boundaries(ncrms,3)

      !---------------------------------------------------------
      !      SGS effects on scalars :
      if (dosgs) call sgs_scalars(ncrms)

      !-----------------------------------------------------------
      !       Calculate PGF for scalar momentum tendency

      !-----------------------------------------------------------
      !       Cloud condensation/evaporation and precipitation processes:
      if(docloud) call micro_proc(ncrms)

      !-----------------------------------------------------------
      !       Apply mean-state acceleration
      if (use_crm_accel .and. .not. crm_accel_ceaseflag) then
        ! Use Jones-Bretherton-Pritchard methodology to accelerate
        ! CRM horizontal mean evolution artificially.
        call accelerate_crm(ncrms, nstep, nstop, crm_accel_ceaseflag)
      endif

      !-----------------------------------------------------------
      !    Compute diagnostics fields:
      call diagnose(ncrms)

      !----------------------------------------------------------
      ! Rotate the dynamic tendency arrays for Adams-bashforth scheme:
      nn=na
      na=nc
      nc=nb
      nb=nn
    enddo ! icycle


    !$omp target teams distribute parallel do collapse(3)
    do j = 1 , ny
      do i = 1 , nx
        do icrm = 1 , ncrms
          cwp(icrm,i,j) = 0.
          cwph(icrm,i,j) = 0.
          cwpm(icrm,i,j) = 0.
          cwpl(icrm,i,j) = 0.

          flag_top(icrm,i,j) = .true.

          cltemp(icrm,i,j) = 0.0; cmtemp(icrm,i,j) = 0.0
          chtemp(icrm,i,j) = 0.0; cttemp(icrm,i,j) = 0.0
        enddo
      enddo
    enddo
    !$omp target teams distribute parallel do collapse(3)
    do j=1,ny
      do i=1,nx
        do icrm = 1 , ncrms
          do k=1,nzm
            l = plev-k+1
            tmp1 = rho(icrm,nz-k)*adz(icrm,nz-k)*dz(icrm)*(qcl(icrm,i,j,nz-k)+qci(icrm,i,j,nz-k))
            !$omp atomic update
            cwp(icrm,i,j) = cwp(icrm,i,j)+tmp1
            cttemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cttemp(icrm,i,j))
            if(cwp(icrm,i,j).gt.cwp_threshold.and.flag_top(icrm,i,j)) then
                !$omp atomic update
                crm_output_cldtop(icrm,l) = crm_output_cldtop(icrm,l) + 1
                flag_top(icrm,i,j) = .false.
            endif
            if(pres(icrm,nz-k).ge.700.) then
                !$omp atomic update
                cwpl(icrm,i,j) = cwpl(icrm,i,j)+tmp1
                cltemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cltemp(icrm,i,j))
            else if(pres(icrm,nz-k).lt.400.) then
                !$omp atomic update
                cwph(icrm,i,j) = cwph(icrm,i,j)+tmp1
                chtemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), chtemp(icrm,i,j))
            else
                !$omp atomic update
                cwpm(icrm,i,j) = cwpm(icrm,i,j)+tmp1
                cmtemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cmtemp(icrm,i,j))
            endif
            tmp1 = rho(icrm,k)*adz(icrm,k)*dz(icrm)
            if(tmp1*(qcl(icrm,i,j,k)+qci(icrm,i,j,k)).gt.cwp_threshold) then
                 !$omp atomic update
                 crm_output_cld(icrm,l) = crm_output_cld(icrm,l) + cf3d(icrm,i,j,k)
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).gt.2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * cf3d(icrm,i,j,k)
                   !$omp atomic update
                   crm_output_mcup (icrm,l) = crm_output_mcup (icrm,l) + tmp
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * (1.0 - cf3d(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcuup(icrm,l) = crm_output_mcuup(icrm,l) + tmp
                 endif
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).lt.-2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * cf3d(icrm,i,j,k)
                   !$omp atomic update
                   crm_output_mcdn (icrm,l) = crm_output_mcdn (icrm,l) + tmp
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * (1. - cf3d(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcudn(icrm,l) = crm_output_mcudn(icrm,l) + tmp
                 endif
            else
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).gt.2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcuup(icrm,l) = crm_output_mcuup(icrm,l) + tmp
                 endif
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).lt.-2*wmin) then
                    tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcudn(icrm,l) = crm_output_mcudn(icrm,l) + tmp
                 endif
            endif
           !$omp atomic update
            crm_output_gliqwp(icrm,l) = crm_output_gliqwp(icrm,l) + qcl(icrm,i,j,k)
            !$omp atomic update
            crm_output_gicewp(icrm,l) = crm_output_gicewp(icrm,l) + qci(icrm,i,j,k)
          enddo
        enddo
      enddo
    enddo
    !$omp target teams distribute parallel do collapse(4)
    do k=1,nzm
      do j=1,ny
        do i=1,nx
          do icrm = 1 , ncrms
            ! Reduced radiation method allows for fewer radiation calculations
            ! by collecting statistics and doing radiation over column groups
            i_rad = (i-1) / (nx/crm_nx_rad) + 1
            j_rad = (j-1) / (ny/crm_ny_rad) + 1
            !$omp atomic update
            crm_rad_temperature(icrm,i_rad,j_rad,k) = crm_rad_temperature(icrm,i_rad,j_rad,k) + tabs(icrm,i,j,k)
            tmp = max(real(0.,crm_rknd),qv(icrm,i,j,k))
            !$omp atomic update
            crm_rad_qv         (icrm,i_rad,j_rad,k) = crm_rad_qv         (icrm,i_rad,j_rad,k) + tmp
            !$omp atomic update
            crm_rad_qc         (icrm,i_rad,j_rad,k) = crm_rad_qc         (icrm,i_rad,j_rad,k) + qcl(icrm,i,j,k)
            !$omp atomic update
            crm_rad_qi         (icrm,i_rad,j_rad,k) = crm_rad_qi         (icrm,i_rad,j_rad,k) + qci(icrm,i,j,k)
            if (qcl(icrm,i,j,k) + qci(icrm,i,j,k) > 0) then
               !$omp atomic update
               crm_rad_cld     (icrm,i_rad,j_rad,k) = crm_rad_cld        (icrm,i_rad,j_rad,k) + cf3d(icrm,i,j,k)
            endif
          enddo
        enddo
      enddo
    enddo

    ! Diagnose mass fluxes to drive None's convective transport of tracers.
    ! definition of mass fluxes is taken from Xu et al., 2002, QJRMS.
    !$omp target teams distribute parallel do collapse(3)
    do j=1, ny
      do i=1, nx
        do icrm = 1 , ncrms
          do k=1, nzm+1
            l=plev+1-k+1
            if(w(icrm,i,j,k).gt.0.) then
              kx=max(1, k-1)
              qsat = qsatw_crm(tabs(icrm,i,j,kx),pres(icrm,kx))
              if(qcl(icrm,i,j,kx)+qci(icrm,i,j,kx).gt.min(real(1.e-5,crm_rknd),0.01*qsat)) then
                tmp = rhow(icrm,k)*w(icrm,i,j,k)
                !$omp atomic update
                mui_crm(icrm,l) = mui_crm(icrm,l)+tmp
              endif
            else if (w(icrm,i,j,k).lt.0.) then
              kx=min(k+1, nzm)
              qsat = qsatw_crm(tabs(icrm,i,j,kx),pres(icrm,kx))
              if(qcl(icrm,i,j,kx)+qci(icrm,i,j,kx).gt.min(real(1.e-5,crm_rknd),0.01*qsat)) then
                tmp = rhow(icrm,k)*w(icrm,i,j,k)
                !$omp atomic update
                mdi_crm(icrm,l) = mdi_crm(icrm,l)+tmp
              else if(qpl(icrm,i,j,kx)+qpi(icrm,i,j,kx).gt.1.0e-4) then
                tmp = rhow(icrm,k)*w(icrm,i,j,k)
               !$omp atomic update
                mdi_crm(icrm,l) = mdi_crm(icrm,l)+tmp
              endif
            endif
          enddo
        enddo
      enddo
    enddo
    !$omp target teams distribute parallel do collapse(3)
    do j=1,ny
      do i=1,nx
        do icrm = 1 , ncrms
          if(cwp(icrm,i,j).gt.cwp_threshold) then
            !$omp atomic update
            crm_output_cltot(icrm) = crm_output_cltot(icrm) + cttemp(icrm,i,j)
          endif
          if(cwph(icrm,i,j).gt.cwp_threshold) then
            !$omp atomic update
            crm_output_clhgh(icrm) = crm_output_clhgh(icrm) + chtemp(icrm,i,j)
          endif
          if(cwpm(icrm,i,j).gt.cwp_threshold) then
            !$omp atomic update
            crm_output_clmed(icrm) = crm_output_clmed(icrm) + cmtemp(icrm,i,j)
          endif
          if(cwpl(icrm,i,j).gt.cwp_threshold) then
            !$omp atomic update
            crm_output_cllow(icrm) = crm_output_cllow(icrm) + cltemp(icrm,i,j)
          endif
        enddo
      enddo
    enddo
    IF (kgen_mainstage) THEN 
          
        !verify init 
        CALL kgen_init_verify(tolerance=1.D-14, minvalue=1.D-14, verboseLevel=1) 
        !CALL kgen_init_verify(tolerance=1.D-7, minvalue=1.D-14, verboseLevel=1) 
        CALL kgen_init_check(check_status, rank=kgen_mpirank) 
          
        !extern verify variables 
        CALL kv_externs_vars(check_status) 
        CALL kv_externs_grid(check_status) 
        CALL kv_externs_params(check_status) 
        CALL kv_externs_microphysics(check_status) 
        CALL kv_externs_micro_params(check_status) 
        CALL kv_externs_crm_output_module(check_status) 
          
        !local verify variables 
        !TMPVAR CALL kv_crm_real__crm_rknd("tmp", check_status, tmp, kgenref_tmp) 
        !TMPVAR CALL kv_crm_real__crm_rknd("tmp1", check_status, tmp1, kgenref_tmp1) 
        !TMPVAR CALL kv_crm_integer__("nn", check_status, nn, kgenref_nn) 
        !TMPVAR CALL kv_crm_integer__("icrm", check_status, icrm, kgenref_icrm) 
        !TMPVAR CALL kv_crm_integer__("i", check_status, i, kgenref_i) 
        !TMPVAR CALL kv_crm_integer__("k", check_status, k, kgenref_k) 
        !TMPVAR CALL kv_crm_integer__("j", check_status, j, kgenref_j) 
        !TMPVAR CALL kv_crm_integer__("l", check_status, l, kgenref_l) 
        !TMPVAR CALL kv_crm_integer__("icyc", check_status, icyc, kgenref_icyc) 
        !TMPVAR CALL kv_crm_integer__("kx", check_status, kx, kgenref_kx) 
        CALL kv_crm_real__crm_rknd("qsat", check_status, qsat, kgenref_qsat) 
        !TMPVAR CALL kv_crm_integer__("i_rad", check_status, i_rad, kgenref_i_rad) 
        !TMPVAR CALL kv_crm_integer__("j_rad", check_status, j_rad, kgenref_j_rad) 
        CALL kv_crm_logical__("crm_accel_ceaseflag", check_status, crm_accel_ceaseflag, kgenref_crm_accel_ceaseflag) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cwp", check_status, cwp, kgenref_cwp) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cwph", check_status, cwph, kgenref_cwph) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cwpm", check_status, cwpm, kgenref_cwpm) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cwpl", check_status, cwpl, kgenref_cwpl) 
        !TMPVAR CALL kv_crm_logical___dim3("flag_top", check_status, flag_top, kgenref_flag_top) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cltemp", check_status, cltemp, kgenref_cltemp) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cmtemp", check_status, cmtemp, kgenref_cmtemp) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("chtemp", check_status, chtemp, kgenref_chtemp) 
        !TMPVAR CALL kv_crm_real__crm_rknd_dim3("cttemp", check_status, cttemp, kgenref_cttemp) 
        CALL kv_crm_real__r8_dim2("mui_crm", check_status, mui_crm, kgenref_mui_crm) 
        CALL kv_crm_real__r8_dim2("mdi_crm", check_status, mdi_crm, kgenref_mdi_crm) 
        CALL kv_crm_real__crm_rknd_dim4_ptr("crm_rad_temperature", check_status, crm_rad_temperature, &
        &kgenref_crm_rad_temperature) 
        CALL kv_crm_real__crm_rknd_dim4_ptr("crm_rad_qv", check_status, crm_rad_qv, kgenref_crm_rad_qv) 
        CALL kv_crm_real__crm_rknd_dim4_ptr("crm_rad_qc", check_status, crm_rad_qc, kgenref_crm_rad_qc) 
        CALL kv_crm_real__crm_rknd_dim4_ptr("crm_rad_qi", check_status, crm_rad_qi, kgenref_crm_rad_qi) 
        CALL kv_crm_real__crm_rknd_dim4_ptr("crm_rad_cld", check_status, crm_rad_cld, kgenref_crm_rad_cld) 
        IF (check_status%rank == 0) THEN 
            WRITE (*, *) "" 
        END IF   
        IF (kgen_verboseLevel > 0) THEN 
            IF (check_status%rank == 0) THEN 
                WRITE (*, *) "Number of output variables: ", check_status%numTotal 
                WRITE (*, *) "Number of identical variables: ", check_status%numIdentical 
                WRITE (*, *) "Number of non-identical variables within tolerance: ", check_status%numInTol 
                WRITE (*, *) "Number of non-identical variables out of tolerance: ", check_status%numOutTol 
                WRITE (*, *) "Tolerance: ", kgen_tolerance 
            END IF   
        END IF   
        IF (check_status%rank == 0) THEN 
            WRITE (*, *) "" 
        END IF   
        IF (check_status%numOutTol > 0) THEN 
            IF (check_status%rank == 0) THEN 
                WRITE (*, *) "Verification FAILED with" // TRIM(ADJUSTL(kgen_filepath)) 
            END IF   
            check_status%Passed = .FALSE. 
            kgen_isverified = .FALSE. 
        ELSE 
            IF (check_status%rank == 0) THEN 
                WRITE (*, *) "Verification PASSED with " // TRIM(ADJUSTL(kgen_filepath)) 
            END IF   
            check_status%Passed = .TRUE. 
            kgen_isverified = .TRUE. 
        END IF   
        IF (check_status%rank == 0) THEN 
            WRITE (*, *) "" 
        END IF   
          
#ifdef _MPI 
        call mpi_barrier(mpi_comm_world, kgen_ierr) 
#endif 
          
        CALL SYSTEM_CLOCK(kgen_start_clock, kgen_rate_clock) 
        DO kgen_intvar = 1, KGEN_MAXITER 
    nstep = nstep + 1
    !$omp target teams distribute parallel do 
    do icrm = 1 , ncrms
      crm_output_timing_factor(icrm) = crm_output_timing_factor(icrm)+1
    enddo

    !------------------------------------------------------------------
    !  Check if the dynamical time step should be decreased
    !  to handle the cases when the flow being locally linearly unstable
    !------------------------------------------------------------------
    call kurant(ncrms)
    !$omp taskwait

    do icyc=1,ncycle
      icycle = icyc
      dtn = dt/ncycle
      dt3(na) = dtn
      dtfactor = dtn/dt

      !---------------------------------------------
      !  	the Adams-Bashforth scheme in time
      call abcoefs(ncrms)

      !---------------------------------------------
      !  	initialize stuff:
      call zero(ncrms)

      !-----------------------------------------------------------
      !       Buoyancy term:
      call buoyancy(ncrms)

      !------------------------------------------------------------
      !       Large-scale and surface forcing:
      call forcing(ncrms)

      ! Apply radiative tendency
      !$omp target teams distribute parallel do collapse(4)
      do k=1,nzm
        do j=1,ny
          do i=1,nx
            do icrm = 1 , ncrms
              i_rad = (i-1) / (nx/crm_nx_rad) + 1
              j_rad = (j-1) / (ny/crm_ny_rad) + 1
              !$omp atomic update
              t(icrm,i,j,k) = t(icrm,i,j,k) + crm_rad_qrad(icrm,i_rad,j_rad,k)*dtn
            enddo
          enddo
        enddo
      enddo

      !----------------------------------------------------------
      !   	suppress turbulence near the upper boundary (spange):
      if (dodamping) call damping(ncrms)

      !---------------------------------------------------------
      !   Ice fall-out
      if(docloud) then
        call ice_fall(ncrms)
      endif

      !----------------------------------------------------------
      !     Update scalar boundaries after large-scale processes:
      call boundaries(ncrms,3)

      !---------------------------------------------------------
      !     Update boundaries for velocities:
      call boundaries(ncrms,0)

      !-----------------------------------------------
      !     surface fluxes:
      if (dosurface) call crm_surface(ncrms,bflx)

      !-----------------------------------------------------------
      !  SGS physics:
      if (dosgs) call sgs_proc(ncrms)

      !----------------------------------------------------------
      !     Fill boundaries for SGS diagnostic fields:
      call boundaries(ncrms,4)

      !-----------------------------------------------
      !       advection of momentum:
      call advect_mom(ncrms)

      !----------------------------------------------------------
      !	SGS effects on momentum:
      if(dosgs) call sgs_mom(ncrms)

      !-----------------------------------------------------------
      !       Coriolis force:
      if (docoriolis) call coriolis(ncrms)

      !---------------------------------------------------------
      !       compute rhs of the Poisson equation and solve it for pressure.
      call pressure(ncrms)

      !---------------------------------------------------------
      !       find velocity field at n+1/2 timestep needed for advection of scalars:
      !  Note that at the end of the call, the velocities are in nondimensional form.
      call adams(ncrms)

      !----------------------------------------------------------
      !     Update boundaries for all prognostic scalar fields for advection:
      call boundaries(ncrms,2)

      !---------------------------------------------------------
      !      advection of scalars :
      call advect_all_scalars(ncrms)

      !-----------------------------------------------------------
      !    Convert velocity back from nondimensional form:
      call uvw(ncrms)

      !----------------------------------------------------------
      !     Update boundaries for scalars to prepare for SGS effects:
      call boundaries(ncrms,3)

      !---------------------------------------------------------
      !      SGS effects on scalars :
      if (dosgs) call sgs_scalars(ncrms)

      !-----------------------------------------------------------
      !       Calculate PGF for scalar momentum tendency

      !-----------------------------------------------------------
      !       Cloud condensation/evaporation and precipitation processes:
      if(docloud) call micro_proc(ncrms)

      !-----------------------------------------------------------
      !       Apply mean-state acceleration
      if (use_crm_accel .and. .not. crm_accel_ceaseflag) then
        ! Use Jones-Bretherton-Pritchard methodology to accelerate
        ! CRM horizontal mean evolution artificially.
        call accelerate_crm(ncrms, nstep, nstop, crm_accel_ceaseflag)
      endif

      !-----------------------------------------------------------
      !    Compute diagnostics fields:
      call diagnose(ncrms)

      !----------------------------------------------------------
      ! Rotate the dynamic tendency arrays for Adams-bashforth scheme:
      nn=na
      na=nc
      nc=nb
      nb=nn
    enddo ! icycle


    !$omp target teams distribute parallel do collapse(3)
    do j = 1 , ny
      do i = 1 , nx
        do icrm = 1 , ncrms
          cwp(icrm,i,j) = 0.
          cwph(icrm,i,j) = 0.
          cwpm(icrm,i,j) = 0.
          cwpl(icrm,i,j) = 0.

          flag_top(icrm,i,j) = .true.

          cltemp(icrm,i,j) = 0.0; cmtemp(icrm,i,j) = 0.0
          chtemp(icrm,i,j) = 0.0; cttemp(icrm,i,j) = 0.0
        enddo
      enddo
    enddo
    !$omp target teams distribute parallel do collapse(3)
    do j=1,ny
      do i=1,nx
        do icrm = 1 , ncrms
          do k=1,nzm
            call extrae_user_function(1)
            l = plev-k+1
            tmp1 = rho(icrm,nz-k)*adz(icrm,nz-k)*dz(icrm)*(qcl(icrm,i,j,nz-k)+qci(icrm,i,j,nz-k))
            !$omp atomic update
            cwp(icrm,i,j) = cwp(icrm,i,j)+tmp1
            cttemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cttemp(icrm,i,j))
            if(cwp(icrm,i,j).gt.cwp_threshold.and.flag_top(icrm,i,j)) then
                !$omp atomic update
                crm_output_cldtop(icrm,l) = crm_output_cldtop(icrm,l) + 1
                flag_top(icrm,i,j) = .false.
            endif
            if(pres(icrm,nz-k).ge.700.) then
                !$omp atomic update
                cwpl(icrm,i,j) = cwpl(icrm,i,j)+tmp1
                cltemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cltemp(icrm,i,j))
            else if(pres(icrm,nz-k).lt.400.) then
                !$omp atomic update
                cwph(icrm,i,j) = cwph(icrm,i,j)+tmp1
                chtemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), chtemp(icrm,i,j))
            else
                !$omp atomic update
                cwpm(icrm,i,j) = cwpm(icrm,i,j)+tmp1
                cmtemp(icrm,i,j) = max(cf3d(icrm,i,j,nz-k), cmtemp(icrm,i,j))
            endif
            tmp1 = rho(icrm,k)*adz(icrm,k)*dz(icrm)
            if(tmp1*(qcl(icrm,i,j,k)+qci(icrm,i,j,k)).gt.cwp_threshold) then
                 !$omp atomic update
                 crm_output_cld(icrm,l) = crm_output_cld(icrm,l) + cf3d(icrm,i,j,k)
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).gt.2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * cf3d(icrm,i,j,k)
                   !$omp atomic update
                   crm_output_mcup (icrm,l) = crm_output_mcup (icrm,l) + tmp
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * (1.0 - cf3d(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcuup(icrm,l) = crm_output_mcuup(icrm,l) + tmp
                 endif
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).lt.-2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * cf3d(icrm,i,j,k)
                   !$omp atomic update
                   crm_output_mcdn (icrm,l) = crm_output_mcdn (icrm,l) + tmp
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k)) * (1. - cf3d(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcudn(icrm,l) = crm_output_mcudn(icrm,l) + tmp
                 endif
            else
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).gt.2*wmin) then
                   tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcuup(icrm,l) = crm_output_mcuup(icrm,l) + tmp
                 endif
                 if(w(icrm,i,j,k+1)+w(icrm,i,j,k).lt.-2*wmin) then
                    tmp = rho(icrm,k)*0.5*(w(icrm,i,j,k+1)+w(icrm,i,j,k))
                   !$omp atomic update
                   crm_output_mcudn(icrm,l) = crm_output_mcudn(icrm,l) + tmp
                 endif
            endif
           !$omp atomic update
            crm_output_gliqwp(icrm,l) = crm_output_gliqwp(icrm,l) + qcl(icrm,i,j,k)
            !$omp atomic update
            crm_output_gicewp(icrm,l) = crm_output_gicewp(icrm,l) + qci(icrm,i,j,k)
            call extrae_user_function(0)
          enddo
        enddo
      enddo
    enddo
    !$omp target teams distribute parallel do collapse(4)
    do k=1,nzm