"""Reproduce the bounded teaching calculations; no project acceptance is evaluated."""
import json, math
from pathlib import Path
HERE=Path(__file__).resolve().parent
CASES=[('right_angle',3430,.28,830,.28,2),('portal',690,.61,450,.70,3),('stepped',620,.66,500,.68,3)]
def bent(co,cp,k,L,P,W):
    if k<=0 or L<=0 or W<=0 or P<0: raise ValueError('Known positive geometry/weight and nonnegative point weight required')
    return co*k/L**2/math.sqrt(1+cp*P/W)
def offset(L1,L2,L3,ell,w1,P,k,configuration):
    if not (L1>0 and L2>0 and L3>=L1 and 0<=ell<=L3 and w1>0 and P>=0 and k>0):
        raise ValueError('Use positive lengths/known weight, shortest L1, and point within L3')
    q=ell/L3; mu=P/(w1*L1)
    if configuration=='D':
        numerator=1.; denominator=1+4.24*(L2/L1+L3/(3*L1)+q*q*mu)
    elif configuration=='E':
        numerator=1+(L1/L3)**3
        denominator=1+L3/L1+4.24*(L2/L1+.25*(3*q*q-q**3)**2*mu)
    else: raise ValueError('Unknown configuration')
    coefficient=math.sqrt(numerator/denominator)
    return {'q':q,'mu':mu,'factor_numerator':numerator,'factor_denominator':denominator,'configuration_factor':coefficient,'frequency_Hz':785*k/L1**2*coefficient}
def results():
    out={'basis':'Hypothetical inputs; source-defined coefficients/equations; empty carbon steel; no water or stainless adjustment','equal_leg_inputs':{'k_in':.79,'L_ft':10,'w_lbf_per_ft':5,'P_lbf':20},'equal_leg_results':[],'offset_inputs':{'k_in':.79,'L1_ft':2,'L2_ft':10,'L3_ft':4,'ell_ft':2,'w1_lbf_per_ft':5,'P_lbf':20},'offset_results':{},'offset_mass_position_change':{}}
    for name,ci,pi,co,po,count in CASES:
        W=count*5*10
        out['equal_leg_results'].append({'configuration':name,'W_total_lbf':W,'in_plane_Hz':bent(ci,pi,.79,10,20,W),'out_of_plane_Hz':bent(co,po,.79,10,20,W)})
    for c in ['D','E']:
        out['offset_results'][c]=offset(2,10,4,2,5,20,.79,c)
        out['offset_mass_position_change'][c]=offset(2,10,4,3,5,20,.79,c)
    m=18/386
    out['valve_exemplar']={'weight_each_lbf':18,'gravity_in_per_s2':386,'mass_each_lbf_s2_per_in':m,'arms_in':[7.5,14.0],'moment_coefficients_lbf_s2':[m*7.5,m*14.0],'hypothetical_in_phase_accelerations_in_per_s2':[100,150],'hypothetical_moment_lbf_in':m*7.5*100+m*14*150}
    return out
if __name__=='__main__':
    data=results()
    target=HERE/'c09-bent-offset-calculations.json'
    target.write_text(json.dumps(data,indent=2)+'\n')
    print(json.dumps(data,indent=2))
