Warning: Can not open [/home/conf/public_html/cgi-bin/show_python.log]. Ignore No title

Download script from state_sum.py
Related files:


import sys
from numpy import exp
import random
import itertools


ns = 3
np = 2


nargs = len(sys.argv)
if nargs > 1:
    np = int(sys.argv[1])
if nargs > 2:
    ns = int(sys.argv[2])


def generate_random_numbers(ns):
    return [random.random() for _ in range(ns)]

eps = []
for i in range(np):
    eps.append(generate_random_numbers(ns))

print()
print("nparticles=", np)
print("nstates=", ns)
print("normalized energies:")
for ip in range(np):
    print(f" particle {ip}:", eps[ip])

print()
print("Calculate Z using all combinations of states")
n = [range(ns)] * np
combinations = list(itertools.product(*n))

Ztot = 0.0
for ic, is_list in enumerate(combinations):
    Etot = 0.0
    for ip, _is in enumerate(is_list):
        Etot += eps[ip][_is]

    Ztot += exp(-Etot)
    print(f" combination {ic}:", is_list, f" Etot={Etot:10.6g} Ztot(partial)={Ztot:10.6g}")

print("Ztot=", Ztot)

print()
print("Calculate Z using the product of Zp")
Zp = [0.0] * np
Ztot2 = 1.0
for ip in range(np):
    for _is in range(ns):
        Zp[ip] += exp(-eps[ip][_is])
    print(f" particle {ip} Zp=", Zp[ip])

    Ztot2 *= Zp[ip]

print("Ztot=", Ztot2)