Skip to content

Commit 0e9901d

Browse files
committed
Added matrix calculations
1 parent c0c5128 commit 0e9901d

4 files changed

Lines changed: 232 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
44

55
[project]
66
name = "PyOpenMagnetics"
7-
version = "1.1.0"
7+
version = "1.1.1"
88
requires-python = ">=3.8"
99
authors = [
1010
{ name="Alfonso Martinez", email="Alfonso_VII@hotmail.com" },

src/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#include "physical_models/Resistivity.h"
2626
#include "physical_models/CoreTemperature.h"
2727
#include "physical_models/LeakageInductance.h"
28+
#include "physical_models/Inductance.h"
29+
#include "physical_models/StrayCapacitance.h"
2830
#include "physical_models/WindingOhmicLosses.h"
2931
#include "physical_models/WindingSkinEffectLosses.h"
32+
#include "physical_models/WindingLosses.h"
3033
#include "processors/Inputs.h"
3134
#include "processors/MagneticSimulator.h"
3235
#include "processors/CircuitSimulatorInterface.h"

src/simulation.cpp

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,126 @@ json extract_column_names(json fileJson) {
190190
}
191191
}
192192

193+
json calculate_inductance_matrix(json magneticJson, double frequency, json modelsData) {
194+
try {
195+
OpenMagnetics::Magnetic magnetic(magneticJson);
196+
197+
auto reluctanceModelName = OpenMagnetics::defaults.reluctanceModelDefault;
198+
if (!modelsData.is_null() && modelsData.find("reluctance") != modelsData.end()) {
199+
OpenMagnetics::from_json(modelsData["reluctance"], reluctanceModelName);
200+
}
201+
202+
OpenMagnetics::Inductance inductance(reluctanceModelName);
203+
auto inductanceMatrix = inductance.calculate_inductance_matrix(magnetic, frequency);
204+
205+
json result;
206+
to_json(result, inductanceMatrix);
207+
return result;
208+
}
209+
catch (const std::exception &exc) {
210+
json exception;
211+
exception["data"] = "Exception: " + std::string{exc.what()};
212+
return exception;
213+
}
214+
}
215+
216+
json calculate_leakage_inductance(json magneticJson, double frequency, size_t sourceIndex) {
217+
try {
218+
OpenMagnetics::Magnetic magnetic(magneticJson);
219+
220+
auto leakageInductanceOutput = OpenMagnetics::LeakageInductance().calculate_leakage_inductance_all_windings(magnetic, frequency, sourceIndex);
221+
222+
json result;
223+
to_json(result, leakageInductanceOutput);
224+
return result;
225+
}
226+
catch (const std::exception &exc) {
227+
json exception;
228+
exception["data"] = "Exception: " + std::string{exc.what()};
229+
return exception;
230+
}
231+
}
232+
233+
json calculate_dc_resistance_per_winding(json coilJson, double temperature) {
234+
try {
235+
OpenMagnetics::Coil coil(coilJson, false);
236+
237+
auto resistances = OpenMagnetics::WindingOhmicLosses::calculate_dc_resistance_per_winding(coil, temperature);
238+
239+
json result = resistances;
240+
return result;
241+
}
242+
catch (const std::exception &exc) {
243+
json exception;
244+
exception["data"] = "Exception: " + std::string{exc.what()};
245+
return exception;
246+
}
247+
}
248+
249+
json calculate_resistance_matrix(json magneticJson, double temperature, double frequency) {
250+
try {
251+
OpenMagnetics::Magnetic magnetic(magneticJson);
252+
253+
OpenMagnetics::WindingLosses windingLosses;
254+
auto resistanceMatrix = windingLosses.calculate_resistance_matrix(magnetic, temperature, frequency);
255+
256+
json result;
257+
to_json(result, resistanceMatrix);
258+
return result;
259+
}
260+
catch (const std::exception &exc) {
261+
json exception;
262+
exception["data"] = "Exception: " + std::string{exc.what()};
263+
return exception;
264+
}
265+
}
266+
267+
json calculate_stray_capacitance(json coilJson, json operatingPointJson, json modelsData) {
268+
try {
269+
OpenMagnetics::Coil coil(coilJson, false);
270+
OperatingPoint operatingPoint(operatingPointJson);
271+
272+
auto strayCapacitanceModelName = OpenMagnetics::StrayCapacitanceModels::ALBACH;
273+
if (!modelsData.is_null() && modelsData.find("strayCapacitance") != modelsData.end()) {
274+
OpenMagnetics::from_json(modelsData["strayCapacitance"], strayCapacitanceModelName);
275+
}
276+
277+
OpenMagnetics::StrayCapacitance strayCapacitance(strayCapacitanceModelName);
278+
auto strayCapacitanceOutput = strayCapacitance.calculate_capacitance(coil);
279+
280+
json result;
281+
to_json(result, strayCapacitanceOutput);
282+
return result;
283+
}
284+
catch (const std::exception &exc) {
285+
json exception;
286+
exception["data"] = "Exception: " + std::string{exc.what()};
287+
return exception;
288+
}
289+
}
290+
291+
json calculate_maxwell_capacitance_matrix(json coilJson, json capacitanceAmongWindingsJson) {
292+
try {
293+
OpenMagnetics::Coil coil(coilJson, false);
294+
auto capacitanceAmongWindings = capacitanceAmongWindingsJson.get<std::map<std::string, std::map<std::string, double>>>();
295+
296+
auto maxwellMatrix = OpenMagnetics::StrayCapacitance::calculate_maxwell_capacitance_matrix(coil, capacitanceAmongWindings);
297+
298+
json result = json::array();
299+
for (const auto& matrix : maxwellMatrix) {
300+
json matrixJson;
301+
to_json(matrixJson, matrix);
302+
result.push_back(matrixJson);
303+
}
304+
return result;
305+
}
306+
catch (const std::exception &exc) {
307+
json exception;
308+
exception["data"] = "Exception: " + std::string{exc.what()};
309+
return exception;
310+
}
311+
}
312+
193313
void register_simulation_bindings(py::module& m) {
194314
m.def("simulate", &simulate,
195315
R"pbdoc(
@@ -304,6 +424,102 @@ void register_simulation_bindings(py::module& m) {
304424
Returns:
305425
JSON array of column name strings.
306426
)pbdoc");
427+
428+
m.def("calculate_inductance_matrix", &calculate_inductance_matrix,
429+
R"pbdoc(
430+
Calculate the complete inductance matrix for a magnetic component.
431+
432+
Computes the inductance matrix at the specified frequency, including
433+
self inductances (diagonal elements) and mutual inductances (off-diagonal).
434+
435+
Args:
436+
magnetic_json: JSON object containing magnetic component specification.
437+
frequency: Operating frequency in Hz.
438+
models_json: JSON object specifying which models to use (e.g., reluctance model).
439+
440+
Returns:
441+
JSON object with the inductance matrix at the specified frequency.
442+
)pbdoc");
443+
444+
m.def("calculate_leakage_inductance", &calculate_leakage_inductance,
445+
R"pbdoc(
446+
Calculate leakage inductance between windings.
447+
448+
Computes the leakage inductance from a source winding to all other windings.
449+
450+
Args:
451+
magnetic_json: JSON object containing magnetic component specification.
452+
frequency: Operating frequency in Hz.
453+
source_index: Index of the source winding (0-based).
454+
455+
Returns:
456+
JSON object with leakage inductance values to each winding.
457+
)pbdoc");
458+
459+
m.def("calculate_dc_resistance_per_winding", &calculate_dc_resistance_per_winding,
460+
R"pbdoc(
461+
Calculate DC resistance for each winding.
462+
463+
Computes the DC resistance of each winding based on wire properties
464+
and temperature.
465+
466+
Args:
467+
coil_json: JSON object containing coil specification with turns and wire info.
468+
temperature: Temperature in degrees Celsius for resistance calculation.
469+
470+
Returns:
471+
JSON array with DC resistance value for each winding in Ohms.
472+
)pbdoc");
473+
474+
m.def("calculate_resistance_matrix", &calculate_resistance_matrix,
475+
R"pbdoc(
476+
Calculate the resistance matrix for a magnetic component.
477+
478+
Computes the frequency-dependent resistance matrix including self and mutual
479+
resistances. Uses the Spreen (1990) method with inductance ratio scaling
480+
for proper mutual resistance calculation.
481+
482+
Args:
483+
magnetic_json: JSON object containing magnetic component specification.
484+
temperature: Temperature in degrees Celsius for resistance calculation.
485+
frequency: Operating frequency in Hz.
486+
487+
Returns:
488+
JSON object with resistance matrix (magnitude and frequency).
489+
)pbdoc");
490+
491+
m.def("calculate_stray_capacitance", &calculate_stray_capacitance,
492+
R"pbdoc(
493+
Calculate stray capacitance for a coil.
494+
495+
Computes turn-to-turn and winding-to-winding capacitances using the
496+
specified capacitance model.
497+
498+
Args:
499+
coil_json: JSON object containing coil specification with turns.
500+
operating_point_json: JSON object with operating conditions for voltage distribution.
501+
models_json: JSON object specifying capacitance model (e.g., "Albach", "Koch").
502+
503+
Returns:
504+
JSON object with capacitance values including capacitance among turns,
505+
capacitance among windings, and Maxwell capacitance matrix.
506+
)pbdoc");
507+
508+
m.def("calculate_maxwell_capacitance_matrix", &calculate_maxwell_capacitance_matrix,
509+
R"pbdoc(
510+
Calculate Maxwell capacitance matrix from inter-winding capacitances.
511+
512+
Converts inter-winding capacitance values to the standard Maxwell
513+
capacitance matrix format used in circuit simulation.
514+
515+
Args:
516+
coil_json: JSON object containing coil specification.
517+
capacitance_among_windings_json: JSON object with capacitance values
518+
between each pair of windings.
519+
520+
Returns:
521+
JSON array containing the Maxwell capacitance matrix.
522+
)pbdoc");
307523
}
308524

309525
} // namespace PyMKF

src/simulation.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ json extract_operating_point(json fileJson, size_t numberWindings, double freque
2020
json extract_map_column_names(json fileJson, size_t numberWindings, double frequency);
2121
json extract_column_names(json fileJson);
2222

23+
// Inductance calculations
24+
json calculate_inductance_matrix(json magneticJson, double frequency, json modelsData);
25+
json calculate_leakage_inductance(json magneticJson, double frequency, size_t sourceIndex);
26+
27+
// Resistance calculations
28+
json calculate_dc_resistance_per_winding(json coilJson, double temperature);
29+
json calculate_resistance_matrix(json magneticJson, double temperature, double frequency);
30+
31+
// Capacitance calculations
32+
json calculate_stray_capacitance(json coilJson, json operatingPointJson, json modelsData);
33+
json calculate_maxwell_capacitance_matrix(json coilJson, json capacitanceAmongWindingsJson);
34+
2335
void register_simulation_bindings(py::module& m);
2436

2537
} // namespace PyMKF

0 commit comments

Comments
 (0)