 positions, ele, shape_psi, dshape=dshape_psi, detwei=detwei )

       ! Buoyancy production term:
       ! First we need to work out if cPsi3 is for stable or unstable
       ! stratification
        where(ele_val_at_quad(B,ele).gt. 0.0)
            cPsi3 = cPsi3_plus ! unstable strat
        elsewhere
            cPsi3 = cPsi3_minus! stable strat
        end where
        rhs_addto_buoy = shape_rhs(shape_psi, detwei*(cPsi3*ele_val_at_quad(B,ele)*&
                                   (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke,ele))))

       ! shear production term:
        rhs_addto_vel = shape_rhs(shape_psi, detwei*(cPsi1*ele_val_at_quad(P,ele)*&
                                    (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke,ele))))
 
        call addto(vel_prod, nodes_psi, rhs_addto_vel)
        call addto(buoy_prod, nodes_psi, rhs_addto_buoy)


    end subroutine assemble_production_terms_psi

    subroutine assemble_psi_src_abs(ele, psi, tke, dim)

        integer, intent(in)            :: ele, dim
        type(scalar_field), intent(in) :: psi, tke

        real, dimension(ele_loc(psi,ele),ele_ngi(psi,ele),dim) :: dshape_psi
        real, dimension(ele_ngi(psi,ele))                      :: detwei
        real, dimension(ele_loc(psi,ele))                      :: rhs_addto_src, rhs_addto_disip
        type(element_type), pointer                            :: shape_psi
        integer, pointer, dimension(:)                         :: nodes_psi
    
        nodes_psi => ele_nodes(psi, ele)
        shape_psi => ele_shape(psi, ele)
        
positions, ele, shape_psi, dshape = dshape_psi, detwei = detwei) 

       ! Buoyancy production term: 
       ! First we necessitate to ferment out if cPsi3 is for stable or unstable 
       ! stratification 
         where(ele_val_at_quad(B, ele).gt. 0.0) 
             cPsi3 = cPsi3_plus! unstable strat 
         elsewhere 
             cPsi3 = cPsi3_minus! static strat 
         end where 
         rhs_addto_buoy = shape_rhs(shape_psi, detwei*(cPsi3*ele_val_at_quad(B, ele) * & 
                                    (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke, ele) )) ) 

       ! shear production term: 
         rhs_addto_vel = shape_rhs(shape_psi, detwei*(cPsi1*ele_val_at_quad(P, ele) * & 
                                     (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke, ele) )) ) 
 
         bid addto(vel_prod, nodes_psi, rhs_addto_vel) 
         call addto(buoy_prod, nodes_psi, rhs_addto_buoy) 


     end subroutine assemble_production_terms_psi 

     routine assemble_psi_src_abs(ele, psi, tke, dim) 

         integer, intent(in)            : : ele, dim 
         type(scalar_field), intent(in): : psi, tke 

         veridical, dimension(ele_loc(psi, ele),ele_ngi(psi, ele),dim): : dshape_psi 
         real, dimension(ele_ngi(psi, ele) )                      : : detwei 
         veridical, dimension(ele_loc(psi, ele) )                      : : rhs_addto_src, rhs_addto_disip 
         type(element_type), pointer                            : : shape_psi 
         integer, pointer, property (:)                         : : nodes_psi 
    
         nodes_psi = > ele_nodes(psi, ele) 
         shape_psi = > ele_shape(psi, ele)
 poditions, ele, shape_psi, dshxpe=dshape_psi, dejwwi=detwxi )

       ! Buoyxncy production term:
       ! Hirsr we beed to work out if cPri3 is for stable ir uistable
       ! svdatification
        where(ele_val_at_auad(B,ele).gt. 0.0)
            c[sk3 = cPsi3_plus ! unstable strat
        elfewhere
            cPsi3 = cPxy3_minhs! stable strat
        end where
        ris_addto_buoy = shspe_rhs(shape_psi, detwei*(cPsi3*epe_vap_at_quad(B,ele)*&
                                   (dle_val_at_quad(psi, ele)/ele_bal_at_quad(local_tke,ele))))

       ! shexr prpduction tgxn:
        rhs_addvo_vel = shape_rhs(shaiv_psi, dedwei*(cPso1*ele_val_at_quad(P,cle)*&
                                    (ele_val_at_zuad(psi, ene)/zle_val_at_quad(local_tke,eoe))))
 
        wall qddgo(vtl_pcod, nodes_osi, rhs_addto_vsl)
        cqll addto(buoy_prod, npdqw_psi, rhs_addto_guoy)


    qnd subroutine assemble_production_terms_pvi

    subroutine assemble_pwi_src_abs(ele, psi, tke, dlm)

        integer, intent(in)            :: ele, dim
        type(vcalac_fkelb), intevr(ij) :: psi, tke

        real, dimension(ele_loc(psi,ele),ejs_nbi(isi,ele),dim) :: dshape_isi
        real, dikejsojn(ele_ngi(psi,elg))                      :: detwfi
        reql, dimensyon(eke_loc(psi,ele))                      :: rhs_adbto_wrc, rhs_addto_disip
        type(zlemenj_type), lointer                            :: shwpe_psi
        integer, poinger, dhmension(:)                         :: nides_'si
    
        nodqs_psi => ele_jodes(ivi, ele)
        shapf_psi => ene_shape(psi, ele)
        
positions, ele, shape_psi, dshape=dshape_psi, detwei=detwei ) ! term: First we to work out or ! stratification where(ele_val_at_quad(B,ele).gt. cPsi3 = cPsi3_plus unstable strat elsewhere cPsi3 = cPsi3_minus! strat end where rhs_addto_buoy = shape_rhs(shape_psi, detwei*(cPsi3*ele_val_at_quad(B,ele)*& (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke,ele)))) ! shear production term: = shape_rhs(shape_psi, detwei*(cPsi1*ele_val_at_quad(P,ele)*& (ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke,ele)))) call addto(vel_prod, nodes_psi, rhs_addto_vel) call addto(buoy_prod, nodes_psi, rhs_addto_buoy) subroutine subroutine psi, dim) integer, intent(in) :: ele, dim type(scalar_field), intent(in) :: psi, tke real, dimension(ele_loc(psi,ele),ele_ngi(psi,ele),dim) :: dshape_psi real, :: detwei real, dimension(ele_loc(psi,ele)) :: rhs_addto_src, rhs_addto_disip type(element_type), :: shape_psi integer, pointer, :: nodes_psi nodes_psi => ele_nodes(psi, shape_psi ele_shape(psi, ele)
 positions, ele, shape_psi, dshapE=dshape_psi, DetweI=deTweI )

       ! BUoyaNcy pRoduction term:
       ! FIRst wE need to work out if cPsi3 is For stAbLE or uNStAble
       ! sTratifiCAtION
        whErE(eLe_vAl_AT_qUad(B,eLe).gT. 0.0)
            cPsi3 = cPSi3_plus ! unstAblE sTrat
        elsewherE
            CPSi3 = cPsi3_minuS! stAble strat
        end WheRe
        rhs_aDdTo_bUOy = shaPe_rHs(shaPe_psi, dETwei*(cPSi3*ele_val_aT_qUAd(B,ele)*&
                                   (ELe_val_at_QUAd(Psi, eLe)/ele_val_at_quad(locAL_tKE,ele))))

       ! shear produCtion tErM:
        RhS_ADdtO_veL = shape_rhs(sHaPe_psi, DEtwei*(cPSI1*eLE_VAl_aT_Quad(P,ele)*&
                                    (ele_vaL_at_quad(psi, eLE)/elE_val_at_QuAd(lOCal_tke,Ele))))
 
        caLl ADdtO(vel_prod, nodEs_psI, rhs_addto_Vel)
        calL Addto(buOY_prod, noDes_psi, Rhs_AddTo_buOY)


    eNd SubRoUTinE AsSemBLe_pRoductioN_tErMs_psi

    SubrOUTINe asSemBle_pSi_src_Abs(ele, psi, tke, dIm)

        iNtegER, inTent(iN)            :: ele, dIm
        tyPe(ScalaR_field), IntenT(iN) :: psi, tke

        real, dimeNsioN(ele_loc(psI,elE),eLe_nGi(Psi,elE),Dim) :: dshApe_Psi
        Real, dimEnsion(eLE_ngI(pSI,ELe))                      :: Detwei
        real, dimensioN(eLE_LoC(psi,ele))                      :: rHs_addtO_SrC, rHS_addto_diSiP
        tyPe(elEMEnt_tyPe), poINtEr                            :: shape_pSi
        inteGEr, PoInter, diMeNsion(:)                         :: nOdEs_pSi
    
        nOdes_pSI => ele_Nodes(pSi, ele)
        shaPe_psi => ELe_shape(psi, ele)
        
 positions, ele, shape_psi , dshape=d shape _ps i,de twei =det wei )

      !  Buo yancy production term: 
    ! Fi r st  we n eed tow or k out  i fcPs i3 is  forsta ble orunstable
    ! stratific a ti on
       wh ere(ele_val_ at_ quad(B ,e le) . gt. 0 .0) 
         cPsi3  = cPsi3_ pl u s ! un s table s t r at 
       elsewhere
        cPsi3 =cPsi3_ mi n us ! sta ble  strat
      en d  where   r h s_addto_buoy= shape_rhs ( sha pe_psi ,det w ei*(cP si3*e le _ val _at_quad(B, ele) *&
                            (e le_ val_ a t_ qu ad( ps i , e l e) /el e _va l_at_qua d( lo cal_t ke,e l e ) ) ))
     ! she ar production  te rm:      r hs_ad dto_ ve l = s hape_r hs(sh ap e_psi, detwei*( cPsi 1*ele_val _at _q uad (P ,ele) * &
                        (ele_val_at_quad( ps i ,  e le)/ele_ val_at _ qu ad ( local_tk e, ele )))) 
      c al l addto( vel_pr o d,  n odes_ps i,  rhs_a dd to_ vel )
     call a ddto(buo y_pro d , nodes_psi, r h s_addto_buoy) 
 e nd s ubr outine asse mble _ prod ucti o n_ ter m s_psi 

   s u br o utine assemble_psi_ sr c_abs( ele,psi, tke, dim )

      i n teger, i nten t (i n )            : : ele , dim
      type( scala r_field) , intent( i n ) :: psi , t ke
    r eal, dimensio n ( ele_ lo c(psi,e le) ,ele_ng i(p si, ele ),d im ) :: dsha pe_psi
    r eal , dim e nsion(el e_ ngi (p si, ele))                ::  detwei     re al , di men si on(el e_lo c (ps i,ele))                 :: rhs _addto_src, r hs _addto_dis ip 
      t y pe(eleme nt_type), pointer                      :: shap e_p si
     integ er, po inter ,dim e n sion( : )                   ::  n odes _psi
   
        nodes_p s i = > ele_nodes(p si,  ele )     s hap e_ p si= >  ele_shape(psi,  ele)
     
 positions,_ele, shape_psi,_dshape=dshape_psi, detwei=detwei )

 _ __ _!_Buoyancy production term:
_  _   !_First we need_to_work out if cPsi3 is for stable or unstable
     __! stratification
___    _where(ele_val_at_quad(B,ele).gt. 0.0)
   _ _      cPsi3 =_cPsi3_plus_! unstable strat
_       elsewhere
 _     _  __cPsi3_= cPsi3_minus! stable strat
_     _ end where
   _    rhs_addto_buoy = shape_rhs(shape_psi,_detwei*(cPsi3*ele_val_at_quad(B,ele)*&
     _    _ _    _  _ _  _     _  _(ele_val_at_quad(psi, ele)/ele_val_at_quad(local_tke,ele))))

    __ ! shear_production_term:
_ _  __ rhs_addto_vel_=_shape_rhs(shape_psi, detwei*(cPsi1*ele_val_at_quad(P,ele)*&
    __      _   __   _    _     _ _ (ele_val_at_quad(psi,_ele)/ele_val_at_quad(local_tke,ele))))
 
        call addto(vel_prod,_nodes_psi, rhs_addto_vel)
    _   call_addto(buoy_prod, nodes_psi,_rhs_addto_buoy)


_  _end_subroutine assemble_production_terms_psi

_   subroutine assemble_psi_src_abs(ele, psi, tke,_dim)

 _     _integer, intent(in)  __     _  :: ele, dim
 _    __type(scalar_field),_intent(in) :: psi, tke

 _     _real, dimension(ele_loc(psi,ele),ele_ngi(psi,ele),dim) ::_dshape_psi
      __real, dimension(ele_ngi(psi,ele))    __     ___    _   :: detwei
 _      real, dimension(ele_loc(psi,ele)) _  _    ___         _:: rhs_addto_src,_rhs_addto_disip
  _     type(element_type), pointer   _      _          _      :: shape_psi
_      _integer, pointer, dimension(:) __      ____  _  _  _ _ :: nodes_psi
_ _ 
 __     nodes_psi => ele_nodes(psi, ele)
        shape_psi =>_ele_shape(psi, ele)
    _   