test_fluctuating_lb.py 11.4 KB
 RudolfWeeber committed Oct 12, 2020 1 ``````"""Tests velocity and stress fluctuations for thermalized LB""" `````` RudolfWeeber committed Nov 20, 2019 2 3 4 `````` import pystencils as ps `````` RudolfWeeber committed Oct 12, 2020 5 6 ``````from lbmpy.creationfunctions import * from lbmpy.macroscopic_value_kernels import macroscopic_values_setter `````` RudolfWeeber committed Nov 20, 2019 7 ``````import numpy as np `````` RudolfWeeber committed Oct 12, 2020 8 ``````from lbmpy.moments import is_bulk_moment, is_shear_moment, get_order `````` Michael Kuron committed May 27, 2021 9 10 11 12 13 ``````from pystencils.rng import PhiloxTwoDoubles import pytest from pystencils.backends.simd_instruction_sets import get_supported_instruction_sets, get_vector_instruction_set from pystencils.cpu.cpujit import get_compiler_config `````` RudolfWeeber committed Nov 20, 2019 14 15 `````` `````` RudolfWeeber committed Oct 12, 2020 16 ``````def single_component_maxwell(x1, x2, kT, mass): `````` RudolfWeeber committed Nov 20, 2019 17 18 `````` """Integrate the probability density from x1 to x2 using the trapezoidal rule""" x = np.linspace(x1, x2, 1000) `````` RudolfWeeber committed Oct 12, 2020 19 20 21 22 `````` return np.trapz(np.exp(-mass * x**2 / (2. * kT)), x) / np.sqrt(2. * np.pi * kT/mass) def rr_getter(moment_group): `````` RudolfWeeber committed Oct 13, 2020 23 24 25 `````` """Maps a group of moments to a relaxation rate (shear, bulk, even, odd) in the 4 relaxation time thermalized LB model """ `````` RudolfWeeber committed Oct 12, 2020 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 `````` is_shear = [is_shear_moment(m, 3) for m in moment_group] is_bulk = [is_bulk_moment(m, 3) for m in moment_group] order = [get_order(m) for m in moment_group] assert min(order) == max(order) order = order[0] if order < 2: return 0 elif any(is_bulk): assert all(is_bulk) return sp.Symbol("omega_bulk") elif any(is_shear): assert all(is_shear) return sp.Symbol("omega_shear") elif order % 2 == 0: assert order > 2 return sp.Symbol("omega_even") else: return sp.Symbol("omega_odd") def second_order_moment_tensor_assignments(function_values, stencil, output_field): """Assignments for calculating the pressure tensor""" assert len(function_values) == len(stencil) dim = len(stencil[0]) return [ps.Assignment(output_field(i, j), sum(c[i] * c[j] * f for f, c in zip(function_values, stencil))) for i in range(dim) for j in range(dim)] def add_pressure_output_to_collision_rule(collision_rule, pressure_field): pressure_ouput = second_order_moment_tensor_assignments(collision_rule.method.pre_collision_pdf_symbols, collision_rule.method.stencil, pressure_field) collision_rule.main_assignments = collision_rule.main_assignments + pressure_ouput `````` RudolfWeeber committed Oct 13, 2020 62 ``````def get_fluctuating_lb(size=None, kT=None, omega_shear=None, omega_bulk=None, omega_odd=None, omega_even=None, rho_0=None, target=None): `````` RudolfWeeber committed Oct 12, 2020 63 64 65 66 67 `````` # Parameters stencil = get_stencil('D3Q19') # Setup data handling `````` RudolfWeeber committed Oct 13, 2020 68 69 `````` dh = ps.create_data_handling( [size]*3, periodicity=True, default_target=target) `````` RudolfWeeber committed Oct 12, 2020 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 `````` src = dh.add_array('src', values_per_cell=len(stencil), layout='f') dst = dh.add_array_like('dst', 'src') rho = dh.add_array('rho', layout='f', latex_name='\\rho') u = dh.add_array('u', values_per_cell=dh.dim, layout='f') pressure_field = dh.add_array('pressure', values_per_cell=( 3, 3), layout='f', gpu=target == 'gpu') force_field = dh.add_array( 'force', values_per_cell=3, layout='f', gpu=target == 'gpu') # Method setup method = create_mrt_orthogonal( stencil=get_stencil('D3Q19'), compressible=True, weighted=True, relaxation_rate_getter=rr_getter, `````` Frederik Hennig committed Aug 11, 2021 85 `````` force_model=force_model_from_string('guo', force_field.center_vector)) `````` RudolfWeeber committed Oct 12, 2020 86 87 88 89 90 91 `````` collision_rule = create_lb_collision_rule( method, fluctuating={ 'temperature': kT }, optimization={'cse_global': True} `````` RudolfWeeber committed Nov 20, 2019 92 93 `````` ) `````` RudolfWeeber committed Oct 12, 2020 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 `````` add_pressure_output_to_collision_rule(collision_rule, pressure_field) collision = create_lb_update_rule(collision_rule=collision_rule, stencil=stencil, compressible=True, kernel_type='collide_only', optimization={'symbolic_field': src}) stream = create_stream_pull_with_output_kernel(collision.method, src, dst, {'density': rho, 'velocity': u}) opts = {'cpu_openmp': True, 'cpu_vectorize_info': None, 'target': dh.default_target} # Compile kernels stream_kernel = ps.create_kernel(stream, **opts).compile() collision_kernel = ps.create_kernel(collision, **opts).compile() sync_pdfs = dh.synchronization_function([src.name]) # Initialization init = macroscopic_values_setter(collision.method, velocity=(0,)*dh.dim, pdfs=src.center_vector, density=rho.center) init_kernel = ps.create_kernel(init, ghost_layers=0).compile() dh.fill(rho.name, rho_0) dh.fill(u.name, np.nan, ghost_layers=True, inner_ghost_layers=True) dh.fill(u.name, 0) `````` RudolfWeeber committed Oct 19, 2020 122 123 124 `````` dh.fill(force_field.name, np.nan, ghost_layers=True, inner_ghost_layers=True) dh.fill(force_field.name, 0) `````` RudolfWeeber committed Oct 12, 2020 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 `````` dh.run_kernel(init_kernel) # time loop def time_loop(start, steps): dh.all_to_gpu() for i in range(start, start+steps): dh.run_kernel(collision_kernel, omega_shear=omega_shear, omega_bulk=omega_bulk, omega_odd=omega_odd, omega_even=omega_even, seed=42, time_step=i) sync_pdfs() dh.run_kernel(stream_kernel) dh.swap(src.name, dst.name) return start+steps `````` RudolfWeeber committed Oct 13, 2020 140 141 142 `````` return dh, time_loop `````` RudolfWeeber committed Oct 19, 2020 143 ``````def test_resting_fluid(target="cpu"): `````` RudolfWeeber committed Oct 13, 2020 144 145 146 `````` rho_0 = 0.86 kT = 4E-4 L = [60]*3 `````` RudolfWeeber committed Oct 19, 2020 147 `````` dh, time_loop = get_fluctuating_lb(size=L[0], target=target, `````` RudolfWeeber committed Oct 13, 2020 148 149 150 `````` rho_0=rho_0, kT=kT, omega_shear=0.8, omega_bulk=0.5, omega_even=.04, omega_odd=0.3) `````` RudolfWeeber committed Oct 12, 2020 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 `````` # Test t = 0 # warm up t = time_loop(t, 10) # Measurement for i in range(10): t = time_loop(t, 5) res_u = dh.gather_array("u").reshape((-1, 3)) res_rho = dh.gather_array("rho").reshape((-1,)) # mass conservation np.testing.assert_allclose(np.mean(res_rho), rho_0, atol=3E-12) # momentum conservation momentum = np.dot(res_rho, res_u) np.testing.assert_allclose(momentum, [0, 0, 0], atol=1E-10) # temperature kinetic_energy = 1/2*np.dot(res_rho, res_u*res_u)/np.product(L) np.testing.assert_allclose( kinetic_energy, [kT/2]*3, atol=kT*0.01) # velocity distribution v_hist, v_bins = np.histogram( res_u, bins=11, range=(-.075, .075), density=True) # Calculate expected values from single v_expected = [] for j in range(len(v_hist)): # Maxwell distribution res = 1./(v_bins[j+1]-v_bins[j]) * \ single_component_maxwell( v_bins[j], v_bins[j+1], kT, rho_0) v_expected.append(res) v_expected = np.array(v_expected) # 10% accuracy on the entire histogram np.testing.assert_allclose(v_hist, v_expected, rtol=0.1) # 1% accuracy on the middle part remove = 3 np.testing.assert_allclose( v_hist[remove:-remove], v_expected[remove:-remove], rtol=0.01) # pressure tensor against expressions from # Duenweg, Schiller, Ladd, https://arxiv.org/abs/0707.1581 res_pressure = dh.gather_array("pressure").reshape((-1, 3, 3)) c_s = np.sqrt(1/3) # speed of sound # average of pressure tensor # Diagonal elements are rho c_s^22 +. When the fluid is # thermalized, the expectation value of = kT due to the # equi-partition theorem. p_av_expected = np.diag([rho_0*c_s**2 + kT]*3) np.testing.assert_allclose( np.mean(res_pressure, axis=0), p_av_expected, atol=c_s**2/2000) `````` RudolfWeeber committed Oct 19, 2020 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 `````` def test_point_force(target="cpu"): """Test momentum balance for thermalized fluid with applied poitn forces""" rho_0 = 0.86 kT = 4E-4 L = [8]*3 dh, time_loop = get_fluctuating_lb(size=L[0], target=target, rho_0=rho_0, kT=kT, omega_shear=0.8, omega_bulk=0.5, omega_even=.04, omega_odd=0.3) # Test t = 0 # warm up t = time_loop(t, 100) introduced_momentum = np.zeros(3) for i in range(100): point_force = 1E-5*(np.random.random(3) - .5) introduced_momentum += point_force # Note that ghost layers are included in the indexing force_pos = np.random.randint(1, L[0]-2, size=3) dh.cpu_arrays["force"][force_pos[0], force_pos[1], force_pos[2]] = point_force t = time_loop(t, 1) res_u = dh.gather_array("u").reshape((-1, 3)) res_rho = dh.gather_array("rho").reshape((-1,)) # mass conservation np.testing.assert_allclose(np.mean(res_rho), rho_0, atol=3E-12) # momentum conservation momentum = np.dot(res_rho, res_u) np.testing.assert_allclose( momentum, introduced_momentum + 0.5 * point_force, atol=1E-10) dh.cpu_arrays["force"][force_pos[0], force_pos[1], force_pos[2]] = np.zeros(3) `````` Michael Kuron committed May 27, 2021 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 `````` @pytest.mark.skipif(not get_supported_instruction_sets(), reason="No vector instruction sets supported") @pytest.mark.parametrize('assume_aligned', (True, False)) @pytest.mark.parametrize('assume_inner_stride_one', (True, False)) @pytest.mark.parametrize('assume_sufficient_line_padding', (True, False)) def test_vectorization(assume_aligned, assume_inner_stride_one, assume_sufficient_line_padding): method = create_mrt_orthogonal( stencil=get_stencil('D2Q9'), compressible=True, weighted=True, relaxation_rate_getter=rr_getter) collision_rule = create_lb_collision_rule( method, fluctuating={ 'temperature': sp.Symbol("kT"), 'rng_node': PhiloxTwoDoubles, 'block_offsets': (0, 0), }, optimization={'cse_global': True} ) collision = create_lb_update_rule(collision_rule=collision_rule, stencil=method.stencil, compressible=True, kernel_type='collide_only') instruction_sets = get_supported_instruction_sets() if get_compiler_config()['os'] == 'windows': # skip instruction sets supported by the CPU but not by the compiler if 'avx' in instruction_sets and ('/arch:avx2' not in get_compiler_config()['flags'].lower() and '/arch:avx512' not in get_compiler_config()['flags'].lower()): instruction_sets.remove('avx') if 'avx512' in instruction_sets and '/arch:avx512' not in get_compiler_config()['flags'].lower(): instruction_sets.remove('avx512') instruction_set = instruction_sets[-1] opts = {'cpu_openmp': False, 'cpu_vectorize_info': { 'instruction_set': instruction_set, 'assume_aligned': assume_aligned, 'assume_inner_stride_one': assume_inner_stride_one, 'assume_sufficient_line_padding': assume_sufficient_line_padding, }, 'target': 'cpu'} if not assume_inner_stride_one and 'storeS' not in get_vector_instruction_set('double', instruction_set): with pytest.warns(UserWarning) as warn: code = ps.create_kernel(collision, **opts) assert 'Could not vectorize loop' in warn[0].message.args[0] else: with pytest.warns(None) as warn: code = ps.create_kernel(collision, **opts) assert len(warn) == 0 code.compile()``````