-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc_check.py
More file actions
167 lines (121 loc) · 4.83 KB
/
sc_check.py
File metadata and controls
167 lines (121 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import numpy as np
import re
import sys
import pint
import scipy.stats as st
un = pint.UnitRegistry()
# Self Consistency Check for Source Files
# .... check consistency of external source files
# Author: Leonel Morejon
# Date: 29/07/2016
epsilon = 1e-200 # tolerance for cosines square sum
# default_magnitudes = ('pid','x','y','z','tx','ty','tz','E','w')
default_magnitudes = ('x','y','z','tx','ty','tz','E','w')
default_units = ('cm',)*3 + ('dimensionless',)*3 + ('GeV','dimensionless')
# default_units = {'x':'cm', 'y':'cm', 'z':'cm',
# 'tx':'adim','ty':'adim','tz':'adim',
# 'E':'GeV', 'w':'adim'}
def guess_sdist(pos_dict):
'''Test function to determine the spatial distribution
of the source. It takes a dictionary with the same
structure as data in the source object, that contains
only the 'x','y','z' values
'''
for k in pos_dict:
if np.all(pos_dict[k] == pos_dict[k][0]):
pos_dict[k] = pos_dict[k][0]
return pos_dict
#### ------- Class for source files
class Source_File(object):
"""Placeholder class for external source files
provided and by external codes.
"""
def __init__(self, *args, **kwargs):
super(Source_File, self).__init__()
fname = args[0]
self.filename = fname
self.data = {}
self.magnitudes = ()
self.units = ()
# Initialize description dictionary ...
self.description = {'parID' : 'unknown',
'sdist' : 'point',
'pdist' : 'pencilbeam',
'edist' : 'monoenergetic'}
# ... and assign given values
for k in self.description:
if k in kwargs:
self.description[k] = kwargs[k];
# distribution functions: uniform by default
s_dist = st.uniform() # spatial distribution function
p_dist = st.uniform() # direction distribution function
e_dist = st.uniform() # energy distribution function
def import_file(self, *args, **kwargs):
''' Assuming the file contains a set of columns.
This is just a wrapper for genfromtxt with some post
processing of the sourcefile data. In other words
it accepts all the same arguments as genfromtxt
(see http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html)
'''
matrix = np.genfromtxt(self.filename, *args, **kwargs)
matrix = matrix.transpose() # to have columns into rows
for mag,u,vals in zip(self.magnitudes, self.units, matrix):
unit = un.Quantity(u) # choose the units from un
self.data[mag] = vals * unit
# call methods to convert from non-standard to standard magnitudes
if set( ('Bx','By','Bz') ).issubset(default_magnitudes):
B = np.sqrt(self.data['Bx']**2 + self.data['By']**2 + self.data['Bz']**2)
sef.data['tx'] = self.data['Bx'] / B
sef.data['ty'] = self.data['By'] / B
sef.data['tz'] = self.data['Bz'] / B
self.check_cosines()
# check consistency between magitudes, units and the
# columns in the file imported
# call methods to set source description parameters: e.g. point, monochromatic, etc
def set_magnitudes(self, magnitudes):
# set a list of magnitudes which refer to the columns in the file
self.magnitudes = magnitudes
def set_units(self, unit_labels):
# set a list of units which refer to the columns in the file
# these will be used by the self consistency check
self.units = [un.Quantity(u) for u in unit_labels]
def check_cosines(self):
# checking that direction cosines
# if ok returns 0, otherwise returns 1
return_value = 0
if set( ('tx','ty','tz') ).issubset(default_magnitudes):
tx = self.data['tx'].magnitude
ty = self.data['ty'].magnitude
tz = self.data['tz'].magnitude
s = tx**2+ty**2+tz**2
if np.any( s > 1-epsilon ):
return_value = np.where(s > 1-epsilon)
print "Warning: Not normalized cosines!"
else:
return_value = 1
print "Error: Some or all director cosines are undefined."
return return_value
def smear_positions(self,fixed=('z',)):
'''Smear the cosines leaving fixed the components along the
direction given (by default 'z')
'''
x = sourcefileobject.data['tx'].magnitude
y = sourcefileobject.data['ty'].magnitude
z = sourcefileobject.data['tz'].magnitude
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')
def renorm_weight(self,norm):
if 'w' in self.magnitudes:
self.data['w'] *= 1./norm
else:
print "Error: No weight values defined"
def save_standard_sourcefile(self,fname):
mag = default_magnitudes[0]
export_array = self.data[mag].magnitude
for mag in default_magnitudes[1:]:
if mag in self.magnitudes:
export_array = np.vstack( [export_array, self.data[mag].magnitude] )
# this below writes in fortran readable format
# the + sign in front of positive numbers could be problematic
# in that case, a simple function using re to change ' +' into ' ' should work
np.savetxt('source_file.dat', export_array, fmt="%+15.8e", delimiter=' ')