-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPyMKF.cpp
3891 lines (3536 loc) · 167 KB
/
PyMKF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "pybind11_json/pybind11_json.hpp"
#include <magic_enum.hpp>
#include "json.hpp"
#include "BobbinWrapper.h"
#include "CoreWrapper.h"
#include "CoilWrapper.h"
#include "WireWrapper.h"
#include "MasWrapper.h"
#include "InitialPermeability.h"
#include "InputsWrapper.h"
#include <MAS.hpp>
#include "Defaults.h"
#include "Constants.h"
#include "MagneticEnergy.h"
#include "MagneticWrapper.h"
#include "Reluctance.h"
#include "Temperature.h"
#include "MagnetizingInductance.h"
#include "CoreLosses.h"
#include "MagneticSimulator.h"
#include "Resistivity.h"
#include "CoreTemperature.h"
#include "CircuitSimulatorInterface.h"
#include "Utils.h"
#include "Settings.h"
#include "MagneticAdviser.h"
#include "LeakageInductance.h"
#include "Insulation.h"
#include "NumberTurns.h"
#include "MagneticSimulator.h"
#include "WindingOhmicLosses.h"
#include "WindingSkinEffectLosses.h"
#include "CircuitSimulatorInterface.h"
using json = nlohmann::json;
using ordered_json = nlohmann::ordered_json;
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
namespace py = pybind11;
std::map<std::string, OpenMagnetics::MasWrapper> masDatabase;
void load_databases(json databasesJson) {
OpenMagnetics::load_databases(databasesJson, true);
}
std::string read_databases(std::string path, bool addInternalData) {
try {
auto masPath = std::filesystem::path{path};
json data;
std::string line;
{
data["coreMaterials"] = json();
std::ifstream coreMaterials(masPath.append("core_materials.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["coreMaterials"][jf["name"]] = jf;
}
}
{
data["coreShapes"] = json();
std::ifstream coreMaterials(masPath.append("core_shapes.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["coreShapes"][jf["name"]] = jf;
}
}
{
data["wires"] = json();
std::ifstream coreMaterials(masPath.append("wires.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["wires"][jf["name"]] = jf;
}
}
{
data["bobbins"] = json();
std::ifstream coreMaterials(masPath.append("bobbins.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["bobbins"][jf["name"]] = jf;
}
}
{
data["insulationMaterials"] = json();
std::ifstream coreMaterials(masPath.append("insulation_materials.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["insulationMaterials"][jf["name"]] = jf;
}
}
{
data["wireMaterials"] = json();
std::ifstream coreMaterials(masPath.append("wire_materials.ndjson"));
while (getline (coreMaterials, line)) {
json jf = json::parse(line);
data["wireMaterials"][jf["name"]] = jf;
}
}
OpenMagnetics::load_databases(data, true, addInternalData);
return "0";
}
catch (const std::exception &exc) {
return std::string{exc.what()};
}
}
std::string load_mas(std::string key, json masJson, bool expand) {
try {
OpenMagnetics::MasWrapper mas(masJson);
if (expand) {
mas.set_magnetic(OpenMagnetics::MasWrapper::expand_magnetic(mas.get_mutable_magnetic()));
mas.set_inputs(OpenMagnetics::MasWrapper::expand_inputs(mas.get_mutable_magnetic(), mas.get_mutable_inputs()));
}
masDatabase[key] = mas;
return std::to_string(masDatabase.size());
}
catch (const std::exception &exc) {
return std::string{exc.what()};
}
}
std::string load_magnetic(std::string key, json magneticJson, bool expand) {
try {
OpenMagnetics::MagneticWrapper magnetic(magneticJson);
if (expand) {
magnetic = OpenMagnetics::MasWrapper::expand_magnetic(magnetic);
}
OpenMagnetics::MasWrapper mas;
mas.set_magnetic(magnetic);
masDatabase[key] = mas;
return std::to_string(masDatabase.size());
}
catch (const std::exception &exc) {
return std::string{exc.what()};
}
}
std::string load_magnetics(std::string keys, json magneticJsons, bool expand) {
try {
json keysJson = json::parse(keys);
for (size_t magneticIndex = 0; magneticIndex < magneticJsons.size(); magneticIndex++) {
OpenMagnetics::MagneticWrapper magnetic(magneticJsons[magneticIndex]);
if (expand) {
magnetic = OpenMagnetics::MasWrapper::expand_magnetic(magnetic);
}
OpenMagnetics::MasWrapper mas;
mas.set_magnetic(magnetic);
masDatabase[to_string(keysJson[magneticIndex])] = mas;
}
return std::to_string(masDatabase.size());
}
catch (const std::exception &exc) {
return std::string{exc.what()};
}
}
// std::string load_magnetics_from_file(std::string path, char separator, size_t magneticIndex, size_t referenceIndex, std::string manufacturerName, bool expand) {
// try {
// std::ifstream in(path);
// // OpenMagnetics::InputsWrapper inputs(inputsJson);
// bool isHeader = true;
// std::cout << "Mierda 1" << std::endl;
// if (in) {
// std::string line;
// std::cout << "Mierda 2" << std::endl;
// while (getline(in, line)) {
// if (isHeader) {
// std::cout << "isHeader" << std::endl;
// isHeader = false;
// continue;
// }
// std::stringstream sep(line);
// std::string field;
// std::cout << "Mierda 3" << std::endl;
// std::vector<std::string> row_data;
// while (getline(sep, field, separator)) {
// std::cout << field << std::endl;
// row_data.push_back(field);
// }
// std::cout << "Mierda 4" << std::endl;
// OpenMagnetics::MagneticWrapper magnetic(json::parse(row_data[magneticIndex]));
// OpenMagnetics::MagneticManufacturerInfo manufacturerInfo;
// manufacturerInfo.set_name(manufacturerName);
// manufacturerInfo.set_reference(row_data[referenceIndex]);
// magnetic.set_manufacturer_info(manufacturerInfo);
// if (expand) {
// magnetic = OpenMagnetics::MasWrapper::expand_magnetic(magnetic);
// // inputs = OpenMagnetics::MasWrapper::expand_inputs(magnetic, inputs);
// }
// OpenMagnetics::MasWrapper mas;
// mas.set_magnetic(magnetic);
// // mas.set_inputs(inputs);
// masDatabase[row_data[referenceIndex]] = mas;
// }
// }
// return std::to_string(masDatabase.size());
// }
// catch (const std::exception &exc) {
// return std::string{exc.what()};
// }
// }
json read_mas(std::string key) {
json result;
to_json(result, masDatabase[key]);
return result;
}
/**
* @brief Retrieves a dictionary of constant values used in the OpenMagnetics library.
*
* This function creates a dictionary (py::dict) and populates it with various constant values
* from the OpenMagnetics::Constants class. These constants include physical constants,
* magnetic properties, and other parameters used in magnetic field calculations.
*
* @return py::dict A dictionary containing the following key-value pairs:
* - "residualGap": The residual gap value.
* - "minimumNonResidualGap": The minimum non-residual gap value.
* - "vacuumPermeability": The permeability of vacuum.
* - "vacuumPermittivity": The permittivity of vacuum.
* - "magneticFluxDensitySaturation": The saturation magnetic flux density.
* - "spacerProtudingPercentage": The percentage of spacer protruding.
* - "coilPainterScale": The scale factor for coil painting.
* - "minimumDistributedFringingFactor": The minimum distributed fringing factor.
* - "maximumDistributedFringingFactor": The maximum distributed fringing factor.
* - "initialGapLengthForSearching": The initial gap length for searching.
* - "roshenMagneticFieldStrengthStep": The step size for Roshen magnetic field strength.
* - "foilToSectionMargin": The margin between foil and section.
* - "planarToSectionMargin": The margin between planar and section.
*/
py::dict get_constants() {
auto constants = OpenMagnetics::Constants();
py::dict constantsMap;
constantsMap["residualGap"] = constants.residualGap;
constantsMap["minimumNonResidualGap"] = constants.minimumNonResidualGap;
constantsMap["vacuumPermeability"] = constants.vacuumPermeability;
constantsMap["vacuumPermittivity"] = constants.vacuumPermittivity;
constantsMap["magneticFluxDensitySaturation"] = constants.magneticFluxDensitySaturation;
constantsMap["spacerProtudingPercentage"] = constants.spacerProtudingPercentage;
constantsMap["coilPainterScale"] = constants.coilPainterScale;
constantsMap["minimumDistributedFringingFactor"] = constants.minimumDistributedFringingFactor;
constantsMap["maximumDistributedFringingFactor"] = constants.maximumDistributedFringingFactor;
constantsMap["initialGapLengthForSearching"] = constants.initialGapLengthForSearching;
constantsMap["roshenMagneticFieldStrengthStep"] = constants.roshenMagneticFieldStrengthStep;
constantsMap["foilToSectionMargin"] = constants.foilToSectionMargin;
constantsMap["planarToSectionMargin"] = constants.planarToSectionMargin;
return constantsMap;
}
/**
* @brief Retrieves the default configuration values as a Python dictionary.
*
* This function creates an instance of the OpenMagnetics::Defaults class and converts its
* properties to a Python dictionary using the pybind11 library. The dictionary contains
* various default settings related to core losses, temperature models, magnetic field
* strength, and other parameters used in the OpenMagnetics framework.
*
* @return py::dict A dictionary containing the default configuration values.
*/
py::dict get_defaults() {
auto defaults = OpenMagnetics::Defaults();
py::dict defaultsMap;
json aux;
to_json(aux, defaults.coreLossesModelDefault);
to_json(aux, defaults.coreLossesModelDefault);
defaultsMap["coreLossesModelDefault"] = aux;
to_json(aux, defaults.coreTemperatureModelDefault);
defaultsMap["coreTemperatureModelDefault"] = aux;
to_json(aux, defaults.reluctanceModelDefault);
defaultsMap["reluctanceModelDefault"] = aux;
to_json(aux, defaults.magneticFieldStrengthModelDefault);
defaultsMap["magneticFieldStrengthModelDefault"] = aux;
to_json(aux, defaults.magneticFieldStrengthFringingEffectModelDefault);
defaultsMap["magneticFieldStrengthFringingEffectModelDefault"] = aux;
defaultsMap["maximumProportionMagneticFluxDensitySaturation"] = defaults.maximumProportionMagneticFluxDensitySaturation;
defaultsMap["coreAdviserFrequencyReference"] = defaults.coreAdviserFrequencyReference;
defaultsMap["coreAdviserMagneticFluxDensityReference"] = defaults.coreAdviserMagneticFluxDensityReference;
defaultsMap["coreAdviserThresholdValidity"] = defaults.coreAdviserThresholdValidity;
defaultsMap["coreAdviserMaximumCoreTemperature"] = defaults.coreAdviserMaximumCoreTemperature;
defaultsMap["coreAdviserMaximumPercentagePowerCoreLosses"] = defaults.coreAdviserMaximumPercentagePowerCoreLosses;
defaultsMap["coreAdviserMaximumMagneticsAfterFiltering"] = defaults.coreAdviserMaximumMagneticsAfterFiltering;
defaultsMap["coreAdviserMaximumNumberStacks"] = defaults.coreAdviserMaximumNumberStacks;
defaultsMap["maximumCurrentDensity"] = defaults.maximumCurrentDensity;
defaultsMap["maximumEffectiveCurrentDensity"] = defaults.maximumEffectiveCurrentDensity;
defaultsMap["maximumNumberParallels"] = defaults.maximumNumberParallels;
defaultsMap["magneticFluxDensitySaturation"] = defaults.magneticFluxDensitySaturation;
defaultsMap["magnetizingInductanceThresholdValidity"] = defaults.magnetizingInductanceThresholdValidity;
defaultsMap["harmonicAmplitudeThreshold"] = defaults.harmonicAmplitudeThreshold;
defaultsMap["ambientTemperature"] = defaults.ambientTemperature;
defaultsMap["measurementFrequency"] = defaults.measurementFrequency;
defaultsMap["magneticFieldMirroringDimension"] = defaults.magneticFieldMirroringDimension;
defaultsMap["maximumCoilPattern"] = defaults.maximumCoilPattern;
to_json(aux, defaults.defaultRoundWindowSectionsOrientation);
defaultsMap["defaultRoundWindowSectionsOrientation"] = aux;
to_json(aux, defaults.defaultRectangularWindowSectionsOrientation);
defaultsMap["defaultRectangularWindowSectionsOrientation"] = aux;
defaultsMap["defaultEnamelledInsulationMaterial"] = defaults.defaultEnamelledInsulationMaterial;
defaultsMap["defaultInsulationMaterial"] = defaults.defaultInsulationMaterial;
defaultsMap["defaultLayerInsulationMaterial"] = defaults.defaultLayerInsulationMaterial;
defaultsMap["overlappingFactorSurroundingTurns"] = defaults.overlappingFactorSurroundingTurns;
to_json(aux, defaults.commonWireStandard);
defaultsMap["commonWireStandard"] = aux;
return defaultsMap;
}
/**
* @brief Retrieves a list of core materials.
*
* This function calls the OpenMagnetics::get_materials function to obtain a list of core materials.
* It then converts each material to a JSON object and adds it to a JSON array.
* If an exception occurs during this process, it catches the exception and returns a JSON object
* containing the exception message.
*
* @return json A JSON array of core materials or a JSON object containing an exception message.
*/
json get_core_materials() {
try {
auto materials = OpenMagnetics::get_materials(std::nullopt);
json result = json::array();
for (auto elem: materials) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the initial permeability of a given material based on specified conditions.
*
* This function finds the core material by its name and calculates its initial permeability
* based on the provided temperature, magnetic field DC bias, and frequency.
*
* @param materialName The name of the material in JSON format.
* @param temperature The temperature at which the permeability is to be calculated.
* @param magneticFieldDcBias The DC bias of the magnetic field.
* @param frequency The frequency at which the permeability is to be calculated.
* @return The initial permeability of the material as a double.
* @throws std::exception If an error occurs during the process, an exception is caught and its message is returned in JSON format.
*/
double get_material_permeability(json materialName, double temperature, double magneticFieldDcBias, double frequency) {
try {
auto materialData = OpenMagnetics::find_core_material_by_name(materialName);
OpenMagnetics::InitialPermeability initialPermeability;
return initialPermeability.get_initial_permeability(materialData, temperature, magneticFieldDcBias, frequency);
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the resistivity of a given material at a specified temperature.
*
* This function uses the OpenMagnetics library to find the core material by name and then
* calculates its resistivity at the given temperature using the appropriate resistivity model.
*
* @param materialName A JSON object containing the name of the material.
* @param temperature The temperature at which to calculate the resistivity.
* @return The resistivity of the material at the specified temperature.
* @throws std::exception If an error occurs while retrieving the material data or calculating resistivity.
*/
double get_material_resistivity(json materialName, double temperature) {
try {
auto materialData = OpenMagnetics::find_core_material_by_name(materialName);
auto resistivityModel = OpenMagnetics::ResistivityModel::factory(OpenMagnetics::ResistivityModels::CORE_MATERIAL);
return (*resistivityModel).get_resistivity(materialData, temperature);
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the Steinmetz coefficients for a given core material at a specified frequency.
*
* This function calls the OpenMagnetics::CoreLossesModel::get_steinmetz_coefficients method to obtain
* the Steinmetz coefficients for the specified core material and frequency. The result is then converted
* to a JSON object and returned. If an exception occurs during the process, the exception message is
* caught and returned as a JSON object.
*
* @param materialName The name of the core material for which the Steinmetz coefficients are to be retrieved.
* @param frequency The frequency at which the Steinmetz coefficients are to be calculated.
* @return A JSON object containing the Steinmetz coefficients or an exception message if an error occurs.
*/
json get_core_material_steinmetz_coefficients(json materialName, double frequency) {
try {
auto steinmetzCoreLossesMethodRangeDatum = OpenMagnetics::CoreLossesModel::get_steinmetz_coefficients(materialName, frequency);
json result;
to_json(result, steinmetzCoreLossesMethodRangeDatum);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves core shapes and converts them to JSON format.
*
* This function calls the OpenMagnetics::get_shapes function to obtain a list of core shapes.
* It then converts each shape to a JSON object and adds it to a JSON array.
* If an exception occurs during this process, the exception message is caught and returned in JSON format.
*
* @return A JSON array containing the core shapes, or a JSON object with an exception message if an error occurs.
*/
json get_core_shapes() {
try {
auto shapes = OpenMagnetics::get_shapes(true);
json result = json::array();
for (auto elem : shapes) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves a list of unique core shape families.
*
* This function fetches all available core shapes using the OpenMagnetics library,
* extracts their families, and returns a JSON array of unique core shape families.
* If an exception occurs during the process, it catches the exception and returns
* a JSON object containing the exception message.
*
* @return json A JSON array of unique core shape families or a JSON object with an exception message.
*/
json get_core_shape_families() {
try {
auto shapes = OpenMagnetics::get_shapes(false);
std::vector<OpenMagnetics::CoreShapeFamily> families;
json result = json::array();
for (auto elem : shapes) {
auto family = elem.get_family();
if (std::find(families.begin(), families.end(), family) == families.end()) {
families.push_back(family);
json aux;
OpenMagnetics::to_json(aux, family);
result.push_back(aux);
}
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves a list of wires and converts them to JSON format.
*
* This function calls the OpenMagnetics::get_wires() function to obtain a list of wires.
* It then converts each wire to a JSON object using the OpenMagnetics::to_json() function
* and stores them in a JSON array. If an exception occurs during this process, it catches
* the exception and returns a JSON object containing the exception message.
*
* @return A JSON array containing the wire data, or a JSON object with an exception message if an error occurs.
*/
json get_wires() {
try {
auto wires = OpenMagnetics::get_wires();
json result = json::array();
for (auto elem : wires) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves a list of bobbins in JSON format.
*
* This function calls the OpenMagnetics::get_bobbins() function to get a list of bobbins,
* converts each bobbin to a JSON object using OpenMagnetics::to_json(), and returns the
* list of JSON objects. If an exception occurs during the process, it catches the exception
* and returns a JSON object containing the exception message.
*
* @return json A JSON array of bobbins or a JSON object with exception details.
*/
json get_bobbins() {
try {
auto bobbins = OpenMagnetics::get_bobbins();
json result = json::array();
for (auto elem : bobbins) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves a list of insulation materials.
*
* This function calls the OpenMagnetics::get_insulation_materials() function to obtain a list of insulation materials.
* It then converts each material to a JSON object and adds it to a JSON array.
* If an exception occurs during this process, the exception message is caught and returned as a JSON object.
*
* @return A JSON array containing the insulation materials, or a JSON object with an exception message if an error occurs.
*/
json get_insulation_materials() {
try {
auto insulationMaterials = OpenMagnetics::get_insulation_materials();
json result = json::array();
for (auto elem : insulationMaterials) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the wire materials and converts them to JSON format.
*
* This function calls the OpenMagnetics::get_wire_materials() function to obtain a list of wire materials.
* It then converts each wire material to a JSON object and adds it to a JSON array.
* If an exception occurs during this process, it catches the exception and returns a JSON object containing the exception message.
*
* @return A JSON array containing the wire materials, or a JSON object with an exception message if an error occurs.
*/
json get_wire_materials() {
try {
auto wireMaterials = OpenMagnetics::get_wire_materials();
json result = json::array();
for (auto elem : wireMaterials) {
json aux;
OpenMagnetics::to_json(aux, elem);
result.push_back(aux);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of core materials.
*
* This function calls the OpenMagnetics::get_material_names function to obtain a list of core material names.
* It then converts this list into a JSON array and returns it.
*
* @return json A JSON array containing the names of core materials. If an exception occurs, a JSON object with the exception message is returned.
*
* @throws std::exception If an error occurs while retrieving the material names.
*/
json get_core_material_names() {
try {
auto materialNames = OpenMagnetics::get_material_names(std::nullopt);
json result = json::array();
for (auto elem : materialNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the core material names by manufacturer.
*
* This function fetches the core material names associated with a given manufacturer
* and returns them in a JSON array. If an exception occurs during the process,
* it catches the exception and returns a JSON object containing the exception message.
*
* @param manufacturerName The name of the manufacturer whose core material names are to be retrieved.
* @return A JSON array containing the core material names. If an exception occurs,
* a JSON object with the exception message is returned.
*/
json get_core_material_names_by_manufacturer(std::string manufacturerName) {
try {
auto materialNames = OpenMagnetics::get_material_names(manufacturerName);
json result = json::array();
for (auto elem : materialNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of core shapes.
*
* This function fetches the names of core shapes based on the specified settings.
* It can include or exclude toroidal cores based on the input parameter.
*
* @param includeToroidal A boolean flag indicating whether to include toroidal cores in the result.
* @return A JSON array containing the names of the core shapes. If an exception occurs,
* a JSON object with the exception message is returned.
*/
json get_core_shape_names(bool includeToroidal) {
try {
auto settings = OpenMagnetics::Settings::GetInstance();
settings->set_use_toroidal_cores(includeToroidal);
auto shapeNames = OpenMagnetics::get_shape_names();
json result = json::array();
for (auto elem : shapeNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of wires.
*
* This function calls the OpenMagnetics::get_wire_names() function to obtain a list of wire names.
* It then converts this list into a JSON array and returns it.
*
* @return json A JSON array containing the names of wires. If an exception occurs, a JSON object
* with the exception message is returned.
*/
json get_wire_names() {
try {
auto wireNames = OpenMagnetics::get_wire_names();
json result = json::array();
for (auto elem : wireNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of bobbins.
*
* This function calls the OpenMagnetics::get_bobbin_names() function to get a list of bobbin names.
* It then converts this list into a JSON array and returns it.
*
* @return json A JSON array containing the names of bobbins. If an exception occurs, a JSON object
* containing the exception message is returned.
*/
json get_bobbin_names() {
try {
auto bobbinNames = OpenMagnetics::get_bobbin_names();
json result = json::array();
for (auto elem : bobbinNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of insulation materials.
*
* This function calls the OpenMagnetics::get_insulation_material_names() function to get a list of insulation material names.
* It then converts the list into a JSON array and returns it.
* If an exception occurs during the process, it catches the exception and returns a JSON object containing the exception message.
*
* @return json A JSON array containing the names of insulation materials, or a JSON object with an exception message if an error occurs.
*/
json get_insulation_material_names() {
try {
auto insulationMaterialNames = OpenMagnetics::get_insulation_material_names();
json result = json::array();
for (auto elem : insulationMaterialNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Retrieves the names of wire materials.
*
* This function calls the OpenMagnetics::get_wire_material_names() function to obtain a list of wire material names.
* It then converts this list into a JSON array and returns it.
* If an exception occurs during the process, it catches the exception and returns a JSON object containing the exception message.
*
* @return json A JSON array containing the names of wire materials, or a JSON object with an exception message if an error occurs.
*/
json get_wire_material_names() {
try {
auto wireMaterialNames = OpenMagnetics::get_wire_material_names();
json result = json::array();
for (auto elem : wireMaterialNames) {
result.push_back(elem);
}
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds core material data by name.
*
* This function searches for core material data using the provided material name.
* It utilizes the OpenMagnetics library to perform the search and converts the
* resulting data to a JSON format.
*
* @param materialName A JSON object containing the name of the material to search for.
* @return A JSON object containing the core material data if found, or an exception message if an error occurs.
*/
json find_core_material_by_name(json materialName) {
try {
auto materialData = OpenMagnetics::find_core_material_by_name(materialName);
json result;
to_json(result, materialData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds core shape data by name.
*
* This function attempts to find the core shape data corresponding to the given shape name.
* If successful, it converts the shape data to a JSON object and returns it.
* If an exception occurs during the process, it catches the exception and returns a JSON object
* containing the exception message.
*
* @param shapeName The name of the core shape to find.
* @return A JSON object containing the core shape data if found, or an exception message if an error occurs.
*/
json find_core_shape_by_name(json shapeName) {
try {
auto shapeData = OpenMagnetics::find_core_shape_by_name(shapeName);
json result;
to_json(result, shapeData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds wire data by its name.
*
* This function searches for wire data using the provided wire name.
* If the wire is found, it returns the wire data in JSON format.
* If an exception occurs during the search, it catches the exception
* and returns a JSON object containing the exception message.
*
* @param wireName The name of the wire to search for, in JSON format.
* @return A JSON object containing the wire data if found, or an exception message if an error occurs.
*/
json find_wire_by_name(json wireName) {
try {
auto wireData = OpenMagnetics::find_wire_by_name(wireName);
json result;
to_json(result, wireData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds bobbin data by its name.
*
* This function attempts to find the bobbin data corresponding to the given bobbin name.
* It uses the OpenMagnetics library to perform the search and converts the result to a JSON object.
* If an exception occurs during the search, it catches the exception and returns a JSON object
* containing the exception message.
*
* @param bobbinName The name of the bobbin to search for, provided as a JSON object.
* @return A JSON object containing the bobbin data if found, or an exception message if an error occurs.
*/
json find_bobbin_by_name(json bobbinName) {
try {
auto bobbinData = OpenMagnetics::find_bobbin_by_name(bobbinName);
json result;
to_json(result, bobbinData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds insulation material data by its name.
*
* This function searches for insulation material data using the provided name.
* It returns the data in JSON format if found, or an exception message in JSON format if an error occurs.
*
* @param insulationMaterialName The name of the insulation material to search for, in JSON format.
* @return A JSON object containing the insulation material data if found, or an exception message if an error occurs.
*/
json find_insulation_material_by_name(json insulationMaterialName) {
try {
auto insulationMaterialData = OpenMagnetics::find_insulation_material_by_name(insulationMaterialName);
json result;
to_json(result, insulationMaterialData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Finds wire material data by name.
*
* This function searches for wire material data using the provided wire material name.
* It utilizes the OpenMagnetics library to perform the search and converts the result
* to a JSON object.
*
* @param wireMaterialName A JSON object containing the name of the wire material to search for.
* @return A JSON object containing the wire material data if found, or an exception message if an error occurs.
*/
json find_wire_material_by_name(json wireMaterialName) {
try {
auto wireMaterialData = OpenMagnetics::find_wire_material_by_name(wireMaterialName);
json result;
to_json(result, wireMaterialData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
json find_wire_by_dimension(double dimension, json wireTypeJson, json wireStandardJson) {
try {
OpenMagnetics::WireType wireType;
from_json(wireTypeJson, wireType);
OpenMagnetics::WireStandard wireStandard;
from_json(wireStandardJson, wireStandard);
auto wireMaterialData = OpenMagnetics::find_wire_by_dimension(dimension, wireType, wireStandard, false);
json result;
to_json(result, wireMaterialData);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Creates a basic bobbin from the given core data.
*
* This function takes a JSON object containing core data and a boolean flag indicating whether to use null dimensions.
* It creates a core object using the provided core data and then creates a bobbin using the core object.
* The resulting bobbin is converted to a JSON object and returned.
* If an exception occurs during the process, the exception message is caught and returned as a JSON object.
*
* @param coreDataJson A JSON object containing the core data.
* @param nullDimensions A boolean flag indicating whether to use null dimensions.
* @return A JSON object representing the created bobbin, or an exception message if an error occurs.
*/
json create_basic_bobbin(json coreDataJson, bool nullDimensions){
try {
OpenMagnetics::CoreWrapper core(coreDataJson, false, false, false);
auto bobbin = OpenMagnetics::BobbinWrapper::create_quick_bobbin(core, nullDimensions);
json result;
to_json(result, bobbin);
return result;
}
catch (const std::exception &exc) {
json exception;
exception["data"] = "Exception: " + std::string{exc.what()};
return exception;
}
}
/**
* @brief Processes core data and returns the processed description.
*
* This function takes a JSON object containing core data, processes it using the OpenMagnetics::CoreWrapper,
* and returns the processed description as a JSON object. If an exception occurs during processing,
* it catches the exception and returns a JSON object containing the exception message.
*
* @param coreDataJson A JSON object containing the core data to be processed.
* @return A JSON object containing the processed core description or an exception message.
*/
json calculate_core_processed_description(json coreDataJson){
try {
OpenMagnetics::CoreWrapper core(coreDataJson, false, false, false);
core.process_data();
json result;
to_json(result, core.get_processed_description().value());
return result;
}
catch (const std::exception &exc) {