-
Notifications
You must be signed in to change notification settings - Fork 1
getEnergyTensor
LeetHappyfeet edited this page May 20, 2024
·
2 revisions
The function getEnergyTensor takes a metric tensor, verifies it, optionally converts it for GPU computation, computes the stress-energy tensor using finite difference methods, and returns the results in a structured format.
This script defines a function getEnergyTensor that converts a metric tensor into a stress-energy tensor. The function provides options for GPU computation and different orders of finite difference methods.
def getEnergyTensor(metric, tryGPU=False, diffOrder='fourth'):
"""
Converts the metric into the stress energy tensor
Parameters:
- metric: A metric dictionary containing tensor components and other information
- tryGPU: A flag indicating whether to use GPU computation (default: False)
- diffOrder: Order of finite difference, either 'second' or 'fourth' (default: 'fourth')
Returns:
- energy: A dictionary representing the energy tensor
"""-
Parameters:
-
metric: Dictionary containing the components of the metric tensor and related data. -
tryGPU: Boolean flag for GPU computation (default isFalse). -
diffOrder: Order of finite difference ('second'or'fourth', default is'fourth').
-
if diffOrder not in ['second', 'fourth']:
raise ValueError("Order Flag Not Specified Correctly. Options: 'second' or 'fourth'")
if not verifyTensor(metric, 1):
raise ValueError("Metric is not verified. Please verify metric using verifyTensor(metric).")
if metric['index'] != "covariant":
metric = changeTensorIndex(metric, "covariant")
print(f"Changed metric from {metric['index']} index to {'covariant'} index")-
Validation:
- Ensures
diffOrderis either'second'or'fourth'. - Uses
verifyTensorto check if the metric is valid. - Converts metric index to "covariant" if necessary using
changeTensorIndex.
- Ensures
if tryGPU:
metricTensorGPU = [[np.array(component, dtype='float32') for component in row] for row in metric['tensor']]
metricTensorGPU = np.array(metricTensorGPU)
metric['scaling'] = np.array(metric['scaling'], dtype='float32')
if diffOrder == 'fourth':
enDenGPU = met2den(metricTensorGPU, metric['scaling'])
elif diffOrder == 'second':
enDenGPU = met2den2(metricTensorGPU, metric['scaling'])
energyTensor = [[np.array(component) for component in row] for row in enDenGPU]
else:
if diffOrder == 'fourth':
energyTensor = met2den(metric['tensor'], metric.get('scaling', 1.0))
elif diffOrder == 'second':
energyTensor = met2den2(metric['tensor'], metric['scaling'])-
GPU Computation:
- Converts metric tensor components to GPU arrays.
- Calls
met2denormet2den2depending ondiffOrder. - Retrieves results from GPU.
-
CPU Computation:
- Directly calls
met2denormet2den2based ondiffOrder.
- Directly calls
energy = {
'type': 'Stress-Energy',
'tensor': energyTensor,
'coords': metric['coords'],
'index': 'contravariant',
'order': diffOrder,
'name': metric['name'],
'date': np.datetime64('today')
}
return energy- Creates a dictionary representing the stress-energy tensor with relevant details such as type, tensor components, coordinates, index type, finite difference order, metric name, and the current date.