Files
toroid/app.py

198 lines
6.3 KiB
Python

from flask import Flask, render_template, request, jsonify
from model import TransformerModel
from optimizer import TransformerOptimizer
app = Flask(__name__)
# Default transformer configuration
# You can modify this to match your actual transformer
def get_default_transformer():
primary_taps = [0, 75, 75]
secondary_taps = [0, 100, 150, 150, 150]
# Resistance per turn for each segment (example values in ohms/turn)
# These would be calculated based on wire gauge, length per turn, etc.
primary_Rp_per_turn = [
0.01,
0.01,
]
secondary_Rs_per_turn = [
0.004,
0.024,
0.024,
0.024,
]
tf = TransformerModel(
Ae_mm2=354.0,
Ve_mm3=43900.0,
use_core_loss_model=True,
Np_total=150,
Ns_total=250,
primary_taps=primary_taps,
secondary_taps=secondary_taps,
primary_Rp_per_turn=primary_Rp_per_turn,
secondary_Rs_per_turn=secondary_Rs_per_turn,
)
return tf
@app.route('/')
def index():
return render_template('index.html')
@app.route('/manual')
def manual():
return render_template('manual.html')
@app.route('/api/simulate', methods=['POST'])
def simulate():
"""Run manual simulation with specified tap and voltage"""
try:
data = request.json
# Extract parameters
primary_tap = int(data.get('primary_tap', 1))
secondary_tap = int(data.get('secondary_tap', 1))
Vp_rms = float(data.get('Vp_rms', 12))
freq_hz = float(data.get('freq_hz', 2000))
load_ohms = float(data.get('load_ohms', 100))
# Create transformer
tf = get_default_transformer()
# Run simulation (core loss is calculated automatically)
result = tf.simulate(
primary_tap=primary_tap,
secondary_tap=secondary_tap,
Vp_rms=Vp_rms,
freq_hz=freq_hz,
load_ohms=load_ohms,
core_loss_W=0.0, # Will be calculated by model
)
return jsonify({
'success': True,
'result': {
'primary_tap': result['primary_tap'],
'secondary_tap': result['secondary_tap'],
'Np_eff': result['Np_eff'],
'Ns_eff': result['Ns_eff'],
'Vp_rms': round(result['Vp_rms'], 2),
'Vs_rms': round(result['Vs_rms'], 2),
'Ip_rms': round(result['Ip_rms'], 3),
'Is_rms': round(result['Is_rms'], 3),
'turns_ratio': round(result['turns_ratio'], 3),
'P_out_W': round(result['P_out_W'], 2),
'P_in_W': round(result['P_in_W'], 2),
'P_cu_W': round(result['P_cu_W'], 2),
'P_cu_primary_W': round(result['P_cu_primary_W'], 3),
'P_cu_secondary_W': round(result['P_cu_secondary_W'], 3),
'P_core_W': round(result['P_core_W'], 2),
'efficiency': round(result['efficiency'] * 100, 2),
'B_peak_T': round(result['B_peak_T'], 4),
}
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 400
@app.route('/api/optimize', methods=['POST'])
def optimize():
try:
data = request.json
# Extract parameters
load_ohms = float(data.get('load_ohms', 100))
target_power_W = float(data.get('target_power_W', 10))
freq_hz = float(data.get('freq_hz', 2000))
Vp_min = float(data.get('Vp_min', 5))
Vp_max = float(data.get('Vp_max', 36))
Vp_step = float(data.get('Vp_step', 0.5))
B_max_T = float(data.get('B_max_T', 0.3))
Vs_max = float(data.get('Vs_max', 200))
Is_max = float(data.get('Is_max', 1.5))
power_tolerance_percent = float(data.get('power_tolerance_percent', 2.0))
# Create transformer and optimizer
tf = get_default_transformer()
opt = TransformerOptimizer(tf)
# Run optimization (core loss is calculated automatically)
result = opt.optimize(
load_ohms=load_ohms,
target_power_W=target_power_W,
freq_hz=freq_hz,
Vp_min=Vp_min,
Vp_max=Vp_max,
Vp_step=Vp_step,
B_max_T=B_max_T,
Vs_max=Vs_max,
Is_max=Is_max,
core_loss_W=0.0, # Will be calculated by model
power_tolerance_percent=power_tolerance_percent,
)
if result:
return jsonify({
'success': True,
'result': {
'primary_tap': result.primary_tap,
'secondary_tap': result.secondary_tap,
'Np_eff': result.Np_eff,
'Ns_eff': result.Ns_eff,
'Vp_rms': round(result.Vp_rms, 2),
'Vs_rms': round(result.Vs_rms, 2),
'Ip_rms': round(result.Ip_rms, 3),
'Is_rms': round(result.Is_rms, 3),
'turns_ratio': round(result.turns_ratio, 3),
'P_out_W': round(result.P_out_W, 2),
'P_in_W': round(result.P_in_W, 2),
'P_cu_W': round(result.P_cu_W, 2),
'P_cu_primary_W': round(result.P_cu_primary_W, 3),
'P_cu_secondary_W': round(result.P_cu_secondary_W, 3),
'P_core_W': round(result.P_core_W, 2),
'efficiency': round(result.efficiency * 100, 2),
'B_peak_T': round(result.B_peak_T, 4),
'power_error_percent': round(result.power_error_percent, 2),
}
})
else:
return jsonify({
'success': False,
'error': 'No valid configuration found within constraints'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 400
@app.route('/api/transformer_info', methods=['GET'])
def transformer_info():
"""Return transformer configuration information"""
tf = get_default_transformer()
return jsonify({
'primary_taps': tf.primary_taps,
'secondary_taps': tf.secondary_taps,
'Ae_mm2': tf.Ae_mm2,
'Np_total': tf.Np_total,
'Ns_total': tf.Ns_total,
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)