Index: codes/icosagcm/devel/Python/src/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/macros.jin	(revision 649)
+++ codes/icosagcm/devel/Python/src/macros.jin	(revision 650)
@@ -10,5 +10,5 @@
  
 {% macro define(key,val) %} 
-{{ flat }}undef {{ key }} 
+{{ flat }}undef {{ key }}
 {{ flat }}define {{ key }} {{ val }} 
 {%- endmacro %}
@@ -23,4 +23,15 @@
 {{ flat }}define {{ key }} {{ key }}_UNDEFINED_HERE
 {%- endmacro %}
+
+{% macro cdef(code, key,val) %} 
+{{ define(key,val) if key in code }}
+{%- endmacro %}
+
+{% macro cundef(code, keys) %} 
+{% for key in keys %}
+{{ undef(key) if key in code }}
+{%- endfor %}
+{%- endmacro %}
+
 
 {% macro dummy() -%}
Index: codes/icosagcm/devel/Python/src/unstructured/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 649)
+++ codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 650)
@@ -86,23 +86,31 @@
 {%- endmacro %}
 
+{# ---------------------- PRELOAD STENCIL INDICES ---------------------- #}
+
+{% macro get_edges(thecode, mesh,degree) %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'edge%d = %s_edge(%d,ij)'%(iedge,mesh,iedge) if 'EDGE' in thecode }} 
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'le_de%d = le_de(edge%d)'%(iedge,iedge) if 'LE_DE' in thecode }}      
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'sign%d = %s_ne(%d,ij)'%(iedge,mesh,iedge) if 'SIGN' in thecode }}   
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'ij_up%d = up(edge%d)'%(iedge,iedge) if 'VERTEX1' in thecode }}
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'ij_down%d = down(edge%d)'%(iedge,iedge) if 'VERTEX2' in thecode }}
+{% endfor %}
+{% endmacro %}
+
+{% macro get_vertices(thecode, mesh,degree) %}
+{% for ivertex in range(1,degree+1) %} 
+{{ 'vertex%d = %s_vertex(%d,ij)'%(ivertex,mesh,ivertex) if 'VERTEX' in thecode }} 
+{% endfor %}
+{% endmacro %}
+
 {# ------------------------------ MESHES --------------------------- #}
-
-{% macro get_edges(thecode, mesh,degree) %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'edge%d = %s_edge(%d,ij)'%(iedge,mesh,iedge) }} 
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'le_de%d = le_de(edge%d)'%(iedge,iedge) }}      
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'sign%d = %s_ne(%d,ij)'%(iedge,mesh,iedge) }}   
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'ij_up%d = up(edge%d)'%(iedge,iedge) if 'VERTEX1' in thecode }}
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'ij_down%d = down(edge%d)'%(iedge,iedge) if 'VERTEX2' in thecode }}
-{% endfor %}
-{% endmacro %}
 
 {# argument 'code' is the body enclosed by ON_XXX ... END_BLOCK ; it takes arguments : mesh,degree=0 #}
@@ -148,6 +156,10 @@
 SELECT CASE(dual_deg(ij))
 CASE(3)
+{{ get_edges(thecode, mesh, 3) }}
+{{ get_vertices(thecode,mesh,3) }}
 {{ vloop(mesh, lrange, flags, code, 3) }}
 CASE(4)
+{{ get_edges(thecode, mesh, 4) }}
+{{ get_vertices(thecode,mesh,4) }}
 {{ vloop(mesh, lrange, flags, code, 4) }}
 CASE DEFAULT
@@ -243,10 +255,10 @@
 {% set thecode = caller(mesh) %}
 {{ define('EDGE', 'l,edge') }}
-{{ define('LE_DE', 'le_de(edge)') }}
-{{ define('SIGN', '1.') }}
-{{ define('CELL1', 'l,ij_left') }}
-{{ define('CELL2',  'l,ij_right') }}
-{{ define('VERTEX1', 'l,ij_down') }}
-{{ define('VERTEX2', 'l,ij_up') }}
+{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
+{{ cdef(thecode, 'SIGN', '1.') }}
+{{ cdef(thecode, 'CELL1', 'l,ij_left') }}
+{{ cdef(thecode, 'CELL2',  'l,ij_right') }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_down') }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_up') }}
 !$OMP DO SCHEDULE(STATIC)
 DO edge = 1, edge_num
@@ -258,11 +270,5 @@
 END DO
 !$OMP END DO
-{{ undef('EDGE') }}
-{{ undef('LE_DE') }}
-{{ undef('SIGN') }}
-{{ undef('CELL1') }}
-{{ undef('CELL2') }}
-{{ undef('VERTEX1') }}
-{{ undef('VERTEX2') }}
+{{ cundef(thecode, ('EDGE','LE_DE','SIGN','CELL1','CELL2','VERTEX1','VERTEX2') ) }}
 {%- endmacro %}
 
@@ -272,17 +278,17 @@
 {% if degree>1 %}
 {% for iedge in range(1,degree+1) %}
-{{ define('EDGE', 'l,edge%d'%iedge) }}
-{{ define('SIGN', 'sign%d'%iedge) }}
-{{ define('LE_DE', 'le_de%d'%iedge) }}
-{{ define('VERTEX1', 'l,ij_up%d'%iedge) }}
-{{ define('VERTEX2', 'l,ij_down%d'%iedge) }}
+{{ cdef(thecode, 'EDGE', 'l,edge%d'%iedge) }}
+{{ cdef(thecode, 'SIGN', 'sign%d'%iedge) }}
+{{ cdef(thecode, 'LE_DE', 'le_de%d'%iedge) }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_up%d'%iedge) }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_down%d'%iedge) }}
   {{ thecode }}
 {% endfor %}
 {% else %}
-{{ define('EDGE', 'l,edge') }}
-{{ define('SIGN', mesh + '_ne(iedge,ij)') }}
-{{ define('LE_DE', 'le_de(edge)') }}
-{{ define('VERTEX1', 'l,ij_up') }}
-{{ define('VERTEX2', 'l,ij_down') }}
+{{ cdef(thecode, 'EDGE', 'l,edge') }}
+{{ cdef(thecode, 'SIGN', mesh + '_ne(iedge,ij)') }}
+{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_up') }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_down') }}
 DO iedge = 1, {{ mesh }}_deg(ij)
   edge = {{ mesh }}_edge(iedge,ij)
@@ -292,10 +298,5 @@
 END DO
 {% endif %}
-
-{{ undef('EDGE') }}
-{{ undef('SIGN') }}
-{{ undef('LE_DE') }}
-{{ undef('VERTEX1') }}
-{{ undef('VERTEX2') }}
+{{ cundef(thecode, ('EDGE', 'SIGN', 'LE_DE', 'VERTEX1', 'VERTEX2') ) }}
 {%- endmacro %}
 
@@ -320,12 +321,13 @@
 {% macro forall_vertices(mesh,degree) -%}
 {% set thecode = caller() %}
-{{ define('VERTEX', 'l,vertex') }}
 {% if degree>1 %}
 {% for ivertex in range(1,degree+1) %}
-  ivertex = {{ ivertex }}
-  vertex = {{ mesh }}_vertex({{ ivertex }},ij) 
+{{ define('RIV2', 'Riv2(%d,ij)'%ivertex ) }}
+{{ define('VERTEX', 'l,vertex%d'%ivertex ) }}
   {{ thecode }}
 {% endfor %}
 {% else %}
+{{ define('RIV2', 'Riv2(ivertex,ij)') }}
+{{ define('VERTEX', 'l,vertex') }}
 DO ivertex = 1, {{ mesh }}_deg(ij)
   vertex = {{ mesh }}_vertex(ivertex,ij)
@@ -334,4 +336,5 @@
 {% endif %}
 {{ undef('VERTEX') }}
+{{ undef('RIV2') }}
 {%- endmacro %}
 
@@ -357,5 +360,4 @@
 #define _FALSE_ (0==1)
 
-#define RIV2 Riv2(ivertex,ij)
 #define AI Ai(ij)
 #define AV Av(ij)
Index: codes/icosagcm/devel/src/unstructured/caldyn_unstructured.F90
===================================================================
--- codes/icosagcm/devel/src/unstructured/caldyn_unstructured.F90	(revision 649)
+++ codes/icosagcm/devel/src/unstructured/caldyn_unstructured.F90	(revision 650)
@@ -8,7 +8,16 @@
 CONTAINS
 
+#define DBL REAL(C_DOUBLE)
+
 #define INDICES1 ij,l,iq,iedge,edge,ivertex,vertex,ij_left,ij_right
 #define INDICES2 ij_up,ij_down,itrisk,edge_trisk,kup,kdown
+#define EDGES edge1,edge2,edge3,edge4,edge5,edge6
+#define VERTICES vertex1,vertex2,vertex3,vertex4,vertex5,vertex6
+#define SIGNS sign1,sign2,sign3,sign4,sign5,sign6
+#define EDGE_ENDS ij_up1,ij_up2,ij_up3,ij_up4,ij_up5,ij_up6,ij_down1,ij_down2,ij_down3,ij_down4,ij_down5,ij_down6
+#define LENGTHS le_de1,le_de2,le_de3,le_de4,le_de5,le_de6
 #define DECLARE_INDICES INTEGER INDICES1,INDICES2
+#define DECLARE_EDGES DBL SIGNS,LENGTHS ; INTEGER EDGES, EDGE_ENDS
+#define DECLARE_VERTICES INTEGER VERTICES
 #define PHI_BOT(ij) Phi_bot
 #define PHI_BOT_VAR 0.
@@ -17,5 +26,4 @@
 #define BINDC(thename) BINDC_(dynamico_ ## thename)
 
-#define DBL REAL(C_DOUBLE)
 #define DOUBLE1(m) DBL, DIMENSION(m)
 #define DOUBLE2(m,n) DBL, DIMENSION(m,n)
@@ -49,4 +57,5 @@
   FIELD_UL     :: DePhil, v_el, G_el, F_el ! LOCAL
   DECLARE_INDICES
+  DECLARE_EDGES
   DBL :: W_el, W2_el, gPhi2, dP, divG, u2, uu
 #include "../kernels_unst/caldyn_slow_NH.k90"
@@ -90,4 +99,5 @@
   FIELD_U     :: u,hflux,du ! IN, OUT, OUT
   DECLARE_INDICES
+  DECLARE_EDGES
   LOGICAL, PARAMETER :: zero=.TRUE.
   DBL :: ke, uu
@@ -118,4 +128,5 @@
   FIELD_THETA :: theta, dtheta_rhodz
   DECLARE_INDICES
+  DECLARE_EDGES
   DBL :: divF, du_trisk
 #include "../kernels_unst/coriolis.k90"
@@ -136,4 +147,6 @@
   FIELD_Z    :: qv
   DECLARE_INDICES
+  DECLARE_EDGES
+  DECLARE_VERTICES
   DBL :: etav, hv
 #include "../kernels_unst/pvort_only.k90"
@@ -147,4 +160,5 @@
   FIELD_U      :: u,du      ! INOUT,INOUT
   DECLARE_INDICES
+  DECLARE_EDGES
   DBL          :: due
 
@@ -166,4 +180,5 @@
   DOUBLE2(llm,edge_num)  :: u
   DECLARE_INDICES
+  DECLARE_EDGES
   DBL :: div_ij
   !$OMP PARALLEL NUM_THREADS(nb_threads)
