iat-cener / tonatiuh

A Monte Carlo ray tracer for the optical simulation of solar concentrating systems
http://iat-cener.github.io/tonatiuh/
GNU General Public License v3.0
55 stars 15 forks source link

Posprocessing #117

Open samscape opened 2 years ago

samscape commented 2 years ago

I am trying to write a python code to calculate the receiver efficiency, following some instructions in a youtube webinar where they did the same thing. However my python code is not working "name 'get_WFgos' is not defined"

Can anyone help me?

import numpy as np import string

import pylab

from math import import os import glob from time import

treat a single TONATIUH output file "tonatiuh_out"

def main():

studentname = 'Simulation'
outputdir= 'C:/Users/swakim/Desktop/Exegetica/ORC/' + studentname + '/'

routine

for i in range(0, 1, 1):
    photonfile= 'Test_' + str(i)
surfIDs, surnames = treat_photons( photonfile, outputdir )
get_statistics ( surfIDs, surfnames, photonfile, outputdir, delphotons = True,)

print ('FINISHED TONATIUH OUTPUT TREATMENT AT:',)
print (strftime( '%H:%M:%S' , gmtime()))
return

treats photon arrays on the shape A=[[photon 8pos]]

and turns it into hit and absorptionmatrices to each surface

def treat_photons( photonfile, outputdir):

photon and parameters files

photons= outputdir + photonfile + '_1.dat' params = outputdir + photonfile + '_parameters.txt'

surf ids & names

surfIDs = [] surfnames = [] readfile=open(params,'r') lines=readfile.readlines() readfile.close() for line in lines: try: treat_fields = string.splitfields( line, '/') surfIDs.append(int(treat_fields[0])) surfname = string.splitfields(treat_fields[-1], '\n') [0] surfnames.append(surfname) except:pass

get photons array

P=np.fromfile(photons,dtype='>d') P=np.reshape(P, (-1,8)) #l photon per matrix line print ('%i photons loaded:' % P.shape[0],) print( strftime ('%H,%M,%S', gmtime()))

get WFgo matrix

WFgos = get_WFgos( P ) np.save( outputdir + 'WFgos', WFgos)

get hits and Abs matricesfor surfaces

for surfID in surfIDs: A, H = get_Ms_4_surf( surfID, P) if A.size> 0: np.save(outputdir + 'absssrfID%i' % (surfID), A) if H.size> 0: np.save(outputdir + 'hitssrfID%i' % (surfID), H)

print ('photons sorted:') print (strftime('%H:%M:%S', gmtime()))

return surfIDs, surfnames

def get_statistics( surfIDs, surfnames, photonfile, outputdir, delphotons=False,):

photons = outputdir + photonfile + '_l.dat'
params = outputdir + photonfile + '_parameters.txt'

readfile=open(params, 'r')
lines=readfile.readlines()
readfile.close()

#overall stats
totalphotons = np.fromfile (photons,dtype='>d')
totalphotons = np.reshape(totalphotons, (-1,8)) #1 photon per matrix line
totalphotons = totalphotons.shape[0]

s = '-------------------------------------------\n'
s += 'photons file: '+ photons + '\n'
s += 'parameters file: ' + params + '\n'
s = '-------------------------------------------\n'
s += 'total photons: %i \n' % totalphotons

WFfile = outputdir + 'WFgos.npy'
WFrays =  np.load( WFfile )
WFrays = WFrays.shape [0]

s += 'total rays from WFront (hiting the system): %i \n' % WFrays
photonpower = float(lines[-1])

s += 'photon power: %0.4f [W/photon] \n' %photonpower
totalpowerKW = photonpower*WFrays / 1000
s += 'total power hiting the system: %0.3f (kw] \n' % totalpowerKW

s += '-------------------------------------------\n'

#surf statistics

s += '\n SURFACE STATISTICS \n'
s += '-------------------------------------------\n'

for i in range(len(surfIDs)):
    surfID = surfIDs[i]
    surfname = surfnames[i]
    try:
        Mfile =outputdir + 'abss_srfID_%i.npy' %(surfID)
        A= np.load(Mfile)
        abss = A.shape[0]
    except:
            abss = 0
    try:
            Mfile = outputdir + 'hits_srfID_%i.npy' % (surfID)
            H = np.load( Mfile )
            hits = H.shape [0]
    except:
        hits = 0
    #OPTICAL EFFICIENCY: ABSORBED PHOTONS / EMITTED PHOTONS (HITING THE SYSTEM)
    opteffic = float( abss) / float (WFrays)
    abspowerKW = float (abss )* photonpower / 1000
    #surf lines
    s += 'surfID: %i | surfname: '% surfID
    s += surfname + '\n'
    s += '** total photon hits: %i \n' % hits
    s += '** total absorbed photon: %i \n' % abss
    s += '>> total absorbed power: %0.3f  [KW] \n' % abspowerKW
    s += '>> optical efficiency: %0.6f \n \n' % opteffic

statsfile = outputdir + photonfile + '_stats.txt'
writefile= open(statsfile, 'w')
writefile.write(s)
writefile.close()

#delete photonfile
if delphotons:
    os.remove(photons)

#delete hits and abss coord matrics
if delcoords:
    files = glob.glob(outputdir + '*.npy')
    for f in files: os.remove(f)

return

def get8WFgos (photonsM):

prevID ==0 and nextID !=0

M = photonsM[photonsM[:,5]==0]
M = M[M[:,6] !=0]

#delete columns 0, 4 ,5, 6,7
M= np.delete (M, np.s_[4:8], axis=1)
M= np.delete (M, np.s_[0:1], axis=1)
return M

def get_Ms_4_surf( surfID, photonsM) :

hits (surfID==surfID)

H= photonsM[photonsM[:,7] == surfID]
#abss (prevID != 0 qnd nextID==0)

if H.size >0:
    A= H[H[:,6] ==0]
if A.size>0: A= A[A[:,5] !=0]
        #delete columns 0,4,5,6,7
if A.size>0:
    A= np.delete(A, np.s[4:8], axis=1)
    A= np.delete(A, np.s[0:1], axis=1)
if H.size >0:
    H= np.delete(H, np.s[4:8], axis=1)
    H= np.delete(H, np.s[0:1], axis=1)

else:
    A=H
return A, H

startup routine

if name== "main": main()