0

The answer given in my previous question about sympy's symbolic and numeric solving was very helpful and optimised the code a lot. I want to use this example to convert another part of code:

The following parts shows the original lines. (To be clear: the variables here are floats):

H_new = symbols("H")
cord_length_expr = cord_line_function2(x_part[0],x_part[-1], H_new, R, B)

elongation_thermal = e_therm * (new_temp - ref_temp) 
dS                 = H_new*(cord_length_expr/displacement) - H0*(L0/displacement) # formula 14.20
elongation_elastic = dS/(Emodulus * area) # formula 14.38
    
H = float(nsolve(L0 * (1 + elongation_thermal) * (1 + elongation_elastic) - cord_length_expr, H_new, H0))

I tried to convert this to numeric solving, by doing:

def elongation_formula(module):
    from sympy import symbols, Eq, lambdify, solve, exp, sinh

    H_new, H0, x_part_start, x_part_end, R, B, Emodulus, e_therm, displacement, area, L0, new_temp, ref_temp = symbols("H_new, H0, x_part_start, x_part_end, R, B, Emodulus, e_therm, displacement, area, L0, new_temp, ref_temp")
    cord_length_expr = cord_line_function2(x_part_start,x_part_end, H_new, R, B)
    
    elongation_thermal = e_therm * (new_temp - ref_temp) 
    dS                 = H_new*(cord_length_expr/displacement) - H0*(L0/displacement)
    elongation_elastic = dS/(Emodulus * area)
    
    eq: Eq = L0 * (1 + elongation_thermal) * (1 + elongation_elastic) - cord_length_expr
    
    a1, a2 = solve(eq.rewrite(exp).expand(), H_new)
    return lambdify((H0, x_part_start, x_part_end, R, B, Emodulus, e_therm, displacement, area, L0, new_temp, ref_temp, e_therm), a1, modules=module)

Additional function here:

def cord_line_function2(x1,x2, H, w, B):
    """
    See 'cord_line_function' method for full explanation. 
    cord_line_function2 calculates the length of a cord, based on 
    its horizontal force and x positions. Shifted x
    positions are quantified with variable B. 
    Use this function is B is known and only the Length needs to be found. 
    """    
    from sympy import sinh

    # return a function based on the given values:
    # L = ∫sqrt(1+(sinh(w*(x+B)/H))^2) dx
    s1 = w*(x1+B)/H
    s2 = w*(x2+B)/H
    L = H*sinh(s2)/w - H*sinh(s1)/w 

    return L

Calling this function elong_function = elongation_formula("math") results in a multiple generators error.

The the full error message here:

Exception has occurred: NotImplementedError
multiple generators [H_new, exp(B*R/H_new), exp(R*x_part_end/H_new), exp(R*x_part_start/H_new)]
No algorithms are implemented to solve equation -H_new*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*R) + H_new*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*R) - H_new*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*R) + H_new*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*R) + L0*e_therm*new_temp - L0*e_therm*ref_temp + L0 - H0*L0**2*e_therm*new_temp/(Emodulus*area*displacement) + H0*L0**2*e_therm*ref_temp/(Emodulus*area*displacement) - H0*L0**2/(Emodulus*area*displacement) + H_new**2*L0*e_therm*new_temp*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*new_temp*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*new_temp*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*new_temp*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*ref_temp*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*ref_temp*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*ref_temp*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*ref_temp*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement)
  File "C:\Users\NL1A2K\Documents\Python_tools\Energy-Power_lines\cord_line.py", line 134, in elongation_formula
    a1, a2 = solve(eq.rewrite(exp).expand(), H_new)
  File "C:\Users\NL1A2K\Documents\Python_tools\Energy-Power_lines\cord_line.py", line 1735, in <module>
    elong_function = elongation_formula("math")
NotImplementedError: multiple generators [H_new, exp(B*R/H_new), exp(R*x_part_end/H_new), exp(R*x_part_start/H_new)]
No algorithms are implemented to solve equation -H_new*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*R) + H_new*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*R) - H_new*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*R) + H_new*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*R) + L0*e_therm*new_temp - L0*e_therm*ref_temp + L0 - H0*L0**2*e_therm*new_temp/(Emodulus*area*displacement) + H0*L0**2*e_therm*ref_temp/(Emodulus*area*displacement) - H0*L0**2/(Emodulus*area*displacement) + H_new**2*L0*e_therm*new_temp*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*new_temp*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*new_temp*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*new_temp*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*ref_temp*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*ref_temp*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*e_therm*ref_temp*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*e_therm*ref_temp*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*exp(B*R/H_new)*exp(R*x_part_end/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*exp(B*R/H_new)*exp(R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) + H_new**2*L0*exp(-B*R/H_new)*exp(-R*x_part_start/H_new)/(2*Emodulus*R*area*displacement) - H_new**2*L0*exp(-B*R/H_new)*exp(-R*x_part_end/H_new)/(2*Emodulus*R*area*displacement)

Since it is not very clear to me what I should do here, help is very welcome again! Thanks in advance!

Tim Moore
  • 5
  • 2
  • Please fix the indentation from function. – toyota Supra Jun 19 '23 at 10:22
  • 1
    @toyotaSupra, indentation is now updated. – Tim Moore Jun 19 '23 at 10:37
  • full error message please – hpaulj Jun 19 '23 at 11:04
  • @hpaulj, the full error message is now given in the question. – Tim Moore Jun 19 '23 at 11:15
  • The code shown is incomplete (what is cord_line_function2?). Rather than add more code though I suggest figuring out how to write a minimal example so that the whole thing is about 4 lines of code. In any case the problem is that `solve` cannot compute a symbolic solution to the equation it is given. It might be possible to rearrange the equation so that solve can handle it. Without seeing the equation though all I can suggest is to try an approach that does not involve `solve`. – Oscar Benjamin Jun 19 '23 at 14:24
  • Does this answer your question? [NotImplementedError in Sympy's solve](https://stackoverflow.com/questions/45894976/notimplementederror-in-sympys-solve) – jared Jun 19 '23 at 14:26
  • @OscarBenjamin, I just added `cord_line_function2`, just in case. I will try to see if I can get a simplified version to work. – Tim Moore Jun 19 '23 at 15:49
  • This nonlinear equation appears to have no analytical solution. – smichr Jun 19 '23 at 17:50
  • @smichr, in that case, is there any other way to improve the performance of the code that I have? – Tim Moore Jun 20 '23 at 06:51
  • You could use lambdify and then scipy's fsolve to solve the equation numerically. – Oscar Benjamin Jun 20 '23 at 15:58

0 Answers0