Package madgraph :: Package iolibs :: Module save_model
[hide private]
[frames] | no frames]

Source Code for Module madgraph.iolibs.save_model

 1  ################################################################################ 
 2  # 
 3  # Copyright (c) 2009 The MadGraph Development team and Contributors 
 4  # 
 5  # This file is a part of the MadGraph 5 project, an application which  
 6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
 7  # high-energy processes in the Standard Model and beyond. 
 8  # 
 9  # It is subject to the MadGraph license which should accompany this  
10  # distribution. 
11  # 
12  # For more information, please visit: http://madgraph.phys.ucl.ac.be 
13  # 
14  ################################################################################ 
15   
16  """Function to save model files.""" 
17   
18  import logging 
19  import os 
20   
21  import madgraph.iolibs.files as files 
22  import madgraph.core.base_objects as base_objects 
23   
24  logger = logging.getLogger('madgraph.save_model') 
25   
26 -def save_particles(fsock, part_list):
27 """Save particle objects contained in part_list in the stream fsock""" 28 29 if not isinstance(part_list, base_objects.ParticleList): 30 raise ValueError, \ 31 "Object %s is not a valid ParticleList" % repr(part_list) 32 33 fsock.write("particles = [\n") 34 35 for part in part_list: 36 if part_list.index(part) != len(part_list) - 1: 37 fsock.write(str(part) + ',') 38 else: 39 fsock.write(str(part)) 40 41 fsock.write("]")
42
43 -def save_interactions(fsock, inter_list):
44 """Save interaction objects contained in inter_list in the stream fsock""" 45 46 if not isinstance(inter_list, base_objects.InteractionList): 47 raise ValueError, \ 48 "Object %s is not a valid InteractionList" % repr(inter_list) 49 50 fsock.write("interactions = [\n") 51 52 for inter in inter_list: 53 if inter_list.index(inter) != len(inter_list) - 1: 54 fsock.write(str(inter) + ',') 55 else: 56 fsock.write(str(inter)) 57 58 fsock.write("]")
59
60 -def save_model(path, model):
61 """Save a full model in directory path (try to create if necessary).""" 62 63 if not isinstance(model, base_objects.Model): 64 raise ValueError, \ 65 "Object %s is not a valid Model" % repr(model) 66 67 if not isinstance(path, str): 68 raise ValueError, \ 69 "Object %s is not a path string" % repr(path) 70 71 if not os.path.isdir(path): 72 logger.warning("Path %s does not exist, try to make it..." % str(path)) 73 try: 74 os.mkdir(path) 75 except IOError, (errno, strerror): 76 logger.error("I/O error (%s): %s" % (errno, strerror)) 77 return None 78 79 print "Saving particles...", 80 files.write_to_file(os.path.join(path, 'particles.py'), 81 save_particles, 82 model['particles']) 83 print "%i particles saved to %s" % (len(model['particles']), 84 os.path.join(path, 'particles.py')) 85 86 print "Saving interactions...", 87 files.write_to_file(os.path.join(path, 'interactions.py'), 88 save_interactions, 89 model['interactions']) 90 print "%i interactions saved to %s" % (len(model['interactions']), 91 os.path.join(path, 'interactions.py'))
92