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 MadGraph5_aMC@NLO Development team and Contributors 
 4  # 
 5  # This file is a part of the MadGraph5_aMC@NLO 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 MadGraph5_aMC@NLO license which should accompany this  
10  # distribution. 
11  # 
12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
13  # 
14  ################################################################################ 
15   
16  """Function to save model files.""" 
17   
18  from __future__ import absolute_import 
19  from __future__ import print_function 
20  import logging 
21  import os 
22   
23  import madgraph.iolibs.files as files 
24  import madgraph.core.base_objects as base_objects 
25   
26  logger = logging.getLogger('madgraph.save_model') 
27   
28 -def save_particles(fsock, part_list):
29 """Save particle objects contained in part_list in the stream fsock""" 30 31 if not isinstance(part_list, base_objects.ParticleList): 32 raise ValueError("Object %s is not a valid ParticleList" % repr(part_list)) 33 34 fsock.write("particles = [\n") 35 36 for part in part_list: 37 if part_list.index(part) != len(part_list) - 1: 38 fsock.write(str(part) + ',') 39 else: 40 fsock.write(str(part)) 41 42 fsock.write("]")
43
44 -def save_interactions(fsock, inter_list):
45 """Save interaction objects contained in inter_list in the stream fsock""" 46 47 if not isinstance(inter_list, base_objects.InteractionList): 48 raise ValueError("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("Object %s is not a valid Model" % repr(model)) 65 66 if not isinstance(path, str): 67 raise ValueError("Object %s is not a path string" % repr(path)) 68 69 if not os.path.isdir(path): 70 logger.warning("Path %s does not exist, try to make it..." % str(path)) 71 try: 72 os.mkdir(path) 73 except IOError as xxx_todo_changeme: 74 (errno, strerror) = xxx_todo_changeme.args 75 logger.error("I/O error (%s): %s" % (errno, strerror)) 76 return None 77 78 print("Saving particles...", end=' ') 79 files.write_to_file(os.path.join(path, 'particles.py'), 80 save_particles, 81 model['particles']) 82 print("%i particles saved to %s" % (len(model['particles']), 83 os.path.join(path, 'particles.py'))) 84 85 print("Saving interactions...", end=' ') 86 files.write_to_file(os.path.join(path, 'interactions.py'), 87 save_interactions, 88 model['interactions']) 89 print("%i interactions saved to %s" % (len(model['interactions']), 90 os.path.join(path, 'interactions.py')))
91