Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogstools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Max Bittens
ogstools
Commits
aebb3d2b
Commit
aebb3d2b
authored
1 year ago
by
Florian Zill
Browse files
Options
Downloads
Patches
Plain Diff
[propertylib] added test for mechanics functions
parent
58608dec
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_mechanics.py
+129
-0
129 additions, 0 deletions
tests/test_mechanics.py
with
129 additions
and
0 deletions
tests/test_mechanics.py
0 → 100644
+
129
−
0
View file @
aebb3d2b
"""
Unit tests for solid mechanics.
"""
import
unittest
import
numpy
as
np
from
ogstools.propertylib
import
tensor_math
from
ogstools.propertylib.tensor_math
import
sym_tensor_to_mat
class
MechanicsTest
(
unittest
.
TestCase
):
"""
Test case for physical properties.
"""
rng
=
np
.
random
.
default_rng
()
N_SAMPLES
=
100000
def
equality
(
self
,
vals1
:
np
.
ndarray
,
vals2
:
np
.
ndarray
,
rtol
=
1e-7
,
atol
=
1e-9
):
"""
Assert the equality of two arrays.
"""
np
.
testing
.
assert_allclose
(
vals1
,
vals2
,
verbose
=
True
,
rtol
=
rtol
,
atol
=
atol
)
def
test_frobenius_norm
(
self
):
"""
Test Frobenius norm.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
sig_mat
=
sym_tensor_to_mat
(
sig
)
frob2
=
np
.
sqrt
(
tensor_math
.
trace
(
np
.
diagonal
(
np
.
transpose
(
sig_mat
,
(
0
,
2
,
1
))
@
sig_mat
,
0
,
2
)
)
)
self
.
equality
(
tensor_math
.
frobenius_norm
(
sig
),
frob2
)
def
test_I1
(
self
):
"""
Test first invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
self
.
equality
(
tensor_math
.
I1
(
sig
),
tensor_math
.
trace
(
tensor_math
.
eigenvalues
(
sig
)),
)
def
test_I2
(
self
):
"""
Test second invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
eig_vals
=
tensor_math
.
eigenvalues
(
sig
)
self
.
equality
(
tensor_math
.
I2
(
sig
),
np
.
sum
(
eig_vals
*
np
.
roll
(
eig_vals
,
1
,
axis
=-
1
),
axis
=-
1
),
)
def
test_I3
(
self
):
"""
Test third invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
eig_vals
=
tensor_math
.
eigenvalues
(
sig
)
self
.
equality
(
tensor_math
.
I3
(
sig
),
np
.
prod
(
eig_vals
,
axis
=-
1
))
def
test_J1
(
self
):
"""
Test first deviator invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
j1
=
tensor_math
.
J1
(
sig
)
self
.
equality
(
j1
,
np
.
zeros
(
j1
.
shape
))
def
test_J2
(
self
):
"""
Test second deviator invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
self
.
equality
(
tensor_math
.
J2
(
sig
),
0.5
*
(
tensor_math
.
trace
(
sig
**
2
)
-
(
1.0
/
3.0
)
*
tensor_math
.
trace
(
sig
)
**
2
),
)
def
test_J3
(
self
):
"""
Test third deviator invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
# not exactly sure why, but for stresses where the below condition
# is very close to zero, the error of this test shoots up.
# Probably some floating point precision issue.
mask
=
(
np
.
abs
(
tensor_math
.
trace
(
sig
-
tensor_math
.
mean
(
sig
)[...,
None
])
)
>
1e-9
)
sig
=
sig
[
mask
]
self
.
equality
(
tensor_math
.
J3
(
sig
),
(
1.0
/
3.0
)
*
(
tensor_math
.
trace
(
sig
**
3
)
-
tensor_math
.
trace
(
sig
**
2
)
*
tensor_math
.
trace
(
sig
)
+
(
2.0
/
9.0
)
*
tensor_math
.
trace
(
sig
)
**
3.0
),
)
def
test_von_mises
(
self
):
"""
Test von Mises invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
ev
=
tensor_math
.
eigenvalues
(
sig
)
self
.
equality
(
tensor_math
.
von_mises
(
sig
),
np
.
sqrt
(
0.5
*
np
.
sum
(
np
.
square
(
ev
-
np
.
roll
(
ev
,
1
,
-
1
)),
-
1
)),
)
def
test_octahedral_shear_stress
(
self
):
"""
Test octahedral shear stress invariant.
"""
for
symten_len
in
[
4
,
6
]:
sig
=
self
.
rng
.
random
((
self
.
N_SAMPLES
,
symten_len
))
*
1e6
self
.
equality
(
tensor_math
.
octahedral_shear
(
sig
),
(
1.0
/
3.0
)
*
np
.
sqrt
(
2
*
tensor_math
.
I1
(
sig
)
**
2
-
6
*
tensor_math
.
I2
(
sig
)
),
)
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