Skip to content

Commit 9287217

Browse files
change list into sets
1 parent 771f1e6 commit 9287217

File tree

4 files changed

+134
-123
lines changed

4 files changed

+134
-123
lines changed

geos-geomechanics/src/geos/geomechanics/model/MohrCircle.py

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
# SPDX-FileContributor: Alexandre Benedicto, Martin Lemay
44
import numpy as np
55
import numpy.typing as npt
6-
from typing_extensions import Self
6+
from typing_extensions import Self, Any
77

88
from geos.geomechanics.processing.geomechanicsCalculatorFunctions import (
99
computeStressPrincipalComponentsFromStressVector, )
1010

1111
__doc__ = """
1212
MohrCircle module define the Mohr's circle parameters.
1313
14-
Inputs are a 6 component stress vector, a circle id, and the mechanical
15-
convention used for compression.
14+
Inputs are a 6 component stress vector, and a circle id.
1615
The class computes principal components from stress vector during initialization.
1716
Accessors get access to these 3 principal components as well as circle center
1817
and radius.
@@ -23,21 +22,21 @@
2322
2423
from processing.MohrCircle import MohrCircle
2524
26-
# create the object
27-
stressVector :npt.NDArray[np.float64]
28-
circleId :str
29-
mohrCircle :MohrCircle = MohrCircle(circleId)
25+
# Create the object
26+
stressVector: npt.NDArray[np.float64]
27+
circleId: str
28+
mohrCircle: MohrCircle = MohrCircle(circleId)
3029
31-
# either directly set principal components (p3 <= p2 <= p1)
30+
# Either directly set principal components (p3 <= p2 <= p1)
3231
mohrCircle.SetPrincipalComponents(p3, p2, p1)
33-
# or compute them from stress vector
32+
# Or compute them from stress vector
3433
mohrCircle.computePrincipalComponents(stressVector)
3534
36-
# access to members
37-
id :str = mohrCircle.getCircleId()
38-
p1, p2, p3 :float = mohrCircle.getPrincipalComponents()
39-
radius :float = mohrCircle.getCircleRadius()
40-
center :float = mohrCircle.getCircleCenter()
35+
# Access to members
36+
id: str = mohrCircle.getCircleId()
37+
p1, p2, p3: float = mohrCircle.getPrincipalComponents()
38+
radius: float = mohrCircle.getCircleRadius()
39+
center: float = mohrCircle.getCircleCenter()
4140
"""
4241

4342

@@ -49,60 +48,86 @@ def __init__( self: Self, circleId: str ) -> None:
4948
Args:
5049
circleId (str): Mohr's circle id.
5150
"""
52-
self.m_circleId: str = circleId
51+
self.circleId: str = circleId
5352

54-
self.m_p1: float = 0.0
55-
self.m_p2: float = 0.0
56-
self.m_p3: float = 0.0
53+
self.p1: float = 0.0
54+
self.p2: float = 0.0
55+
self.p3: float = 0.0
5756

5857
def __str__( self: Self ) -> str:
5958
"""Overload of __str__ method."""
60-
return self.m_circleId
59+
return self.circleId
6160

6261
def __repr__( self: Self ) -> str:
6362
"""Overload of __repr__ method."""
64-
return self.m_circleId
63+
return self.circleId
64+
65+
def __eq__( self: Self, other: Any ) -> bool:
66+
"""Overload of __eq__ method."""
67+
if not isinstance( other, MohrCircle ):
68+
return NotImplemented
69+
return self.circleId == other.circleId
70+
71+
def __hash__( self: Self ) -> int:
72+
"""Overload of hash method."""
73+
return hash( self.circleId )
6574

6675
def setCircleId( self: Self, circleId: str ) -> None:
6776
"""Set circle Id variable.
6877
6978
Args:
70-
circleId (str): circle Id.
79+
circleId (str): Circle Id.
7180
"""
72-
self.m_circleId = circleId
81+
self.circleId = circleId
7382

7483
def getCircleId( self: Self ) -> str:
7584
"""Access the Id of the Mohr circle.
7685
7786
Returns:
7887
str: Id of the Mohr circle
7988
"""
80-
return self.m_circleId
89+
return self.circleId
8190

8291
def getCircleRadius( self: Self ) -> float:
83-
"""Compute and return Mohr's circle radius from principal components."""
84-
return ( self.m_p1 - self.m_p3 ) / 2.0
92+
"""Compute and return Mohr's circle radius from principal components.
93+
94+
Returns:
95+
float: Mohr circle radius.
96+
"""
97+
return ( self.p1 - self.p3 ) / 2.0
8598

8699
def getCircleCenter( self: Self ) -> float:
87-
"""Compute and return Mohr's circle center from principal components."""
88-
return ( self.m_p1 + self.m_p3 ) / 2.0
100+
"""Compute and return Mohr's circle center from principal components.
101+
102+
Returns:
103+
float: Mohr circle center.
104+
"""
105+
return ( self.p1 + self.p3 ) / 2.0
89106

90107
def getPrincipalComponents( self: Self ) -> tuple[ float, float, float ]:
91-
"""Get Moh's circle principal components."""
92-
return ( self.m_p3, self.m_p2, self.m_p1 )
108+
"""Get Moh's circle principal components.
109+
110+
Returns:
111+
tuple[float, float, float]: Mohr circle principal components.
112+
"""
113+
return ( self.p3, self.p2, self.p1 )
93114

94115
def setPrincipalComponents( self: Self, p3: float, p2: float, p1: float ) -> None:
95116
"""Set principal components.
96117
97118
Args:
98-
p3 (float): first component. Must be the lowest.
99-
p2 (float): second component.
100-
p1 (float): third component. Must be the greatest.
119+
p3 (float): First component. Must be the lowest.
120+
p2 (float): Second component.
121+
p1 (float): Third component. Must be the greatest.
122+
123+
Raises:
124+
ValueError: Expected p3 <= p2 <= p1.
101125
"""
102-
assert ( p3 <= p2 ) and ( p2 <= p1 ), "Component order is wrong."
103-
self.m_p3 = p3
104-
self.m_p2 = p2
105-
self.m_p1 = p1
126+
if ( p3 <= p2 ) and ( p2 <= p1 ):
127+
raise ValueError( "Component order is wrong. Expected p3 <= p2 <= p1." )
128+
self.p3 = p3
129+
self.p2 = p2
130+
self.p1 = p1
106131

107132
def computePrincipalComponents( self: Self, stressVector: npt.NDArray[ np.float64 ] ) -> None:
108133
"""Calculate principal components.
@@ -111,5 +136,5 @@ def computePrincipalComponents( self: Self, stressVector: npt.NDArray[ np.float6
111136
stressVector (npt.NDArray[np.float64]): 6 components stress vector
112137
Stress vector must follow GEOS convention (XX, YY, ZZ, YZ, XZ, XY)
113138
"""
114-
# get stress principal components
115-
self.m_p3, self.m_p2, self.m_p1 = ( computeStressPrincipalComponentsFromStressVector( stressVector ) )
139+
# Get stress principal components
140+
self.p3, self.p2, self.p1 = ( computeStressPrincipalComponentsFromStressVector( stressVector ) )

geos-pv/src/geos/pv/utils/mohrCircles/functionsMohrCircle.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
circles and Mohr-Coulomb failure envelope.
2020
"""
2121

22+
2223
class StressConventionEnum( Enum ):
2324
"""Utility Enum to define the effective stress convention used for compression.
2425
2526
The usual convention considers the compression as positive.
2627
With GEOS convention, the compression is considered negative.
2728
"""
28-
GEOS_STRESS_CONVENTION = - 1.0
29+
GEOS_STRESS_CONVENTION = -1.0
2930
COMMON_STRESS_CONVENTION = 1.0
3031

3132

32-
3333
def buildPythonViewScript(
3434
dir_path: str,
3535
mohrCircles: list[ MohrCircle ],
@@ -73,11 +73,11 @@ def findAnnotateTuples( mohrCircle: MohrCircle, ) -> tuple[ str, str, tuple[ flo
7373
"""Get the values and location of min and max normal stress or Mohr's circle.
7474
7575
Args:
76-
mohrCircle (MohrCircle): input Mohr's circle
77-
maxTau (float): max shear stress
76+
mohrCircle (MohrCircle): Mohr's circle to consider.
77+
maxTau (float): Max shear stress
7878
7979
Returns:
80-
tuple[str, str, tuple[float, float], tuple[float, float]]: labels and
80+
tuple[str, str, tuple[float, float], tuple[float, float]]: Labels and
8181
location of labels.
8282
"""
8383
p3, _, p1 = mohrCircle.getPrincipalComponents()
@@ -93,9 +93,8 @@ def getMohrCircleId( cellId: str, timeStep: str ) -> str:
9393
"""Get Mohr's circle ID from cell id and time step.
9494
9595
Args:
96-
cellId (str): cell ID
97-
98-
timeStep (str): time step.
96+
cellId (str): Cell ID
97+
timeStep (str): Time step.
9998
10099
Returns:
101100
str: Mohr's circle ID
@@ -108,30 +107,32 @@ def createMohrCircleAtTimeStep(
108107
cellIds: list[ str ],
109108
timeStep: str,
110109
convention: StressConventionEnum,
111-
) -> list[ MohrCircle ]:
110+
) -> set[ MohrCircle ]:
112111
"""Create MohrCircle object(s) at a given time step for all cell ids.
113112
114113
Args:
115-
stressArray (npt.NDArray[np.float64]): stress numpy array
116-
117-
cellIds (list[str]): list of cell ids
118-
119-
timeStep (str): time step
114+
stressArray (npt.NDArray[np.float64]): Stress numpy array
115+
cellIds (list[str]): List of cell ids
116+
timeStep (str): Time step
117+
convention (StressConventionEnum): Convention used for compression.
120118
121-
convention (bool): convention used for compression.
122-
* False is Geos convention (compression is negative)
123-
* True is usual convention (compression is positive)
119+
Raises:
120+
ValueError: Stress array must consists of 6 components.
124121
125122
Returns:
126-
list[MohrCircle]: list of MohrCircle objects.
123+
set[MohrCircle]: Set of MohrCircle objects.
127124
"""
128-
assert stressArray.shape[ 1 ] == 6, "Stress vector must be of size 6."
129-
mohrCircles: list[ MohrCircle ] = []
125+
if stressArray.shape[ 1 ] == 6:
126+
raise ValueError( "Expected 6 components for stress array, not {stressArray.shape[ 1 ]}.\n \
127+
Cannot proceed with the creation of Mohr circles." )
128+
129+
mohrCircles: set[ MohrCircle ] = set()
130130
for i, cellId in enumerate( cellIds ):
131131
ide: str = getMohrCircleId( cellId, timeStep )
132132
mohrCircle: MohrCircle = MohrCircle( ide )
133133
mohrCircle.computePrincipalComponents( stressArray[ i ] * convention.value )
134-
mohrCircles.append( mohrCircle )
134+
mohrCircles.add( mohrCircle )
135+
135136
return mohrCircles
136137

137138

@@ -140,11 +141,10 @@ def createMohrCirclesFromPrincipalComponents(
140141
"""Create Mohr's circle objects from principal components.
141142
142143
Args:
143-
mohrCircleParams (list[tuple[str, float, float, float]]): list of Mohr's
144-
circle parameters
144+
mohrCircleParams (list[tuple[str, float, float, float]]): List of Mohr's circle parameters
145145
146146
Returns:
147-
list[MohrCircle]: list of Mohr's circle objects.
147+
list[MohrCircle]: List of Mohr's circle objects.
148148
"""
149149
mohrCircles: list[ MohrCircle ] = []
150150
for circleId, p3, p2, p1 in mohrCircleParams:
@@ -158,9 +158,8 @@ def createMohrCoulombEnvelope( rockCohesion: float, frictionAngle: float ) -> Mo
158158
"""Create MohrCoulomb object from user parameters.
159159
160160
Args:
161-
rockCohesion (float): rock cohesion (Pa).
162-
163-
frictionAngle (float): friction angle in radian.
161+
rockCohesion (float): Rock cohesion (Pa).
162+
frictionAngle (float): Friction angle in radian.
164163
165164
Returns:
166165
MohrCoulomb: MohrCoulomb object.

geos-pv/src/geos/pv/utils/mohrCircles/mainMohrCircles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
)
3131

3232
def setup_data( view ) -> None: # noqa
33+
"""Setup data."""
3334
pass
3435

3536
def render( view, width: int, height: int ): # noqa
37+
"""Render method."""
3638
fig.set_size_inches( float( width ) / 100.0, float( height ) / 100.0 )
3739
return python_view.figure_to_image( fig )
3840

0 commit comments

Comments
 (0)