Package models :: Module model_reader
[hide private]
[frames] | no frames]

Source Code for Module models.model_reader

  1  ################################################################################ 
  2  # 
  3  # Copyright (c) 2010 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  """Module to allow reading a param_card and setting all parameters and 
 16  couplings for a model""" 
 17   
 18  from __future__ import division 
 19   
 20  import array 
 21  import cmath 
 22  import copy 
 23  import itertools 
 24  import logging 
 25  import math 
 26  import os 
 27  import re 
 28   
 29  import madgraph.core.base_objects as base_objects 
 30  import models.check_param_card as card_reader 
 31  from madgraph import MadGraph5Error, MG5DIR 
 32   
 33  ZERO = 0 
 34   
 35  #=============================================================================== 
 36  # Logger for model_reader 
 37  #=============================================================================== 
 38   
 39  logger = logging.getLogger('models.model_reader') 
 40   
 41  #=============================================================================== 
 42  # ModelReader: Used to read a param_card and calculate parameters and 
 43  #              couplings of the model. 
 44  #=============================================================================== 
45 -class ModelReader(base_objects.Model):
46 """Object to read all parameters and couplings of a model 47 """ 48
49 - def default_setup(self):
50 """The particles is changed to ParticleList""" 51 self['coupling_dict'] = {} 52 self['parameter_dict'] = {} 53 super(ModelReader, self).default_setup()
54
55 - def set_parameters_and_couplings(self, param_card = None):
56 """Read a param_card and calculate all parameters and 57 couplings. Set values directly in the parameters and 58 couplings, plus add new dictionary coupling_dict from 59 parameter name to value.""" 60 61 # Extract external parameters 62 external_parameters = self['parameters'][('external',)] 63 64 # Read in param_card 65 if param_card: 66 67 # Check that param_card exists 68 if not os.path.isfile(param_card): 69 raise MadGraph5Error, \ 70 "No such file %s" % param_card 71 72 73 # Create a dictionary from LHA block name and code to parameter name 74 parameter_dict = {} 75 for param in external_parameters: 76 try: 77 dictionary = parameter_dict[param.lhablock.lower()] 78 except KeyError: 79 dictionary = {} 80 parameter_dict[param.lhablock.lower()] = dictionary 81 dictionary[tuple(param.lhacode)] = param 82 83 param_card = card_reader.ParamCard(param_card) 84 85 key = [k for k in param_card.keys() if not k.startswith('qnumbers ') 86 and not k.startswith('decay_table')] 87 88 if set(key) != set(parameter_dict.keys()): 89 raise MadGraph5Error, '''Invalid restriction card (not same block) 90 %s != %s 91 ''' % (set(key), set(parameter_dict.keys())) 92 for block in key: 93 for id in parameter_dict[block]: 94 try: 95 value = param_card[block].get(id).value 96 except: 97 raise MadGraph5Error, '%s %s not define' % (block, id) 98 else: 99 exec("locals()[\'%s\'] = %s" % (parameter_dict[block][id].name, 100 value)) 101 parameter_dict[block][id].value = float(value) 102 103 else: 104 # No param_card, use default values 105 for param in external_parameters: 106 exec("locals()[\'%s\'] = %s" % (param.name, param.value)) 107 108 # Define all functions used 109 for func in self['functions']: 110 exec("def %s(%s):\n return %s" % (func.name, 111 ",".join(func.arguments), 112 func.expr)) 113 114 # Extract derived parameters 115 derived_parameters = [] 116 keys = [key for key in self['parameters'].keys() if \ 117 key != ('external',)] 118 keys.sort(key=len) 119 for key in keys: 120 derived_parameters += self['parameters'][key] 121 122 123 # Now calculate derived parameters 124 for param in derived_parameters: 125 try: 126 exec("locals()[\'%s\'] = %s" % (param.name, param.expr)) 127 except Exception as error: 128 msg = 'Unable to evaluate %s: raise error: %s' % (param.expr, error) 129 raise MadGraph5Error, msg 130 param.value = complex(eval(param.name)) 131 if not eval(param.name) and eval(param.name) != 0: 132 logger.warning("%s has no expression: %s" % (param.name, 133 param.expr)) 134 135 # Correct width sign for Majorana particles (where the width 136 # and mass need to have the same sign) 137 for particle in self.get('particles'): 138 if particle.is_fermion() and particle.get('self_antipart') and \ 139 particle.get('width').lower() != 'zero' and \ 140 eval(particle.get('mass')).real < 0: 141 exec("locals()[\'%(width)s\'] = -abs(%(width)s)" % \ 142 {'width': particle.get('width')}) 143 144 # Extract couplings 145 couplings = sum(self['couplings'].values(), []) 146 # Now calculate all couplings 147 for coup in couplings: 148 exec("locals()[\'%s\'] = %s" % (coup.name, coup.expr)) 149 coup.value = complex(eval(coup.name)) 150 if not eval(coup.name) and eval(coup.name) != 0: 151 logger.warning("%s has no expression: %s" % (coup.name, 152 coup.expr)) 153 154 # Set parameter and coupling dictionaries 155 156 self.set('parameter_dict', dict([(param.name, param.value) \ 157 for param in external_parameters + \ 158 derived_parameters])) 159 160 # Add "zero" 161 self.get('parameter_dict')['ZERO'] = complex(0.) 162 163 self.set('coupling_dict', dict([(coup.name, coup.value) \ 164 for coup in couplings]))
165