Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lbmpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
lbmpy
Commits
da5b4d93
Commit
da5b4d93
authored
8 years ago
by
Martin Bauer
Browse files
Options
Downloads
Patches
Plain Diff
new_lbm: SRT simplifications working
parent
25fcd8b8
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
methods/momentbased.py
+33
-6
33 additions, 6 deletions
methods/momentbased.py
methods/momentbasedsimplifications.py
+9
-2
9 additions, 2 deletions
methods/momentbasedsimplifications.py
simplificationfactory.py
+48
-0
48 additions, 0 deletions
simplificationfactory.py
with
90 additions
and
8 deletions
methods/momentbased.py
+
33
−
6
View file @
da5b4d93
...
...
@@ -11,12 +11,10 @@ from lbmpy.moments import MOMENT_SYMBOLS, momentMatrix, exponentsToPolynomialRep
from
pystencils.equationcollection
import
EquationCollection
from
pystencils.sympyextensions
import
commonDenominator
RelaxationInfo
=
namedtuple
(
'
Relaxationinfo
'
,
[
'
equilibriumValue
'
,
'
relaxationRate
'
])
class
MomentBasedLbmMethod
(
AbstractLbmMethod
):
def
__init__
(
self
,
stencil
,
momentToRelaxationInfoDict
,
conservedQuantityComputation
,
forceModel
=
None
):
"""
Moment based LBM is a class to represent the single (SRT), two (TRT) and multi relaxation time (MRT) methods.
...
...
@@ -63,7 +61,7 @@ class MomentBasedLbmMethod(AbstractLbmMethod):
undefinedEquilibriumSymbols
=
symbolsInEquilibriumMoments
-
conservedQuantities
assert
len
(
undefinedEquilibriumSymbols
)
==
0
,
"
Undefined symbol(s) in equilibrium moment: %s
"
%
\
(
undefinedEquilibriumSymbols
,
)
(
undefinedEquilibriumSymbols
,)
self
.
_weights
=
None
...
...
@@ -94,11 +92,11 @@ class MomentBasedLbmMethod(AbstractLbmMethod):
return
table
.
format
(
content
=
content
,
nb
=
'
style=
"
border:none
"'
)
@property
def
zerothOrderEquilibriumMomentSymbol
(
self
,):
def
zerothOrderEquilibriumMomentSymbol
(
self
,
):
return
self
.
_conservedQuantityComputation
.
definedSymbols
(
order
=
0
)[
1
]
@property
def
firstOrderEquilibriumMomentSymbols
(
self
,):
def
firstOrderEquilibriumMomentSymbols
(
self
,
):
return
self
.
_conservedQuantityComputation
.
definedSymbols
(
order
=
1
)[
1
]
@property
...
...
@@ -158,6 +156,8 @@ class MomentBasedLbmMethod(AbstractLbmMethod):
M
=
self
.
_momentMatrix
m_eq
=
self
.
_equilibriumMoments
relaxationRateSubExpressions
,
D
=
self
.
_generateRelaxationMatrix
(
D
)
collisionRule
=
f
+
M
.
inv
()
*
D
*
(
m_eq
-
M
*
f
)
collisionEqs
=
[
sp
.
Eq
(
lhs
,
rhs
)
for
lhs
,
rhs
in
zip
(
self
.
postCollisionPdfSymbols
,
collisionRule
)]
...
...
@@ -166,9 +166,36 @@ class MomentBasedLbmMethod(AbstractLbmMethod):
simplificationHints
.
update
(
self
.
_conservedQuantityComputation
.
definedSymbols
())
simplificationHints
[
'
relaxationRates
'
]
=
D
.
atoms
(
sp
.
Symbol
)
simplificationHints
[
'
stencil
'
]
=
self
.
stencil
return
EquationCollection
(
collisionEqs
,
eqValueEqs
.
subexpressions
+
eqValueEqs
.
mainEquations
,
allSubexpressions
=
relaxationRateSubExpressions
+
eqValueEqs
.
subexpressions
+
eqValueEqs
.
mainEquations
return
EquationCollection
(
collisionEqs
,
allSubexpressions
,
simplificationHints
)
@staticmethod
def
_generateRelaxationMatrix
(
relaxationMatrix
):
"""
For SRT and TRT the equations can be easier simplified if the relaxation times are symbols, not numbers.
This function replaces the numbers in the relaxation matrix with symbols in this case, and returns also
the subexpressions, that assign the number to the newly introduced symbol
"""
rr
=
[
relaxationMatrix
[
i
,
i
]
for
i
in
range
(
relaxationMatrix
.
rows
)]
uniqueRelaxationRates
=
set
(
rr
)
if
len
(
uniqueRelaxationRates
)
<=
2
:
# special handling for SRT and TRT
subexpressions
=
{}
for
rt
in
uniqueRelaxationRates
:
rt
=
sp
.
sympify
(
rt
)
if
not
isinstance
(
rt
,
sp
.
Symbol
):
rtSymbol
=
sp
.
Symbol
(
"
rt_%d
"
%
(
len
(
subexpressions
),))
subexpressions
[
rt
]
=
rtSymbol
newRR
=
[
subexpressions
[
sp
.
sympify
(
e
)]
if
sp
.
sympify
(
e
)
in
subexpressions
else
e
for
e
in
rr
]
substitutions
=
[
sp
.
Eq
(
e
[
1
],
e
[
0
])
for
e
in
subexpressions
.
items
()]
return
substitutions
,
sp
.
diag
(
*
newRR
)
else
:
return
[],
relaxationMatrix
# ------------------------------------ Helper Functions ----------------------------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
methods/momentbasedsimplifications.py
+
9
−
2
View file @
da5b4d93
...
...
@@ -93,10 +93,13 @@ def replaceDensityAndVelocity(lbmCollisionEqs):
def
replaceCommonQuadraticAndConstantTerm
(
lbmCollisionEqs
):
"""
A common quadratic term (f_eq_common) is extracted from the collision equation for center
and substituted in all equations
Required simplification hints:
- density: density symbol
- velocity: sequence of velocity symbols
- relaxationRates:
- relaxationRates:
set of symbolic relaxation rates
- stencil:
"""
sh
=
lbmCollisionEqs
.
simplificationHints
...
...
@@ -123,6 +126,10 @@ def replaceCommonQuadraticAndConstantTerm(lbmCollisionEqs):
def
cseInOpposingDirections
(
lbmCollisionEqs
):
"""
Looks for common subexpressions in terms for opposing directions (e.g. north & south, top & bottom )
Required simplification hints:
- relaxationRates: set of symbolic relaxation rates
- stencil:
"""
sh
=
lbmCollisionEqs
.
simplificationHints
assert
'
stencil
'
in
sh
,
"
Needs simplification hint
'
stencil
'
: Sequence of discrete velocities
"
...
...
@@ -214,6 +221,6 @@ def __getCommonQuadraticAndConstantTerms(lbmCollisionEqs):
for
u
in
sh
[
'
velocity
'
]:
weight
=
weight
.
subs
(
u
,
0
)
weight
=
weight
/
sh
[
'
rho
'
]
weight
=
weight
/
sh
[
'
density
'
]
return
t
/
weight
This diff is collapsed.
Click to expand it.
simplificationfactory.py
0 → 100644
+
48
−
0
View file @
da5b4d93
from
functools
import
partial
import
sympy
as
sp
from
pystencils.equationcollection.simplifications
import
applyOnAllEquations
,
\
subexpressionSubstitutionInMainEquations
,
sympyCSE
,
addSubexpressionsForDivisions
def
createSimplificationStrategy
(
lbmMethod
,
doCseInOpposingDirections
=
False
,
doOverallCse
=
False
):
from
pystencils.equationcollection
import
SimplificationStrategy
from
lbmpy.methods
import
MomentBasedLbmMethod
from
lbmpy.methods.momentbasedsimplifications
import
replaceSecondOrderVelocityProducts
,
\
factorDensityAfterFactoringRelaxationTimes
,
factorRelaxationRates
,
cseInOpposingDirections
,
\
replaceCommonQuadraticAndConstantTerm
,
replaceDensityAndVelocity
s
=
SimplificationStrategy
()
if
isinstance
(
lbmMethod
,
MomentBasedLbmMethod
):
expand
=
partial
(
applyOnAllEquations
,
operation
=
sp
.
expand
)
expand
.
__name__
=
"
expand
"
s
.
add
(
expand
)
s
.
add
(
replaceSecondOrderVelocityProducts
)
s
.
add
(
expand
)
s
.
add
(
factorRelaxationRates
)
s
.
add
(
replaceDensityAndVelocity
)
s
.
add
(
replaceCommonQuadraticAndConstantTerm
)
s
.
add
(
factorDensityAfterFactoringRelaxationTimes
)
s
.
add
(
subexpressionSubstitutionInMainEquations
)
if
doCseInOpposingDirections
:
s
.
add
(
cseInOpposingDirections
)
if
doOverallCse
:
s
.
add
(
sympyCSE
)
s
.
add
(
addSubexpressionsForDivisions
)
return
s
if
__name__
==
'
__main__
'
:
from
lbmpy.stencils
import
getStencil
from
lbmpy.methods.momentbased
import
createOrthogonalMRT
stencil
=
getStencil
(
"
D2Q9
"
)
m
=
createOrthogonalMRT
(
stencil
,
compressible
=
True
)
cr
=
m
.
getCollisionRule
()
simp
=
createSimplificationStrategy
(
m
)
simp
(
cr
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment