[Python] How to Calculate Thermodynamic Equations Using Sympy in Python

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    I solved the Schrödinger equation for a given potential and then found the energy equation to substitute into the thermodynamic equations. However, I struggled to write a program code using Python with the help of GPT. I came up with this, but it doesn't work or solve the integral.

    import sympy as sp

    #variables
    beta, Q1, Q2, Q3, n, delta, lambda_var, rho, aleph, aleph0, k_B = sp.symbols('beta Q1
    Q2 Q3 lambda_var n delta rho aleph aleph0 k_B', real=True, positive=True)

    lambda_var = -delta + sp.sqrt(Q3)
    rho = n + delta

    # partition function of energy
    Z_beta_integral = sp.integrate(sp.exp(beta * (Q2 * rho**2 + Q2 * Q3 / rho**2)), (rho,
    delta, delta + lambda_var))


    Z_beta_solution = sp.simplify(Z_beta_integral)


    F = -sp.ln(Z_beta_solution) / beta # free energy

    U = -sp.diff(sp.ln(Z_beta_solution), beta) # internal energy

    S = sp.simplify(k_B * (sp.ln(Z_beta_solution) + beta * U)) # entropy

    C_V = sp.simplify(sp.diff(U, beta)) # specific heat

    print(f'Z_beta_integral = {Z_beta_integral}')
    print(f'Z_beta_solution = {Z_beta_solution}')

    print("\nInternal Energy U:")
    print(sp.latex(U))
    print("\nEntropy S:")
    print(sp.latex(S))
    print("\nHeat Capacity C_V:")
    print(sp.latex(C_V))
    print("\nFree Energy F:")
    print(sp.latex(F))

    Continue reading...

Compartilhe esta Página