frib-high-level-controls / FLAME

Fast Linear Accelerator Model Engine
MIT License
10 stars 8 forks source link

Data file not found when configured with dictionary #9

Closed dxmaxwell closed 8 years ago

dxmaxwell commented 8 years ago

When I run the following example:

import flame
import numpy

lattice = {}
lattice['elements'] = []

lattice['sim_type'] = "MomentMatrix"

lattice['MpoleLevel'] = "2";

lattice['HdipoleFitMode'] = "1";

lattice['IonZ']  = 33.0/238.0
lattice['IonEs'] = 931.49432e6
lattice['IonEk'] = 0.5e6
lattice['IonW']  = lattice['IonEs'] + lattice['IonEk']

lattice['Stripper_IonChargeStates'] = numpy.array([76.0/238.0, 77.0/238.0, 78.0/238.0, 79.0/238.0, 80.0/238.0]);
lattice['Stripper_NCharge']         = numpy.array([2660.0, 4360.0, 5300.0, 5090.0, 3230.0]);

lattice['IonChargeStates'] = numpy.array([33.0/238.0])
lattice['NCharge'] = numpy.array([10111.0])

lattice['BaryCenter0'] = numpy.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])

lattice['S0'] = numpy.array([
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
])

lattice['Eng_Data_Dir'] = "/home/devuser/machines/FRIB/flame_data"

lattice['elements'].append({ 'name':'S', "type":"source", "vector_variable":"BaryCenter", "matrix_variable":"S" })
lattice['elements'].append({ 'name':'LS1_CA01:GV_D1124', 'type':'drift', 'L':0.072, 'aper':0.04 })
lattice['elements'].append({ 'name':'drift_1', 'type':'drift', 'L':0.135064, 'aper':0.04 })
lattice['elements'].append({ 'name':'LS1_CA01:CAV1_D1127', 'type':'rfcavity', 'L':0.24, 'aper':0.034, 'cavtype':"0.041QWR", 'f':8.05e+07, 'phi':353.787, 'scl_fac':0.639566 })

try:
  flame.Machine(flame.GLPSPrinter(lattice))
  print "SUCCESS #1"
except Exception as e:
  print "FAILURE #1:", e 

try:
  flame.Machine(lattice)
  print "SUCCESS #2"
except Exception as e:
  print "FAILURE #2:", e

I get the following error:

devuser@develop-vmphy0:~$ python test.py
SUCCESS #1
FAILURE #2: Error while constructing element 3 'LS1_CA01:CAV1_D1127' : St13runtime_error : Error parsing /axisData_41.txt : boost::filesystem::last_write_time: No such file or directory: "/axisData_41.txt"

There is a difference between loading from lattice file and loading from dictionary.

mdavidsaver commented 8 years ago

The short version is that this is as expect. The 'Eng_Data_Dir' parameter should be given for the rfcavity elements.

The confusion here is due to the differences between the Config produced by the glps parser and that produced from a python dict. The glps file has the concept of two scopes for variable definitions: file and element. Variables defined in the file scope are inherited by all elements. When a python dict is turned into a Config this is not done.

This is side-effect of how Dict2Config() works at present (depth first traversal combined with unordered dict iteration). I'll take another look at this.

In the mean time you'll have to specify all necessary parameters for each element.

mdavidsaver commented 8 years ago

I've made an attempt to "fix" the confusion. The GLPSParser.parse() now returns a list of tuples instead of a dict. Machine() and GLPSPrinter() accept either a dict, OrderedDict, or list of tuples. When translating these into Config the scope should be done in the same manner as the GLPS parser.

Please try it and close this ticket if sufficient.

dxmaxwell commented 8 years ago

Thank you, I will give it a try.

On Fri, Jun 24, 2016 at 1:39 PM, mdavidsaver notifications@github.com wrote:

I've made an attempt to "fix" the confusion. The GLPSParser.parse() now returns a list of tuples instead of a dict. Machine() and GLPSPrinter() accept either a dict, OrderedDict, or list of tuples. When translating these into Config the scope should be done in the same manner as the GLPS parser.

Please try it and close this ticket if sufficient.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/frib-high-level-controls/FLAME/issues/9#issuecomment-228411316, or mute the thread https://github.com/notifications/unsubscribe/AAGshyy9OfCroCsVAphzKRvHxGKEtnvVks5qPBZrgaJpZM4I9HPx .

dxmaxwell commented 8 years ago

I converted my example script from above to use the new tuple structure:

import flame
import numpy

lattice = []
elements = []
lattice.append(('elements', elements))

lattice.append(('sim_type',"MomentMatrix"))

lattice.append(('MpoleLevel',"2"))

lattice.append(('HdipoleFitMode',"1"))

lattice.append(('IonEs',931.49432e6))           # Nucleon mass [eV/u].
lattice.append(('IonEk',0.5e6))         # Kinetic energy at LS1 entrance [eV/u].

lattice.append(('Stripper_IonChargeStates', numpy.array([76.0/238.0, 77.0/238.0, 78.0/238.0, 79.0/238.0, 80.0/238.0])))
lattice.append(('Stripper_NCharge', numpy.array([2660.0, 4360.0, 5300.0, 5090.0, 3230.0])))

lattice.append(('IonChargeStates', numpy.array([33.0/238.0])))
lattice.append(('NCharge',numpy.array([10111.0])))

lattice.append(('BaryCenter0', numpy.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])))

lattice.append(('S0', numpy.array([
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
  0.0, 0.0,  0.0, 0.0,  0.0, 0.0, 0.0,
])))

lattice.append(('Eng_Data_Dir', "/home/devuser/machines/FRIB/flame_data"))

elements.append([ ('name','S'), ("type","source"), ("vector_variable","BaryCenter"), ("matrix_variable","S") ])
elements.append([ ('name','LS1_CA01:GV_D1124'), ('type','drift'), ('L',0.072), ('aper',0.04) ])
elements.append([ ('name','drift_1'), ('type','drift'), ('L',0.135064), ('aper',0.04) ])
elements.append([ ('name','LS1_CA01:CAV1_D1127'), ('type','rfcavity'), ('L',0.24), ('aper',0.034), ('cavtype',"0.041QWR"), ('f',8.05e+07), ('phi',353.787), ('scl_fac',0.639566) ])

try:
  flame.Machine(flame.GLPSPrinter(lattice))
  print "SUCCESS #1"
except Exception as e:
  print "FAILURE #1:", e 

try:
  flame.Machine(lattice)
  print "SUCCESS #2"
except Exception as e:
  print "FAILURE #2:", e

But I get the same error:

devuser@develop-vmphy0:~$ python test.py 
SUCCESS #1
FAILURE #2: Error while constructing element 3 'LS1_CA01:CAV1_D1127' : St13runtime_error : Error parsing /axisData_41.txt : boost::filesystem::last_write_time: No such file or directory: "/axisData_41.txt"
mdavidsaver commented 8 years ago

lattice = {}

Try with

lattice = OrderedDict()
mdavidsaver commented 8 years ago

Please try again with c0cfa6ae3e046473bd920b2707ec789361c03596

dxmaxwell commented 8 years ago

That fixed it!