From 2c3ca96854647ab49aa5cbc46b382117f06be242 Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Wed, 15 May 2019 14:49:26 +0200
Subject: [PATCH] Fix: compressible models are now generated correctly

---
 lbmpy_walberla/templates/LatticeModel.tmpl.h  | 20 +++++++++----------
 lbmpy_walberla/walberla_lbm_generation.py     |  2 +-
 lbmpy_walberla_tests/test_walberla_codegen.py | 15 ++++++++++++--
 3 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/lbmpy_walberla/templates/LatticeModel.tmpl.h b/lbmpy_walberla/templates/LatticeModel.tmpl.h
index 5ae95c6..e3246f3 100644
--- a/lbmpy_walberla/templates/LatticeModel.tmpl.h
+++ b/lbmpy_walberla/templates/LatticeModel.tmpl.h
@@ -100,7 +100,7 @@ public:
     static const real_t w[{{Q}}];
     static const real_t wInv[{{Q}}];
 
-    static const bool compressible = {{compressible}};
+    static const bool compressible = {% if compressible %}true{% else %}false{% endif %};
     static const int equilibriumAccuracyOrder = {{equilibrium_order}};
 
     class Sweep
@@ -211,7 +211,7 @@ public:
                       const Vector3< real_t > & u = Vector3< real_t >( real_t(0.0) ),
                       real_t rho = real_t(1.0) )
    {
-        {% if compressible == 'false' %}
+        {% if not compressible %}
         rho -= real_t(1.0);
         {% endif %}
         {{equilibrium_from_direction}}
@@ -221,7 +221,7 @@ public:
                                    const Vector3<real_t> & u = Vector3< real_t >(real_t(0.0)),
                                    real_t rho = real_t(1.0) )
    {
-        {% if compressible == 'false' %}
+        {% if not compressible %}
         rho -= real_t(1.0);
         {% endif %}
         {{symmetric_equilibrium_from_direction}}
@@ -231,7 +231,7 @@ public:
                                     const Vector3< real_t > & u = Vector3<real_t>( real_t(0.0) ),
                                     real_t rho = real_t(1.0) )
    {
-        {% if compressible == 'false' %}
+        {% if not compressible %}
         rho -= real_t(1.0);
         {% endif %}
         {{asymmetric_equilibrium_from_direction}}
@@ -240,7 +240,7 @@ public:
    static std::vector< real_t > get( const Vector3< real_t > & u = Vector3<real_t>( real_t(0.0) ),
                                      real_t rho = real_t(1.0) )
    {
-      {% if compressible == 'false' %}
+      {% if not compressible %}
       rho -= real_t(1.0);
       {% endif %}
 
@@ -296,7 +296,7 @@ struct Equilibrium< {{class_name}}, void >
    static void set( FieldPtrOrIterator & it,
                     const Vector3< real_t > & u = Vector3< real_t >( real_t(0.0) ), real_t rho = real_t(1.0) )
    {
-        {%if compressible == 'false' %}
+        {%if not compressible %}
         rho -= real_t(1.0);
         {%endif %}
 
@@ -309,7 +309,7 @@ struct Equilibrium< {{class_name}}, void >
    static void set( PdfField_T & pdf, const cell_idx_t x, const cell_idx_t y, const cell_idx_t z,
                     const Vector3< real_t > & u = Vector3< real_t >( real_t(0.0) ), real_t rho = real_t(1.0) )
    {
-      {%if compressible == 'false' %}
+      {%if not compressible %}
       rho -= real_t(1.0);
       {%endif %}
 
@@ -364,7 +364,7 @@ struct DensityAndVelocity<{{class_name}}>
         const real_t u_2(0.0);
         {% endif %}
 
-        Equilibrium<{{class_name}}>::set(it, Vector3<real_t>(u_0, u_1, u_2), rho{%if compressible %} + real_t(1) {%endif%});
+        Equilibrium<{{class_name}}>::set(it, Vector3<real_t>(u_0, u_1, u_2), rho{%if not compressible %} + real_t(1) {%endif%});
     }
 
     template< typename PdfField_T >
@@ -376,7 +376,7 @@ struct DensityAndVelocity<{{class_name}}>
         const real_t u_2(0.0);
         {% endif %}
 
-        Equilibrium<{{class_name}}>::set(pdf, x, y, z, Vector3<real_t>(u_0, u_1, u_2), rho {%if compressible %} + real_t(1) {%endif%});
+        Equilibrium<{{class_name}}>::set(pdf, x, y, z, Vector3<real_t>(u_0, u_1, u_2), rho {%if not compressible %} + real_t(1) {%endif%});
     }
 };
 
@@ -398,7 +398,7 @@ struct DensityAndVelocityRange<{{class_name}}, FieldIteratorXYZ>
             const real_t u_2(0.0);
             {% endif %}
 
-            Equilibrium<{{class_name}}>::set(cellIt, Vector3<real_t>(u_0, u_1, u_2), rho{%if compressible %} + real_t(1) {%endif%});
+            Equilibrium<{{class_name}}>::set(cellIt, Vector3<real_t>(u_0, u_1, u_2), rho{%if not compressible %} + real_t(1) {%endif%});
         }
    }
 };
diff --git a/lbmpy_walberla/walberla_lbm_generation.py b/lbmpy_walberla/walberla_lbm_generation.py
index 90e0550..aa4b14c 100644
--- a/lbmpy_walberla/walberla_lbm_generation.py
+++ b/lbmpy_walberla/walberla_lbm_generation.py
@@ -100,7 +100,7 @@ def generate_lattice_model(generation_context, class_name, lb_method, refinement
         'stencil_name': stencil_name,
         'D': lb_method.dim,
         'Q': len(lb_method.stencil),
-        'compressible': 'true' if params['compressible'] else 'false',
+        'compressible': lb_method.conserved_quantity_computation.compressible,
         'weights': ",".join(str(w.evalf()) + constant_suffix for w in lb_method.weights),
         'inverse_weights': ",".join(str((1/w).evalf()) + constant_suffix for w in lb_method.weights),
 
diff --git a/lbmpy_walberla_tests/test_walberla_codegen.py b/lbmpy_walberla_tests/test_walberla_codegen.py
index 2f4ff68..a43b869 100644
--- a/lbmpy_walberla_tests/test_walberla_codegen.py
+++ b/lbmpy_walberla_tests/test_walberla_codegen.py
@@ -17,16 +17,18 @@ class WalberlaLbmpyCodegenTest(unittest.TestCase):
             force_field = ps.fields("force(3): [3D]", layout='fzyx')
             omega = sp.Symbol("omega")
 
-            lb_method = create_lb_method(stencil='D3Q19', method='srt', relaxation_rates=[omega],
+            lb_method = create_lb_method(stencil='D3Q19', method='srt', relaxation_rates=[omega], compressible=True,
                                          force_model='guo', force=force_field.center_vector)
 
             scaling = RefinementScaling()
             scaling.add_standard_relaxation_rate_scaling(omega)
             scaling.add_force_scaling(force_field)
 
-            generate_lattice_model(ctx, 'SrtWithForceFieldModel', lb_method, refinement_scaling=scaling)
+            generate_lattice_model(ctx, 'SrtWithForceFieldModel', lb_method, refinement_scaling=scaling,
+                                   update_rule_params={'compressible': True})
             generate_boundary(ctx, 'MyUBB', UBB([0.05, 0, 0]), lb_method)
             generate_boundary(ctx, 'MyNoSlip', NoSlip(), lb_method)
+            assert 'static const bool compressible = true;' in ctx.files['SrtWithForceFieldModel.h']
 
     @staticmethod
     def test_sparse():
@@ -48,3 +50,12 @@ class WalberlaLbmpyCodegenTest(unittest.TestCase):
 
             lb_assignments = create_lb_update_rule(stencil='D3Q19', method='srt').main_assignments
             generate_pack_info_from_kernel(ctx, 'MyPackInfo2', lb_assignments)
+
+    @staticmethod
+    def test_incompressible():
+        with ManualCodeGenerationContext() as ctx:
+            omega = sp.Symbol("omega")
+
+            lb_method = create_lb_method(stencil='D3Q19', method='srt', relaxation_rates=[omega], compressible=False)
+            generate_lattice_model(ctx, 'Model', lb_method, update_rule_params={'compressible': False})
+            assert 'static const bool compressible = false;' in ctx.files['Model.h']
-- 
GitLab