danieljfarrell / pvtrace

Optical ray tracing for luminescent materials and spectral converter photovoltaic devices
Other
97 stars 94 forks source link

hello_world.py on windows hangs #43

Closed danieljfarrell closed 3 years ago

danieljfarrell commented 3 years ago

The Meshcat Visualiser no longer creates it's own ZMQ server on WIndows. This makes the hello_world.py script hang when run on Windows.

Workaround

  1. Follow the standard installation steps in the README using Conda
  2. Activate the python environment conda activate pvtrace-env
  3. Start a meshcat server by typing meshcat-server the terminal This will print the ZMQ url to the console
    > meshcat-server
    zmq_url=tcp://127.0.0.1:6000
    web_url=http://127.0.0.1:7000/static/
  4. Open a new terminal window
  5. Activate the python environment conda activate pvtrace-env
  6. Copy the modified hello_world.py script to your folder (see script below)
  7. Make sure the zmq_url you found in step 3 is the same as in line 32 of the script
  8. Run the script python hello_world.py
  9. The visualiser should now open a browser window and work as expected.
# hello_world.py
import time
import sys
import functools
import numpy as np
from pvtrace import *

world = Node(
    name="world (air)",
    geometry=Sphere(
        radius=10.0,
        material=Material(refractive_index=1.0),
    )
)

sphere = Node(
    name="sphere (glass)",
    geometry=Sphere(
        radius=1.0,
        material=Material(refractive_index=1.5),
    ),
    parent=world
)
sphere.location = (0, 0, 2)

light = Node(
    name="Light (555nm)",
    light=Light(direction=functools.partial(cone, np.pi/8)),
    parent=world
)

# Change the zmq_url in the line below to the URL of your server
renderer = MeshcatRenderer(zmq_url="tcp://127.0.0.1:6000", wireframe=True, open_browser=True)
scene = Scene(world)
renderer.render(scene)
for ray in scene.emit(100):
    steps = photon_tracer.follow(scene, ray)
    path, events = zip(*steps)
    renderer.add_ray_path(path)
    time.sleep(0.1)

# Wait for Ctrl-C to terminate the script; keep the window open
print("Ctrl-C to close")
while True:
    try:
        time.sleep(.3)
    except KeyboardInterrupt:
        sys.exit()