Index: codes/icosagcm/devel/src/dynamics/compute_geopot.F90
===================================================================
--- codes/icosagcm/devel/src/dynamics/compute_geopot.F90	(revision 936)
+++ codes/icosagcm/devel/src/dynamics/compute_geopot.F90	(revision 937)
@@ -10,10 +10,126 @@
   SAVE
 
-  PUBLIC :: compute_geopot_hex, compute_geopot_manual
+#include "../unstructured/unstructured.h90"
+
+  PUBLIC :: compute_geopot_unst, compute_geopot_hex, compute_geopot_manual
 
 CONTAINS
 
+#ifdef BEGIN_DYSL
+
+#define THECELL {{ thecell }}
+
+{# ---------------- macro to generate code computing pressure top-down ---------------
+   formula = formula to compute 'gravitational' mass
+   = rhodz (dry) rhodz*theta (boussinesq) rhodz*(1+qv) (moist)         #}
+
+#define BALANCE(formula) {% call(thecell) balance() %} formula {% endcall %}
+{% macro balance() %} {% set formula=caller %}
+SEQUENCE_C1
+  PROLOGUE(llm)
+    pk(CELL) = ptop + .5*g*{{ formula('CELL') }}
+  END_BLOCK
+  BODY('llm-1,1,-1') 
+    pk(CELL) = pk(UP(CELL)) + (.5*g)*({{ formula('CELL') }}+{{ formula('UP(CELL)') }})
+  END_BLOCK
+  IF(caldyn_eta == eta_lag) THEN
+    EPILOGUE(1)
+      ps(HIDX(CELL)) = pk(CELL) + .5*g*{{ formula('CELL') }}
+    END_BLOCK
+  END IF
+END_BLOCK
+{%- endmacro %}
+
+{# ------------ macro to generate code computing geopotential bottom-up --------------
+   var = variable to be stored in pk(CELL)
+   caller() computes gv = g*v where v = specific volume
+   details depend on caldyn_thermo #}
+
+#define GEOPOT(var) {% call geopot(var) %}
+{% macro geopot(var) %} {% set formula=caller %}
+  SEQUENCE_C1
+    BODY('1,llm')
+      p_ik = pk(CELL)
+      {{ formula() }}
+      pk(CELL) = {{ var }}
+      geopot(UP(CELL)) = geopot(CELL) + gv*rhodz(CELL)
+    END_BLOCK
+  END_BLOCK
+{%- endmacro %}
+
+#define END_GEOPOT {% endcall %}
+
+KERNEL(compute_geopot)
+  SELECT CASE(caldyn_thermo)
+  CASE(thermo_boussinesq)
+    ! use hydrostatic balance with theta*rhodz to find pk (=Lagrange multiplier=pressure)
+    BALANCE( theta(THECELL,1)*rhodz(THECELL) )
+    ! now pk contains the Lagrange multiplier (pressure)
+    ! specific volume 1 = dphi/g/rhodz
+    SEQUENCE_C1
+      BODY('1,llm')
+        geopot(UP(CELL)) = geopot(CELL) + g*rhodz(CELL)
+      END_BLOCK
+    END_BLOCK
+  CASE(thermo_theta)
+    BALANCE( rhodz(THECELL) )
+    GEOPOT('exner_ik')
+      exner_ik = cpp * (p_ik/preff) ** kappa
+      gv = (g*kappa)*theta(CELL,1)*exner_ik/p_ik
+    END_GEOPOT
+  CASE(thermo_entropy)
+    BALANCE( rhodz(THECELL) )
+    GEOPOT('temp_ik')
+      temp_ik = Treff*exp((theta(CELL,1) + Rd*log(p_ik/preff))/cpp)
+      gv = (g*Rd)*temp_ik/p_ik   ! specific volume v = Rd*T/p
+    END_GEOPOT
+  CASE(thermo_variable_Cp)
+    ! thermodynamics with variable Cp
+    !      Cp.dT = dh = Tds + vdp
+    !              pv = RT
+    ! =>           ds = (dh+v.dp)/T = Cp.dT/T - R dp/p
+    ! 		Cp(T) = Cp0 * (T/T0)^nu
+    ! =>       s(p,T) = Cp(T)/nu - R log(p/preff) 
+    ! 	       	    h = Cp(T).T/(nu+1)
+    BALANCE( rhodz(THECELL) )
+    GEOPOT('temp_ik')
+      Cp_ik = nu*( theta(CELL,1) + Rd*log(p_ik/preff) )
+      temp_ik = Treff* (Cp_ik/cpp)**(1./nu)
+      gv = (g*Rd)*temp_ik/p_ik   ! specific volume v = Rd*T/p
+    END_GEOPOT
+  CASE(thermo_moist)
+    BALANCE( rhodz(THECELL)*(1.+theta(THECELL,2)) )
+    GEOPOT('temp_ik')
+      qv = theta(CELL,2) ! water vaper mixing ratio = mv/md
+      Rmix = Rd+qv*Rv
+      chi = ( theta(CELL,1) + Rmix*log(p_ik/preff) ) / (cpp + qv*cppv) ! log(T/Treff)
+      temp_ik = Treff*exp(chi)
+      ! specific volume v = R*T/p
+      ! R = (Rd + qv.Rv)/(1+qv)
+      gv = g*Rmix*temp_ik/(p_ik*(1+qv))
+    END_GEOPOT
+  END SELECT
+END_BLOCK
+
+#endif END_DYSL
+
   !**************************** Geopotential *****************************
 
+  SUBROUTINE compute_geopot_unst(rhodz,theta,ps,pk,geopot)
+        USE ISO_C_BINDING, only : C_DOUBLE, C_FLOAT
+    USE data_unstructured_mod, ONLY : enter_trace, exit_trace, &
+         id_geopot
+
+    FIELD_MASS  :: rhodz,pk   ! IN, OUT
+    FIELD_THETA :: theta      ! IN
+    FIELD_GEOPOT :: geopot    ! IN(l=1)/OUT(l>1)
+    FIELD_PS     :: ps        ! OUT
+    DECLARE_INDICES
+    NUM :: chi, gv, exner_ik, temp_ik, p_ik, qv, Rmix, Cp_ik
+    START_TRACE(id_geopot, 3,0,3)
+#include "../kernels_unst/compute_geopot.k90"
+    STOP_TRACE
+  END SUBROUTINE compute_geopot_unst
+  
   SUBROUTINE compute_geopot_hex(rhodz,theta, ps,pk,geopot) 
     REAL(rstd),INTENT(IN)    :: rhodz(iim*jjm,llm)
