Thermal
jax_lab.core.thermal.Thermal
Bases: object
Single phase hybrid thermal LBM solver. The fluid is advanced by the wrapped LBM fluid_solver while the temperature field is advanced by a finite difference solver on the same mesh.
The temperature equation solved is: \frac{\partial T}{\partial t} = -\mathbf{u} \cdot \nabla T + \frac{1}{\rho c_v}(\nabla \cdot (K \nabla T) + S_T) where S_T is a user defined source term (see source()).
Spatial derivatives are evaluated with the isotropic lattice difference stencils in grad_x and laplacian_x and time integration uses the fourth order Runge-Kutta scheme with dt = 1.
Parameters
fluid_solver (LBMBase): Configured fluid solver instance (e.g. BGKSim or MRTSim). Grid, lattice, precision and I/O settings are shared with it.
specific_heat (float or numpy.ndarray): Specific heat c_v. Either a scalar or an array of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
thermal_conductivity (float or numpy.ndarray): Thermal conductivity K with the same shape options as specific_heat.
checkpoint_dir (str, optional): Directory for temperature checkpoints. Defaults to “./temperature_checkpoints”.
apply_buoyancy (bool, optional): If True, adds a density variation based buoyancy force to the fluid solver. Defaults to False.
gravity (sequence of float, optional): Gravitational acceleration vector, required when apply_buoyancy is True.
rk_substeps (int, optional): Number of Runge-Kutta substeps per LBM step. Defaults to 1. The explicit RK4 diffusion update is stable only if K / (rho c_v) |lambda_max| dt < 2.785, where |lambda_max| is the largest eigenvalue of the lattice laplacian stencil (16/3 for both D2Q9 and D3Q19). Increase rk_substeps when the thermal diffusivity exceeds this limit.
Source code in jax_lab/core/thermal.py
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 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 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |
RHS
Right hand side of the thermal equation: -\mathbf{u} \cdot \nabla T + \frac{1}{\rho c_v}(K \nabla^2 T + \nabla K \cdot \nabla T + S_T)
Parameters
T (jax.numpy.ndarray): Temperature field.
rho (jax.numpy.ndarray): Density field.
u (jax.numpy.ndarray): Velocity field.
Returns
jax.numpy.ndarray: RHS of the thermal equation, of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Source code in jax_lab/core/thermal.py
apply_bc
This function applies the boundary conditions to the temperature field.
It iterates over all thermal boundary conditions and applies them in list order.
Parameters
T (jax.numpy.ndarray): Temperature field.
timestep (int): Current timestep of the simulation.
Returns
jax.numpy.ndarray: Temperature field after applying boundary conditions.
Source code in jax_lab/core/thermal.py
apply_force_thermal
Modified version of the single phase apply_force function that adds a density variation based buoyancy force to any user defined fluid force, using the exact-difference method due to Kupershtokh.
Computes delta_feq = feq(rho, u + du) - feq(rho, u) directly from cu, dcu and delta_usqr instead of building a second full equilibrium distribution and subtracting feq from it. Lattice-generic (D2Q9, D3Q19, D3Q27).
f_postcollision is population-space for BGKSim/KBCSim, but moment-space (m, with feq’s slot holding meq) when fluid_solver is an MRTSim, since MRTSim.collision calls apply_force with its moments - adding a population-space delta_feq directly to those would silently mix spaces. When fluid_solver is an MRTSim, delta_feq is transformed into moment space with its M first, matching MRTSim.apply_force.
Note: the buoyancy force is computed from the local density deviation relative to the mean density, not from the temperature field, since the fluid collision does not have access to the temperature.
Parameters
f_postcollision (jax.numpy.ndarray): Post-collision distribution functions (population-space), or moments (moment-space, when fluid_solver is an MRTSim).
feq (jax.numpy.ndarray): Equilibrium distribution functions. Unused - kept for interface compatibility, since the compact difference formula only needs rho, u and the force.
rho (jax.numpy.ndarray): Density field.
u (jax.numpy.ndarray): Velocity field.
Returns
jax.numpy.ndarray: f_postcollision with the force applied, in the same space it was given in.
Source code in jax_lab/core/thermal.py
assign_fields_sharded
This function initializes the temperature field of the simulation.
It calls initialize_temperature_field, which can return a scalar or an array of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D. If it returns None, a uniform temperature of 1 is assumed.
Returns
T: a distributed JAX array of shape (nx, ny, 1) or (nx, ny, nz, 1) holding the temperature field.
Source code in jax_lab/core/thermal.py
distributed_array_init
Initialize a distributed array using JAX, with a specified shape, data type, and initial value. Optionally, provide a custom sharding strategy.
Parameters
shape (tuple): The shape of the array to be created.
ttype (dtype): The data type of the array to be created.
init_val (scalar, optional): The initial value to fill the array with. Defaults to 0.
sharding (Sharding, optional): The sharding strategy to use. Defaults to the fluid solver sharding.
Returns
jax.numpy.ndarray: A JAX array with the specified shape, data type, initial value, and sharding strategy.
Source code in jax_lab/core/thermal.py
grad_x
Compute the gradient of a scalar field using the isotropic lattice difference stencil: \nabla \phi(x) = \frac{1}{c_s^2} \sum_i w_i \mathbf{c}_i \phi(x + \mathbf{c}_i)
The neighbor values are obtained with the streaming operation, which shifts along -c_i, hence the sign flip in the final contraction.
Note: streaming wraps periodically at the domain edges. On non-periodic boundaries the affected nodes must be corrected by the thermal boundary conditions.
Parameters
field (jax.numpy.ndarray): Scalar field of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Returns
jax.numpy.ndarray: Gradient of the field, of shape (nx, ny, 2) in 2D or (nx, ny, nz, 3) in 3D.
Source code in jax_lab/core/thermal.py
handle_io_timestep
This function handles the input/output (I/O) operations at each time step of the simulation.
It prepares the data to be saved and calls the output_data function, which can be overwritten by the user to customize the I/O operations.
Parameters
timestep (int): The current time step of the simulation.
f (jax.numpy.ndarray): The post-streaming distribution functions at the current time step.
fstar (jax.numpy.ndarray): The post-collision distribution functions at the current time step.
T (jax.numpy.ndarray): The temperature field at the current time step.
rho (jax.numpy.ndarray): The density field at the current time step.
u (jax.numpy.ndarray): The velocity field at the current time step.
T_prev (jax.numpy.ndarray): The temperature field at the previous I/O time step.
rho_prev (jax.numpy.ndarray): The density field at the previous I/O time step.
u_prev (jax.numpy.ndarray): The velocity field at the previous I/O time step.
Source code in jax_lab/core/thermal.py
initialize_temperature_field
Return the initial temperature field.
The default implementation returns None, which :meth:assign_fields_sharded interprets as a uniform temperature of 1.
Override this method to provide a scalar or spatially varying field.
Returns
None Sentinel requesting the default uniform temperature.
Source code in jax_lab/core/thermal.py
laplacian_x
Compute the laplacian of a scalar field using the isotropic lattice difference stencil: \nabla^2 \phi(x) = \frac{2}{c_s^2} \sum_i w_i [\phi(x + \mathbf{c}_i) - \phi(x)]
Note: streaming wraps periodically at the domain edges. On non-periodic boundaries the affected nodes must be corrected by the thermal boundary conditions.
Parameters
field (jax.numpy.ndarray): Scalar field of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Returns
jax.numpy.ndarray: Laplacian of the field, of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Source code in jax_lab/core/thermal.py
output_data
This function is intended to be overwritten by the user to customize the I/O operations of the simulation. By default it does nothing.
Parameters
**kwargs (dict): The simulation data at the current I/O timestep; see handle_io_timestep.
Source code in jax_lab/core/thermal.py
run
This function runs the hybrid thermal LBM simulation for a specified number of time steps.
It first initializes the temperature field and the fluid distribution functions and then enters a loop where it performs the coupled simulation steps (fluid collision, streaming and boundary conditions, followed by the Runge-Kutta temperature update) for each time step.
The function can also print the progress of the simulation, save the simulation data, and compute the performance of the simulation in million lattice updates per second (MLUPS).
Parameters
t_max (int): The total number of time steps to run the simulation.
Returns
f (jax.numpy.ndarray): The distribution functions after the simulation.
T (jax.numpy.ndarray): The temperature field after the simulation.
Source code in jax_lab/core/thermal.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | |
set_thermal_boundary_conditions
This function sets the boundary conditions for the temperature field.
It is intended to be overwritten by the user to specify the boundary conditions according to the specific problem being solved, by appending ThermalBoundaryCondition instances (DirichletTemperature, NeumannTemperature) to self.thermal_BCs. Conditions are applied in list order, so later entries win on shared nodes (e.g. corners).
By default no thermal boundary condition is applied, which corresponds to a fully periodic temperature field.
Source code in jax_lab/core/thermal.py
source
Source term S_T of the thermal equation.
It is intended to be overwritten by the user to specify a volumetric heat source according to the specific problem being solved. By default it returns zero everywhere.
Parameters
T (jax.numpy.ndarray): Temperature field.
Returns
jax.numpy.ndarray: Source term at each lattice node, same shape as T.
Source code in jax_lab/core/thermal.py
step
This function performs a single step of the hybrid thermal LBM simulation.
It evaluates the macroscopic fields and temperature right hand side at the current time level, then advances the fluid and temperature fields together by one time step.
Parameters
T_prev (jax.numpy.ndarray): The temperature field from the previous timestep.
f_poststreaming (jax.numpy.ndarray): The post-streaming distribution functions.
timestep (int): The current timestep of the simulation.
return_fpost (bool, optional): If True, the function also returns the post-collision distribution functions.
Returns
T (jax.numpy.ndarray): The temperature field after the simulation step.
f_poststreaming (jax.numpy.ndarray): The post-streaming distribution functions after the simulation step.
f_postcollision (jax.numpy.ndarray or None): The post-collision distribution functions after the simulation step, or None if return_fpost is False.
Source code in jax_lab/core/thermal.py
update_macroscopic_output
Compute the macroscopic density and velocity used for I/O. Overridden by MultiphaseThermal to include the force correction and pytrees.
Parameters
f (jax.numpy.ndarray): Post-streaming distribution functions.
T (jax.numpy.ndarray): Temperature field.
Returns
rho (jax.numpy.ndarray): Density field.
u (jax.numpy.ndarray): Velocity field.
Source code in jax_lab/core/thermal.py
jax_lab.core.thermal.MultiphaseThermal
Bases: Thermal
Multiphase implementation of the hybrid thermal LBM solver with phase change support. The fluid is advanced by the wrapped Multiphase solver (pytrees of distribution functions, one per component) while a single shared temperature field is advanced by the finite difference solver.
The temperature equation solved is (reference 1 in the module docstring): \frac{\partial T}{\partial t} = -\mathbf{u} \cdot \nabla T + \frac{1}{\rho c_v}(\nabla \cdot (K \nabla T) - T \frac{\partial p_{EOS}}{\partial T} \nabla \cdot \mathbf{u} + S_T)
where the T (dp_EOS/dT) div(u) term accounts for the latent heat released or absorbed during phase change, rho and u are the mixture density and velocity, and S_T is a user defined source term (see source()).
The temperature field is passed into the fluid solver every step, so the EOS of the fluid solver must be constructed with temperature_field_type=”thermal”; the pressure (and hence the pseudopotential) then follows the local temperature, which is what drives evaporation and condensation.
Assumes all components share a single temperature field (thermal equilibrium between components at every node).
Source code in jax_lab/core/thermal.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
RHS
Right hand side of the thermal equation for multiphase flow: -\mathbf{u} \cdot \nabla T + \frac{1}{\rho c_v}(K \nabla^2 T + \nabla K \cdot \nabla T - T \frac{\partial p_{EOS}}{\partial T} \nabla \cdot \mathbf{u} + S_T)
The mixture (total) density and velocity are used; the phase change term uses the sum of dp_EOS/dT over all components.
Parameters
T (jax.numpy.ndarray): Temperature field.
rho_tree (pytree of jax.numpy.ndarray): Density field of all components.
u_tree (pytree of jax.numpy.ndarray): Velocity field of all components.
Returns
jax.numpy.ndarray: RHS of the thermal equation, of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Source code in jax_lab/core/thermal.py
divergence_x
Compute the divergence of a vector field using the isotropic lattice difference stencil, by summing the diagonal entries of the gradient of each velocity component.
Parameters
u (jax.numpy.ndarray): Vector field of shape (nx, ny, 2) in 2D or (nx, ny, nz, 3) in 3D.
Returns
jax.numpy.ndarray: Divergence of the field, of shape (nx, ny, 1) in 2D or (nx, ny, nz, 1) in 3D.
Source code in jax_lab/core/thermal.py
step
This function performs a single step of the multiphase hybrid thermal LBM simulation.
It evaluates the component fields and temperature right hand side at the current time level, then advances the fluid and temperature fields together by one time step. The current temperature is passed to the thermal EOS used by the fluid collision.
Parameters
T_prev (jax.numpy.ndarray): The temperature field from the previous timestep.
f_poststreaming_tree (pytree of jax.numpy.ndarray): The post-streaming distribution functions.
timestep (int): The current timestep of the simulation.
return_fpost (bool, optional): If True, the function also returns the post-collision distribution functions.
Returns
T (jax.numpy.ndarray): The temperature field after the simulation step.
f_poststreaming_tree (pytree of jax.numpy.ndarray): The post-streaming distribution functions after the simulation step.
f_postcollision_tree (pytree of jax.numpy.ndarray or None): The post-collision distribution functions after the simulation step, or None if return_fpost is False.
Source code in jax_lab/core/thermal.py
update_macroscopic_output
Compute the component densities and force-corrected velocities for I/O.
Parameters
f_tree (pytree of jax.numpy.ndarray): Post-streaming distribution functions.
T (jax.numpy.ndarray): Temperature field.
Returns
rho_tree (pytree of jax.numpy.ndarray): Density field of all components.
u_tree (pytree of jax.numpy.ndarray): Velocity field of all components.