3
3
import warnings
4
4
from typing import List , Tuple
5
5
6
- from classy_blocks .grading import relations as gr
6
+ import scipy .optimize
7
+
8
+ import classy_blocks .grading .relations as gr
7
9
from classy_blocks .grading .chop import Chop
8
10
from classy_blocks .types import ChopTakeType
9
11
@@ -20,23 +22,13 @@ def sum_length(start_size: float, count: int, c2c_expansion: float) -> float:
20
22
return length
21
23
22
24
23
- def sum_count (lengths : List [float ], chops : List [Chop ]):
24
- count = 0
25
-
26
- for i , chop in enumerate (chops ):
27
- length = lengths [i ]
28
- count += chop .calculate (length ).count
29
-
30
- return count
31
-
32
-
33
25
class ChopParams (abc .ABC ):
34
26
@abc .abstractmethod
35
27
def get_count (self , length : float ) -> int :
36
28
"""Calculates count based on given length - used once only"""
37
29
38
30
@abc .abstractmethod
39
- def get_chops (self , count : int , length : float ) -> List [Chop ]:
31
+ def get_chops (self , count : int , length : float , size_before : float = 0 , size_after : float = 0 ) -> List [Chop ]:
40
32
"""Fixes cell count but modifies chops so that proper cell sizing will be obeyed"""
41
33
# That depends on inherited classes' philosophy
42
34
@@ -48,7 +40,7 @@ class FixedCountParams(ChopParams):
48
40
def get_count (self , _length ):
49
41
return self .count
50
42
51
- def get_chops (self , count , _length ) -> List [Chop ]:
43
+ def get_chops (self , count , _length , _size_before = 0 , _size_after = 0 ) -> List [Chop ]:
52
44
return [Chop (count = count )]
53
45
54
46
@@ -60,7 +52,7 @@ class SimpleChopParams(ChopParams):
60
52
def get_count (self , length : float ):
61
53
return int (length / self .cell_size )
62
54
63
- def get_chops (self , count , _length ):
55
+ def get_chops (self , count , _length , _size_before = 0 , _size_after = 0 ):
64
56
return [Chop (count = count )]
65
57
66
58
@@ -69,17 +61,61 @@ class HighReChopParams(ChopParams):
69
61
cell_size : float
70
62
71
63
def get_count (self , length : float ):
72
- # the first chop defines the count; it's a very simple one
73
- return int (length / self .cell_size )
74
-
75
- def get_chops (self , count : int , _ ):
76
- # TODO: adjust length ratio for smoothest transition in the middle of block
77
- return [
78
- Chop (length_ratio = 0.5 , count = count // 2 , start_size = self .cell_size ),
79
- Chop (length_ratio = 0.5 , count = count // 2 , end_size = self .cell_size ),
64
+ # the first chop defines the count;
65
+ count = int (length / self .cell_size )
66
+ # it must be divisible by 2
67
+ if count % 2 != 0 :
68
+ count += 1
69
+
70
+ return count
71
+
72
+ def get_chops (self , count , length , size_before = 0 , size_after = 0 ):
73
+ # length of the wire that was used to set count
74
+ if size_before == 0 :
75
+ size_before = self .cell_size
76
+ if size_after == 0 :
77
+ size_after = self .cell_size
78
+
79
+ chops = [
80
+ Chop (count = count // 2 ),
81
+ Chop (count = count // 2 ),
80
82
]
81
83
84
+ def objfun (params ):
85
+ chops [0 ].length_ratio = params [0 ]
86
+ chops [1 ].length_ratio = 1 - params [0 ]
87
+
88
+ chops [0 ].total_expansion = params [1 ]
89
+ chops [1 ].total_expansion = params [2 ]
90
+
91
+ data_1 = chops [0 ].calculate (length )
92
+ data_2 = chops [1 ].calculate (length )
82
93
94
+ ofstart = (size_before - data_1 .start_size ) ** 2
95
+ ofmid1 = (data_1 .end_size - self .cell_size ) ** 2
96
+ ofmid2 = (data_2 .start_size - self .cell_size ) ** 2
97
+ ofend = (data_2 .end_size - size_after ) ** 2
98
+
99
+ return max ([ofstart , ofmid1 , ofmid2 , ofend ])
100
+
101
+ initial = [0.5 , 1 , 1 ]
102
+ bounds = (
103
+ (0.1 , 0.9 ),
104
+ (0.1 , 10 ),
105
+ (0.1 , 10 ),
106
+ )
107
+ result = scipy .optimize .minimize (objfun , initial , bounds = bounds ).x
108
+
109
+ chops [0 ].length_ratio = result [0 ]
110
+ chops [1 ].length_ratio = 1 - result [0 ]
111
+
112
+ chops [0 ].total_expansion = result [1 ]
113
+ chops [1 ].total_expansion = result [2 ]
114
+
115
+ return chops
116
+
117
+
118
+ # INVALID! Next on list
83
119
@dataclasses .dataclass
84
120
class LowReChopParams (ChopParams ):
85
121
"""Parameters for mesh grading for Low-Re cases.
@@ -164,16 +200,17 @@ def get_count(self, length: float):
164
200
165
201
if remaining_length <= 0 :
166
202
warnings .warn ("Stopping chops at boundary layer (not enough space)!" , stacklevel = 1 )
167
- return sum_count ([length ], chops )
203
+ # return chops
204
+ return 0
168
205
169
206
# buffer
170
207
buffer , buffer_size = self ._get_buffer_chop (last_bl_size )
171
208
buffer .length_ratio = buffer_size / length
172
209
chops .append (buffer )
173
210
if buffer_size >= remaining_length :
174
211
warnings .warn ("Stopping chops at buffer layer (not enough space)!" , stacklevel = 1 )
175
-
176
- return sum_count ([ self . boundary_layer_thickness , buffer_size ], chops )
212
+ # return chops
213
+ return 1
177
214
178
215
# bulk
179
216
remaining_length = remaining_length - buffer_size
@@ -182,7 +219,7 @@ def get_count(self, length: float):
182
219
chops .append (bulk )
183
220
184
221
# return chops
185
- return sum_count ([ self . boundary_layer_thickness , buffer_size , remaining_length ], chops )
222
+ return 1
186
223
187
- def get_chops (self , count : int , length : float ) -> List [Chop ]:
224
+ def get_chops (self , count , length , size_before = 0 , size_after = 0 ) -> List [Chop ]:
188
225
raise NotImplementedError ("TODO!" )
0 commit comments