Skip to content

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.

Overview of getEnergyTensor.py

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.

Function Definition and Parameters

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 is False).
    • diffOrder: Order of finite difference ('second' or 'fourth', default is 'fourth').

Input Validation and Metric Verification

    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 diffOrder is either 'second' or 'fourth'.
    • Uses verifyTensor to check if the metric is valid.
    • Converts metric index to "covariant" if necessary using changeTensorIndex.

GPU and CPU Computation

    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 met2den or met2den2 depending on diffOrder.
    • Retrieves results from GPU.
  • CPU Computation:
    • Directly calls met2den or met2den2 based on diffOrder.

Struct Creation

    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.

Clone this wiki locally