@@ -73,45 +73,47 @@ def find_fluxoid_solution(
73
73
"""
74
74
device = model .device
75
75
fluxoids = fluxoids or {}
76
- holes = list (device .holes )
76
+ hole_names = list (device .holes )
77
77
current_units = model .current_units
78
+ inductance_units = f"Phi_0 / { current_units } "
78
79
solve_kwargs = solve_kwargs .copy ()
79
80
applied_field = solve_kwargs .pop ("applied_field" , None )
80
- target_fluxoids = np .array ([fluxoids .get (name , 0 ) for name in holes ])
81
-
82
- model = model .copy ()
83
- model .circulating_currents = {}
84
-
85
- # Find the hole fluxoids assuming no circulating currents.
86
- solution_no_circ = solve (
87
- model = model ,
88
- applied_field = applied_field ,
89
- ** solve_kwargs ,
90
- )[- 1 ]
91
-
92
- if not holes :
93
- if np .any (target_fluxoids ):
94
- raise ValueError (
95
- "Cannot calculate nonzero fluxoid solution for a device with no holes."
96
- )
97
- return solution_no_circ
98
-
99
- fluxoids = [
100
- sum (solution_no_circ .hole_fluxoid (name )).to ("Phi_0" ).magnitude for name in holes
101
- ]
102
- fluxoids = np .array (fluxoids )
103
-
104
- M = device .mutual_inductance_matrix (
105
- units = f"Phi_0 / { current_units } " , ** solve_kwargs
106
- )
107
- # Solve for the circulating currents needed to realize the target_fluxoids.
108
- I_circ = np .linalg .solve (M .magnitude , target_fluxoids - fluxoids )
109
- circulating_currents = dict (zip (holes , I_circ ))
110
- # Solve the model with the optimized circulating currents.
111
- model .set_circulating_currents (circulating_currents )
112
- solution = solve (
113
- model = model ,
114
- applied_field = applied_field ,
115
- ** solve_kwargs ,
116
- )[- 1 ]
81
+ target_fluxoids = np .array ([fluxoids .get (name , 0 ) for name in hole_names ])
82
+
83
+ orig_circulating_currents = model .circulating_currents
84
+ try :
85
+ # Find the hole fluxoids assuming no circulating currents.
86
+ model .set_circulating_currents ({name : 0 for name in hole_names })
87
+ solution_no_circ = solve (
88
+ model = model ,
89
+ applied_field = applied_field ,
90
+ ** solve_kwargs ,
91
+ )[- 1 ]
92
+
93
+ if not hole_names :
94
+ if np .any (target_fluxoids ):
95
+ raise ValueError (
96
+ "Cannot calculate nonzero fluxoid solution for a device with no holes."
97
+ )
98
+ return solution_no_circ
99
+
100
+ fluxoids = [
101
+ sum (solution_no_circ .hole_fluxoid (name )).to ("Phi_0" ).magnitude
102
+ for name in hole_names
103
+ ]
104
+ fluxoids = np .array (fluxoids )
105
+ M = device .mutual_inductance_matrix (units = inductance_units , ** solve_kwargs )
106
+
107
+ # Solve for the circulating currents needed to realize the target_fluxoids.
108
+ I_circ = np .linalg .solve (M .magnitude , target_fluxoids - fluxoids )
109
+ circulating_currents = dict (zip (hole_names , I_circ ))
110
+ # Solve the model with the optimized circulating currents.
111
+ model .set_circulating_currents (circulating_currents )
112
+ solution = solve (
113
+ model = model ,
114
+ applied_field = applied_field ,
115
+ ** solve_kwargs ,
116
+ )[- 1 ]
117
+ finally :
118
+ model .set_circulating_currents (orig_circulating_currents )
117
119
return solution
0 commit comments