From 4356a9b99fd9d1241202247d4517e659c284a39e Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Fri, 26 Jul 2019 12:10:18 +0200
Subject: [PATCH] Extended LB code generation tests, and adapted it to new
 lbmpy_walberla

---
 tests/lbm/CMakeLists.txt                      | 12 +--
 ...eField.cpp => LbCodeGenerationExample.cpp} | 28 +++----
 tests/lbm/codegen/LbCodeGenerationExample.prm | 75 +++++++++++++++++++
 tests/lbm/codegen/LbCodeGenerationExample.py  | 35 +++++++++
 tests/lbm/codegen/SrtWithForceFieldModel.py   | 24 ------
 5 files changed, 131 insertions(+), 43 deletions(-)
 rename tests/lbm/codegen/{SrtWithForceField.cpp => LbCodeGenerationExample.cpp} (84%)
 create mode 100644 tests/lbm/codegen/LbCodeGenerationExample.prm
 create mode 100644 tests/lbm/codegen/LbCodeGenerationExample.py
 delete mode 100644 tests/lbm/codegen/SrtWithForceFieldModel.py

diff --git a/tests/lbm/CMakeLists.txt b/tests/lbm/CMakeLists.txt
index dd9496fce..8f735fb94 100644
--- a/tests/lbm/CMakeLists.txt
+++ b/tests/lbm/CMakeLists.txt
@@ -71,9 +71,9 @@ waLBerla_compile_test( FILES SuViscoelasticityTest.cpp DEPENDS field blockforest
 waLBerla_execute_test( NAME  SuViscoelasticityTest COMMAND $<TARGET_FILE:SuViscoelasticityTest> ${CMAKE_CURRENT_SOURCE_DIR}/Su.prm )
 
 # Code Generation
-waLBerla_python_file_generates(codegen/SrtWithForceFieldModel.py
-        SrtWithForceFieldModel.cpp SrtWithForceFieldModel.h
-        MyNoSlip.cpp MyNoSlip.h
-        MyUBB.cpp MyUBB.h)
-waLBerla_compile_test( FILES codegen/SrtWithForceFieldModel.py
-                             codegen/SrtWithForceField.cpp )
+waLBerla_python_file_generates(codegen/LbCodeGenerationExample.py
+                              LbCodeGenerationExample_LatticeModel.cpp
+                              LbCodeGenerationExample_NoSlip.cpp
+                              LbCodeGenerationExample_UBB.cpp )
+waLBerla_compile_test( FILES codegen/LbCodeGenerationExample.py
+                             codegen/LbCodeGenerationExample.cpp)
diff --git a/tests/lbm/codegen/SrtWithForceField.cpp b/tests/lbm/codegen/LbCodeGenerationExample.cpp
similarity index 84%
rename from tests/lbm/codegen/SrtWithForceField.cpp
rename to tests/lbm/codegen/LbCodeGenerationExample.cpp
index 57e8674bf..665292f1b 100644
--- a/tests/lbm/codegen/SrtWithForceField.cpp
+++ b/tests/lbm/codegen/LbCodeGenerationExample.cpp
@@ -18,8 +18,6 @@
 //
 //======================================================================================================================
 
-#include "SrtWithForceFieldModel.h"
-
 #include "blockforest/all.h"
 #include "core/all.h"
 #include "domain_decomposition/all.h"
@@ -34,18 +32,20 @@
 #include "lbm/gui/Connection.h"
 #include "lbm/vtk/VTKOutput.h"
 
-#include "MyUBB.h"
-#include "MyNoSlip.h"
+#include "LbCodeGenerationExample_UBB.h"
+#include "LbCodeGenerationExample_NoSlip.h"
+#include "LbCodeGenerationExample_LatticeModel.h"
 
 
 using namespace walberla;
 
-typedef lbm::SrtWithForceFieldModel          LatticeModel_T;
-typedef LatticeModel_T::Stencil              Stencil_T;
-typedef LatticeModel_T::CommunicationStencil CommunicationStencil_T;
-typedef lbm::PdfField< LatticeModel_T >      PdfField_T;
+typedef lbm::LbCodeGenerationExample_LatticeModel LatticeModel_T;
+typedef LatticeModel_T::Stencil                   Stencil_T;
+typedef LatticeModel_T::CommunicationStencil      CommunicationStencil_T;
+typedef lbm::PdfField< LatticeModel_T >           PdfField_T;
 
-typedef GhostLayerField<real_t, LatticeModel_T::Stencil::D > ForceField_T;
+typedef GhostLayerField< real_t, LatticeModel_T::Stencil::D > VectorField_T;
+typedef GhostLayerField< real_t, 1 > ScalarField_T;
 
 typedef walberla::uint8_t    flag_t;
 typedef FlagField< flag_t >  FlagField_T;
@@ -68,9 +68,11 @@ int main( int argc, char ** argv )
    const double remainingTimeLoggerFrequency = parameters.getParameter< double >( "remainingTimeLoggerFrequency", 3.0 ); // in seconds
 
    // create fields
-   BlockDataID forceFieldId = field::addToStorage<ForceField_T>(blocks, "Force", real_t(0.0) );
+   BlockDataID forceFieldId = field::addToStorage<VectorField_T>( blocks, "Force", real_t( 0.0 ));
+   BlockDataID velFieldId = field::addToStorage<VectorField_T>( blocks, "Velocity", real_t( 0.0 ));
+   BlockDataID omegaFieldId = field::addToStorage<ScalarField_T>( blocks, "Omega", real_t( 0.0 ));
 
-   LatticeModel_T latticeModel = LatticeModel_T( forceFieldId, omega );
+   LatticeModel_T latticeModel = LatticeModel_T( forceFieldId, omegaFieldId, velFieldId, omega );
    BlockDataID pdfFieldId = lbm::addPdfFieldToStorage( blocks, "pdf field", latticeModel, initialVelocity, real_t(1) );
    BlockDataID flagFieldId = field::addFlagFieldToStorage< FlagField_T >( blocks, "flag field" );
 
@@ -80,8 +82,8 @@ int main( int argc, char ** argv )
 
    auto boundariesConfig = walberlaEnv.config()->getOneBlock( "Boundaries" );
 
-   lbm::MyUBB ubb(blocks, pdfFieldId);
-   lbm::MyNoSlip noSlip(blocks, pdfFieldId);
+   lbm::LbCodeGenerationExample_UBB ubb(blocks, pdfFieldId);
+   lbm::LbCodeGenerationExample_NoSlip noSlip(blocks, pdfFieldId);
 
    geometry::initBoundaryHandling<FlagField_T>(*blocks, flagFieldId, boundariesConfig);
    geometry::setNonBoundaryCellsToDomain<FlagField_T>(*blocks, flagFieldId, fluidFlagUID);
diff --git a/tests/lbm/codegen/LbCodeGenerationExample.prm b/tests/lbm/codegen/LbCodeGenerationExample.prm
new file mode 100644
index 000000000..f233188ac
--- /dev/null
+++ b/tests/lbm/codegen/LbCodeGenerationExample.prm
@@ -0,0 +1,75 @@
+
+Parameters 
+{
+	omega           1.8;
+	timesteps       500;
+
+	useGui 0;
+	remainingTimeLoggerFrequency 3; // in seconds
+}
+
+DomainSetup
+{
+   blocks        <  1,    1, 1 >;
+   cellsPerBlock <  30, 30, 10 >;
+   periodic      <  0,    0, 1 >;  
+}
+
+StabilityChecker
+{
+   checkFrequency 100;
+   streamOutput   false;
+   vtkOutput      true;
+}
+
+Boundaries 
+{
+    /*
+       possible sub-blocks: documentation for the subblocks can be found in src/geometry/initializers/BoundaryFrom*.h
+       	   - CellInterval	 BoundaryFromCellInterval.h
+       	   - Border          BoundaryFromDomainBorder.h
+       	   - VoxelFile	     BoundaryFromVoxelFile.h
+       	   - Body			 BoundaryFromBody.h
+       	   - GrayScaleImage  BoundaryFromImage.h
+       	   - RGBAImage	     BoundaryFromImage.h
+    */
+    
+	Border { direction W;    walldistance -1;  flag NoSlip; }
+	Border { direction E;    walldistance -1;  flag NoSlip; }
+    Border { direction S;    walldistance -1;  flag NoSlip; }
+    Border { direction N;    walldistance -1;  flag UBB; }
+}
+
+
+VTK 
+{
+   fluid_field
+   {
+      writeFrequency 100;
+      ghostLayers    1;
+      
+      before_functions {
+         PDFGhostLayerSync;
+      }
+      
+      inclusion_filters {
+         DomainFilter;
+      }
+      
+      writers {
+         Velocity;
+         Density;
+      }
+   }
+   
+   flag_field
+   {
+      writeFrequency 10000000; // write only once
+      ghostLayers    1;
+      
+      writers {
+         FlagField;
+      }
+   }
+
+}
diff --git a/tests/lbm/codegen/LbCodeGenerationExample.py b/tests/lbm/codegen/LbCodeGenerationExample.py
new file mode 100644
index 000000000..55c93853a
--- /dev/null
+++ b/tests/lbm/codegen/LbCodeGenerationExample.py
@@ -0,0 +1,35 @@
+import sympy as sp
+import pystencils as ps
+from lbmpy.creationfunctions import create_lb_collision_rule
+from lbmpy.boundaries import NoSlip, UBB
+from pystencils_walberla import CodeGeneration
+from lbmpy_walberla import RefinementScaling, generate_boundary, generate_lattice_model
+
+with CodeGeneration() as ctx:
+    omega, omega_free = sp.symbols("omega, omega_free")
+    force_field, vel_field, omega_out = ps.fields("force(3), velocity(3), omega_out: [3D]", layout='fzyx')
+
+    # the collision rule of the LB method where the some advanced features
+    collision_rule = create_lb_collision_rule(
+        stencil='D3Q19', compressible=True,
+        method='mrt3', relaxation_rates=[omega, omega, omega_free],
+        entropic=True,                    # entropic method where second omega is chosen s.t. entropy condition
+        omega_output_field=omega_out,     # scalar field where automatically chosen omega of entropic or Smagorinsky method is written to
+        force=force_field.center_vector,  # read forces for each lattice cell from an external force field
+                                          # that is initialized and changed in C++ app
+        output={'velocity': vel_field},   # write macroscopic velocity to field in every time step
+                                          # useful for coupling multiple LB methods, e.g. hydrodynamic to advection/diffusion LBM
+        optimization={'cse_global': True}
+    )
+
+    # the refinement scaling object describes how certain parameters are scaled across grid scales
+    # there are two default scaling behaviors available for relaxation rates and forces:
+    scaling = RefinementScaling()
+    scaling.add_standard_relaxation_rate_scaling(omega)
+    scaling.add_force_scaling(force_field)
+
+    # generate lattice model and (optionally) boundary conditions
+    # for CPU simulations waLBerla's internal boundary handling can be used as well
+    generate_lattice_model(ctx, 'LbCodeGenerationExample_LatticeModel', collision_rule, refinement_scaling=scaling)
+    generate_boundary(ctx, 'LbCodeGenerationExample_UBB', UBB([0.05, 0, 0]), collision_rule.method)
+    generate_boundary(ctx, 'LbCodeGenerationExample_NoSlip', NoSlip(), collision_rule.method)
diff --git a/tests/lbm/codegen/SrtWithForceFieldModel.py b/tests/lbm/codegen/SrtWithForceFieldModel.py
deleted file mode 100644
index f68ec173f..000000000
--- a/tests/lbm/codegen/SrtWithForceFieldModel.py
+++ /dev/null
@@ -1,24 +0,0 @@
-import sympy as sp
-import pystencils as ps
-from lbmpy.creationfunctions import create_lb_method
-from lbmpy.boundaries import NoSlip, UBB
-from pystencils_walberla import CodeGeneration
-from lbmpy_walberla import generate_lattice_model, RefinementScaling, generate_boundary
-
-
-with CodeGeneration() as ctx:
-    omega = sp.Symbol("omega")
-    force_field = ps.fields("force(3): [3D]", layout='fzyx')
-
-    # lattice Boltzmann method
-    lb_method = create_lb_method(stencil='D3Q19', method='srt', relaxation_rates=[omega],
-                                 force_model='guo', force=force_field.center_vector)
-
-    scaling = RefinementScaling()
-    scaling.add_standard_relaxation_rate_scaling(omega)
-    scaling.add_force_scaling(force_field)
-
-    # generate components
-    generate_lattice_model(ctx, 'SrtWithForceFieldModel', lb_method, refinement_scaling=scaling)
-    generate_boundary(ctx, 'MyUBB', UBB([0.05, 0, 0]), lb_method)
-    generate_boundary(ctx, 'MyNoSlip', NoSlip(), lb_method)
-- 
GitLab