@@ -193,8 +193,14 @@ def sutherland_formula(T, b, S):
193193
194194 coefficients_dict = {}
195195 if kwargs .get ("kxx" ) is None :
196- pool = multiprocessing .Pool ()
197- coefficients_dict_list = pool .map (self .run , self .frequency )
196+ # Use multiprocessing only when beneficial (>2 frequencies)
197+ # For small workloads, sequential execution avoids process spawn overhead
198+ if len (self .frequency ) > 2 :
199+ with multiprocessing .Pool () as pool :
200+ coefficients_dict_list = pool .map (self .run , self .frequency )
201+ else :
202+ coefficients_dict_list = [self .run (freq ) for freq in self .frequency ]
203+
198204 coefficients_dict = {k : [] for k in coefficients_dict_list [0 ].keys ()}
199205 for d in coefficients_dict_list :
200206 for k in coefficients_dict :
@@ -216,6 +222,12 @@ def run(self, frequency):
216222 self .omega = frequency
217223 self .area = np .pi * 2.0 * self .radius * self .clearance
218224
225+ # Cache constants for form_rhs() optimization
226+ self ._gamma_R = self .gamma * self .R
227+ self ._radius_omega = self .radius * self .omega
228+ self ._rough_factor = 1.0e4 * self .roughness
229+ self ._mu_factor = 5.0e5
230+
219231 try :
220232 base_state_results = self .calculate_leakage ()
221233 if not base_state_results :
@@ -292,32 +304,40 @@ def form_rhs(self, mz2, T, mt):
292304 if mz2 <= 0 :
293305 mz2 = 1e-9
294306
295- mz , mt2 = np .sqrt (mz2 ), mt ** 2
296- c = np .sqrt (self .gamma * self .R * T )
307+ # Optimized: cache repeated calculations
308+ mz = np .sqrt (mz2 )
309+ mt2 = mt ** 2
310+ c = np .sqrt (self ._gamma_R * T )
297311 u = mz * c
298312 if u == 0 :
299313 u = 1e-9
300- w , rho = mt * c , self .mdot / (self .area * u )
301- Romega = self .radius * self .omega
314+ w = mt * c
315+ rho = self .mdot / (self .area * u )
316+ Romega = self ._radius_omega
302317 mr = Romega / c if c > 0 else 0
303- utot = np .sqrt (u ** 2 + w ** 2 ) / 2.0
304- utot_rotor = np .sqrt (u ** 2 + (w - Romega ) ** 2 )
318+ u2 = u ** 2
319+ w_minus_Romega = w - Romega
320+ utot = np .sqrt (u2 + w ** 2 ) * 0.5
321+ utot_rotor = np .sqrt (u2 + w_minus_Romega ** 2 )
305322 if utot == 0 :
306323 utot = 1e-9
307324 if utot_rotor == 0 :
308325 utot_rotor = 1e-9
309- mu = self .b_suther * T ** 1.5 / (self .s_suther + T )
310- fs_term = (5.0e5 * mu ) / (rho * self .clearance * utot )
311- fs = (1.375e-3 ) * (1.0 + fs_term ** (1.0 / 3.0 ))
326+ # Pre-compute T**1.5 and shared divisor
327+ T_15 = T ** 1.5
328+ mu = self .b_suther * T_15 / (self .s_suther + T )
329+ mu_factor_mu = self ._mu_factor * mu
330+ fs_term = mu_factor_mu / (rho * self .clearance * utot )
331+ fs = 1.375e-3 * (1.0 + fs_term ** (1.0 / 3.0 ))
312332 fs_geom = (
313333 np .sqrt (1.0 + mt2 / mz2 ) / (4.0 * self .clearance ) * fs
314334 if self .clearance > 0
315335 else 0
316336 )
317- fr_term = 1.0e4 * self .roughness + ( 5.0e5 * mu ) / (
337+ fr_term = self ._rough_factor + mu_factor_mu / (
318338 rho * self .clearance * utot_rotor
319339 )
320- fr = ( 1.375e-3 ) * (1.0 + fr_term ** (1.0 / 3.0 ))
340+ fr = 1.375e-3 * (1.0 + fr_term ** (1.0 / 3.0 ))
321341 fr_geom = (
322342 np .sqrt (1.0 + (mt - mr ) ** 2 / mz2 ) / self .clearance * fr
323343 if self .clearance > 0
0 commit comments