-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_magnetic.html
More file actions
360 lines (301 loc) · 17.2 KB
/
test_magnetic.html
File metadata and controls
360 lines (301 loc) · 17.2 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenMagnetics Virtual Builder - Magnetic Tests</title>
<style>
body { font-family: monospace; padding: 20px; background: #1a1a1a; color: #f0f0f0; }
.pass { color: #4caf50; }
.fail { color: #f44336; }
.info { color: #2196f3; }
.warn { color: #ff9800; }
pre { white-space: pre-wrap; margin: 0; line-height: 1.4; }
#output { padding: 10px; background: #2a2a2a; border-radius: 4px; }
h1 { color: #fff; }
</style>
</head>
<body>
<h1>OpenMagnetics Virtual Builder - Magnetic Tests</h1>
<div id="output"><pre id="log">Initializing...</pre></div>
<script type="module">
// =================================================================
// Test Configuration - mirrors Python test_magnetic.py
// =================================================================
// STL export options - coarser for faster testing
const stlOptions = { tolerance: 1.0, angularTolerance: 0.8, binary: true };
// Test results
const testResults = [];
let totalTests = 0;
let passedTests = 0;
let failedTests = 0;
// Path to test data (relative to MVB root)
const TEST_DATA_PATH = '/testData';
function log(msg, isPass = null, isWarn = false) {
const logEl = document.getElementById('log');
let className = '';
if (isPass === true) className = 'pass';
else if (isPass === false) className = 'fail';
else if (isWarn) className = 'warn';
else className = 'info';
logEl.innerHTML += `<span class="${className}">${msg}</span>\n`;
console.log(msg);
}
async function downloadBlob(filename, blob) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
// Store for Puppeteer to collect
if (!window.stlFiles) window.stlFiles = [];
window.stlFiles.push({ filename, blob });
// If running in Puppeteer, save immediately
if (window.saveFile) {
try {
const reader = new FileReader();
const base64 = await new Promise((resolve) => {
reader.onload = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(blob);
});
await window.saveFile(filename, base64);
} catch (e) {
console.error('Failed to save:', filename, e);
}
}
}
// =================================================================
// Test Runner - mirrors Python _run_test() method
// =================================================================
async function runMagneticTest(replicad, builder, masFilename, validateGeometry = false) {
// Load the MAS JSON file
const response = await fetch(`${TEST_DATA_PATH}/${masFilename}`);
if (!response.ok) {
throw new Error(`Failed to load ${masFilename}: ${response.statusText}`);
}
const masData = await response.json();
// Get magnetic data
const magneticData = masData.magnetic || masData;
const outputName = masFilename.replace('.json', '');
// Build the magnetic component
// Geometry is now returned in mm (SCALE units) for efficient STL export
const shape = builder.getMagnetic(magneticData, outputName);
if (!shape) {
throw new Error('getMagnetic returned null');
}
// Export to STL directly - geometry is already in mm
const stl = shape.blobSTL(stlOptions);
downloadBlob(`${outputName}.stl`, stl);
log(` STL size: ${stl.size} bytes`);
return {
shape,
stlSize: stl.size,
masData
};
}
// =================================================================
// Main Test Execution
// =================================================================
async function runTests() {
log('Initializing OpenCASCADE.js...');
log('Loading WASM (takes 10-20 seconds)...');
try {
// Import replicad with proper OpenCASCADE initialization
const replicad = await import('/node_modules/replicad/dist/replicad.js');
const opencascade = await import('/node_modules/replicad-opencascadejs/src/replicad_single.js');
// Initialize OpenCASCADE with explicit WASM path
const oc = await opencascade.default({
locateFile: (file) => `/node_modules/replicad-opencascadejs/src/${file}`
});
replicad.setOC(oc);
log('✓ OpenCASCADE.js loaded!', true);
// Import our builder
const { ReplicadBuilder } = await import('/src/index.js');
log('✓ ReplicadBuilder loaded!', true);
const builder = new ReplicadBuilder(replicad);
// =============================================================
// Toroidal Tests - mirrors Python TestToroidal class
// =============================================================
log('\n========================================');
log('Toroidal Tests');
log('========================================');
// test_toroidal_single_turn_with_core
await runTest('test_toroidal_single_turn_with_core', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_one_turn.json', true);
return result.stlSize > 100;
});
// test_toroidal_two_turns_spread
await runTest('test_toroidal_two_turns_spread', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_two_turns_spread.json', true);
return result.stlSize > 100;
});
// test_toroidal_two_turns_centered
await runTest('test_toroidal_two_turns_centered', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_two_turns_centered.json', true);
return result.stlSize > 100;
});
// test_toroidal_full_layer
await runTest('test_toroidal_full_layer', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_full_layer.json', true);
return result.stlSize > 100;
});
// test_toroidal_multilayer
await runTest('test_toroidal_multilayer', async () => {
const result = await runMagneticTest(replicad, builder,
'test_wind_three_sections_two_layer_toroidal_contiguous_spread_top_additional_coordinates.json',
true
);
return result.stlSize > 100;
});
// test_toroidal_two_layers_not_compact
await runTest('test_toroidal_two_layers_not_compact', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_two_layers_not_compact.json', true);
return result.stlSize > 100;
});
// test_toroidal_full_layer_rectangular_wires
await runTest('test_toroidal_full_layer_rectangular_wires', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_full_layer_rectangular_wires.json');
return result.stlSize > 100;
});
// test_toroidal_one_turn_rectangular_wire
await runTest('test_toroidal_one_turn_rectangular_wire', async () => {
const result = await runMagneticTest(replicad, builder, 'toroidal_one_turn_rectangular_wire.json');
return result.stlSize > 100;
});
// =============================================================
// Concentric Tests - mirrors Python TestConcentric class
// =============================================================
log('\n========================================');
log('Concentric Tests');
log('========================================');
// test_concentric_rectangular_column_one_turn
await runTest('test_concentric_rectangular_column_one_turn', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_rectangular_column_one_turn.json');
return result.stlSize > 100;
});
// test_concentric_rectangular_column_two_turns
await runTest('test_concentric_rectangular_column_two_turns', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_rectangular_column_two_turns.json');
return result.stlSize > 100;
});
// test_concentric_rectangular_column_full_layer
await runTest('test_concentric_rectangular_column_full_layer', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_rectangular_column_full_layer.json');
return result.stlSize > 100;
});
// test_concentric_rectangular_column_two_layers
await runTest('test_concentric_rectangular_column_two_layers', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_rectangular_column_two_layers.json');
return result.stlSize > 100;
});
// test_concentric_rectangular_column_two_layers_with_bobbin
await runTest('test_concentric_rectangular_column_two_layers_with_bobbin', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_rectangular_column_two_layers_with_bobbin.json');
return result.stlSize > 100;
});
// test_concentric_round_column_four_layers
await runTest('test_concentric_round_column_four_layers', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_round_column_four_layers.json');
return result.stlSize > 100;
});
// test_concentric_round_column_one_rectangular_turn
await runTest('test_concentric_round_column_one_rectangular_turn', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_round_column_one_rectangular_turn.json');
return result.stlSize > 100;
});
// test_concentric_round_column_two_layers_rectangular_turns
await runTest('test_concentric_round_column_two_layers_rectangular_turns', async () => {
const result = await runMagneticTest(replicad, builder, 'concentric_round_column_two_layers_rectangular_turns.json');
return result.stlSize > 100;
});
// =============================================================
// Planar Tests - PCB/FR4 board rendering
// =============================================================
log('\n========================================');
log('Planar Transformer Tests');
log('========================================');
// test_planar_transformer_with_fr4_boards
await runTest('test_planar_transformer_with_fr4_boards', async () => {
const result = await runMagneticTest(replicad, builder, 'planar_transformer.json');
return result.stlSize > 100;
});
// test_bug_planar_missing_fr4 - specific bug test
await runTest('test_bug_planar_missing_fr4', async () => {
const result = await runMagneticTest(replicad, builder, 'bug_planar_missing_fr4.json');
return result.stlSize > 100;
});
// test_getFR4Board_direct - explicitly test FR4 board generation
await runTest('test_getFR4Board_direct', async () => {
// Load the bug file
const response = await fetch(`${TEST_DATA_PATH}/bug_planar_missing_fr4.json`);
const masData = await response.json();
const magneticData = masData.magnetic || masData;
const coil = magneticData.coil;
// Get group and bobbin data
const groupDesc = coil.groupsDescription[0];
const bobbinProcessed = coil.bobbin.processedDescription;
log(` Group type: ${groupDesc.type}`);
log(` Group dimensions: [${groupDesc.dimensions.join(', ')}]`);
log(` Group coordinates: [${groupDesc.coordinates.join(', ')}]`);
log(` Column shape: ${bobbinProcessed.columnShape}`);
// Call getFR4Board directly
const fr4Board = builder.getFR4Board(coil);
if (!fr4Board) {
log(' ✗ getFR4Board returned null!', false);
return false;
}
log(' ✓ FR4 board shape generated');
// Try to export as STL
const stl = fr4Board.blobSTL(stlOptions);
log(` STL size: ${stl.size} bytes`);
downloadBlob('fr4_board_direct.stl', stl);
return stl.size > 100;
});
// =============================================================
// Summary
// =============================================================
log('\n========================================');
log('Test Summary');
log('========================================');
log(`Total: ${totalTests}, Passed: ${passedTests}, Failed: ${failedTests}`);
if (failedTests === 0) {
log('✓ All tests PASSED!', true);
window.allTestsPassed = true;
} else {
log(`✗ ${failedTests} test(s) FAILED`, false);
window.allTestsPassed = false;
}
window.testsDone = true;
window.testResults = testResults;
} catch (err) {
log(`\n✗ Fatal error: ${err.message}`, false);
log(err.stack, false);
window.testsDone = true;
window.allTestsPassed = false;
}
}
async function runTest(name, testFn) {
totalTests++;
log(`\n${name}`);
try {
const result = await testFn();
if (result) {
log(` ✓ PASSED`, true);
passedTests++;
testResults.push({ name, passed: true });
} else {
log(` ✗ FAILED (assertion)`, false);
failedTests++;
testResults.push({ name, passed: false, error: 'Assertion failed' });
}
} catch (err) {
log(` ✗ FAILED: ${err.message}`, false);
log(` Stack: ${err.stack.split('\n').slice(0, 5).join('\n ')}`, false);
failedTests++;
testResults.push({ name, passed: false, error: err.message });
}
}
// Start tests
runTests();
</script>
</body>
</html>