danieljfarrell / pvtrace

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

vpython module not working #9

Closed carlotapereira closed 5 years ago

carlotapereira commented 5 years ago

Hello Daniel!

I was messing around with your project and I got stuck trying to get the vpython module to work. I followed every step in the wiki and Pvtrace works but I get this message: visual is not installed

I noticed that in the environment.yml, vpython is there as a dependency and it's actually installed in the env. I also installed vpython using the installer and also with pip.

So I searched a little bit and found that the latest version of vpython doesn't use visual module anymore and uses now only vpython. So instead of import visual it should be from vpython import *.

Is there anything I'm doing wrong or does the code need to be updated to work with the new version of vpython?

Thanks! Carlota

danieljfarrell commented 5 years ago

I've been spending a more of time recently updating pvtrace and noticed this too on the weekend. I have a fix on my development branch, which I am yet to push to github.

You're right, we now need to import vpython, but their API has changed quite a bit over the years and this requires some additional updating to be done in the Trace.py and Visualise.py modules.

I'll push once I have done a final check over the changes, probably in the next few days. But until then, the visualiser is broken.

danieljfarrell commented 5 years ago

Got around to pushing that commit 080cdad to master. Take a look and let me know if it works for you. Try and run the example/homogen.py. This should pop open a browser window and you should start seeing photons flying around đź‘Ť

carlotapereira commented 5 years ago

Hi Daniel,

Thank you very much for your help! The vpython module seems to work just fine now! I'm sorry it took me a while to reply, I only got back to try to run pvtrace again this weekend.

I seem to have a problem, however, when I try to run a simulation with a Rod device, instead of a LSC. Simply substituting the LSC class to a Rod in the homogen example results in a error message.

Thanks again! Best regards, Carlota

danieljfarrell commented 5 years ago

Hi Carlota,

Post your script and I’ll take a look.

carlotapereira commented 5 years ago

homogen2.zip I barely changed anything in comparison with the example script homogen. The error "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" keeps pooping up, in regards to several parts of the code. However, if you do the suggested changes, eventually everything seems to run OK.

danieljfarrell commented 5 years ago

Want to make a pull request for the fixes you made so I can add them? I have not run the script yet to take a look at the errors, but I will when I can.

carlotapereira commented 5 years ago

Hi Daniel,

Is it possible to to a cylinder coating in a LSC with a cylindrical shape with pvtrace? Sorry to bother you again,

Carlota

danieljfarrell commented 5 years ago

Hi Carlota,

Try making the cylinder shape you want and then pass it to the coating object with the shape parameter. Now ray trace the coating object.

In pseudocode,

shape = Cylinder()
coating = Coating(reflectivity=my_reflectivity, shape=shape)
...
# now ray trace the coating 
carlotapereira commented 5 years ago

Hi Daniel,

I tried doing it as you said but I got the error "Cylinder' object has no attribute 'origin' ". I'm probably doing something wrong, so if you could take a quick look at my code, I would be most grateful! I've been struggling with this for a while and just can't seem to make it work.

I also tried to translate the cylinder, so I can make try to make it smaller (if I don't translate it, it kinda disappears within the z axis), but the command I'm using is also probably not the right one.

Thank you very much for your availability, Best regards Carlota fibra.zip

danieljfarrell commented 5 years ago

Hi Carlota,

I can’t open this right now, but if they post the text of your script here I will be able to get a feel for what is happening.

But to be honest, I only ever used the Coating stuff with LSC shapes that were boxes. I don’t want to increase your expectations, it might not actually be possible without writing some Python code to fix things.

I will release pvtrace 2.0 soon which will have coating built into the architecture (rather than just bolted on as they are in pvtrace 1.3!)

carlotapereira commented 5 years ago
import numpy as np
import pvtrace
from pvtrace.external import transformations as tf
from pvtrace import *

#Create light source from AM1.5 data, truncate to 400 -- 800nm range
file = os.path.join(PVTDATA,'sources','AM1.5g-full.txt')
oriel = load_spectrum(file, xbins=np.arange(300,1600))
source = PointSource(spectrum=oriel, center=(0,0,0.08), phimin =0, phimax =2*np.pi, thetamin =0.5*np.pi, thetamax =np.pi)

#Load dye absorption and emission data
file = os.path.join(PVTDATA, 'dyes', 'fluro-red.abs.txt')
abs = load_spectrum(file)
file = os.path.join(PVTDATA, 'dyes', 'fluro-red-fit.ems.txt')
ems = load_spectrum(file)

#Make the LSC

lsc = Rod(radius=0.01,length=0.05)
new_transform= tf.translation_matrix([0.05, 0.1, 0])
lsc.name = "LSC"
lsc.material = Material(absorption_data=abs, emission_data=ems, quantum_efficiency=0.5, refractive_index=1.3)
lsc.name = "LSC"

scene = Scene()
scene.add_object(lsc)

# Coating
shape = Cylinder(radius=0.011,length=0.05)
coating = Coating(reflectivity=0.5, shape=shape)
coating.name= "tape"
scene.add_object(coating)

# Ask python that the directory of this script file is and use it as the location of the database file
pwd = os.getcwd()
dbfile = os.path.join(pwd, 'homogen_db2.sql') # <--- the name of the database file

trace = Tracer(scene=scene, source=source, seed=1, throws=300, database_file=dbfile, use_visualiser=True, show_log=False)
trace.show_lines = True
trace.show_path = True
import time
tic = time.clock()
trace.start()
toc = time.clock()

# 6) Statistics
print("")
print("Run Time: ", toc - tic)
print("")

print("Technical details:")
generated = len(trace.database.uids_generated_photons())
killed = len(trace.database.killed())
thrown = generated - killed
print("\t Generated \t", generated)
print("\t Killed \t", killed)
print("\t Thrown \t", thrown)

print("Summary:")
print("\t Optical efficiency \t", (len(trace.database.uids_out_bound_on_surface('left', luminescent=True)) + len(trace.database.uids_out_bound_on_surface('right', luminescent=True)) + len(trace.database.uids_out_bound_on_surface('near', luminescent=True)) + len(trace.database.uids_out_bound_on_surface('far', luminescent=True))) * 100 / thrown, "%")
print("\t Photon efficiency \t", (len(trace.database.uids_out_bound_on_surface('left')) + len(trace.database.uids_out_bound_on_surface('right')) + len(trace.database.uids_out_bound_on_surface('near')) + len(trace.database.uids_out_bound_on_surface('far')) + len(trace.database.uids_out_bound_on_surface('top')) + len(trace.database.uids_out_bound_on_surface('bottom'))) * 100 / thrown, "%")

print("Luminescent photons:")
edges = ['left', 'near', 'far', 'right']
apertures = ['top', 'bottom']

for surface in edges:
    print("\t", surface, "\t", len(trace.database.uids_out_bound_on_surface(surface, luminescent=True))/thrown * 100, "%")

for surface in apertures:
    print("\t", surface, "\t", len(trace.database.uids_out_bound_on_surface(surface, luminescent=True))/thrown * 100, "%")

print("\t", len(trace.database.uids_nonradiative_losses())/thrown * 100, "%")

print("Solar photons (transmitted/reflected):")
for surface in edges:
    print("\t", surface, "\t", len(trace.database.uids_out_bound_on_surface(surface, solar=True))/thrown * 100, "%")

for surface in apertures:
    print("\t", surface, "\t", len(trace.database.uids_out_bound_on_surface(surface, solar=True))/thrown * 100, "%")
carlotapereira commented 5 years ago

This is the code that I'm using. I'm new to python and not a very experienced programmer, so thanks again for all your help!

Carlota

danieljfarrell commented 5 years ago

Hello Carlota,

Thanks for posting the code, it makes it much easier to see what is happening. The actual error message you get too would help a lot, maybe you could paste that in too? From what you wrote before, the software was complaining that a cylinder does not have an origin parameter. I have only used a coating object with box geometries. It seems that it does not work with cylinder geometries.

I just opened an issue: #10 . It proposes a fix, but I have not tested it yet.

I want to see if this change fixes the issue, but I know I won't have time to test this for a week or so. I'm posting this here to give you some ideas on how to proceed if you want to try yourself as I thought this was better than silence!

carlotapereira commented 5 years ago

Hi Daniel,

Thanks for the fast answer! Meanwhile I'll try to test the changes you proposed and see if that fixes the issue. Carlota

danieljfarrell commented 5 years ago

So I managed to take a look this evening.

But I came to the conclusions that this is an architectural problem. It could be fixed with more debugging. Unfortunately, I can't invest time in this now. I'm working hard on pvtrace 2.0 at the moment and I think it's a better use of time to add this feature to the pvtrace 2.0 architecture in a fully supported, documented and integrated way, rather than duck tapping this on to pvtrace 1.0. Look out for pvtrace 2.0 soon. I will release in about another month.

Made an attempt to fix this issue with this commit 597f095 however running scripts/debug_coating.py, and after generating a few rays, the tracing stops with a error in the fresnel refraction calculation. I imaging this is due to the coating angular reflectivity function has resulted in a ray direction which would never occur if the surface has pure Fresnel reflection. Unfortunately, this is not easy to solve. Mainly because the pvtrace 1.0 architecture is not really designed to deal with coating. They were added quickly and work only with box geometries (well that's how I used them anyway).

The solution here is to wait for pvtrace 2.0 which will have much better support for coatings.

carlotapereira commented 5 years ago

Ok, thank you so much for everything and for taking the time to help me. I'll wait for pvtrace 2.0 then!

Best regards, Carlota

danieljfarrell commented 5 years ago

Hello Carlota,

pvtrace 2.0 has been released. Just letting you know as it might be useful to you, https://pvtrace.readthedocs.io/en/latest/

At the moment it does not do coatings but I will work on that next.

carlotapereira commented 5 years ago

Hi Daniel,

Thank you so much for letting me know!

Carlota