Skip to content

Elastic Tensors

Heavily inspired by fxcoudert's ELATE, and since it is a modified version of that, this is subject to the same MIT license.

See the page, and the source here:

    http://progs.coudert.name/elate
    https://github.com/fxcoudert/elate

ElasticTensor

Class to represent an elastic tensor, along with methods to access it

from_string(s) classmethod

Initialize the elastic tensor from a string

Source code in chmpy/ext/elastic_tensor.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@classmethod
def from_string(cls, s):
    """Initialize the elastic tensor from a string"""
    if not s:
        raise ValueError("no matrix was provided")

    if isinstance(s, str):
        # Remove braces and pipes
        s = s.replace("|", " ").replace("(", " ").replace(")", " ")

        # Remove empty lines
        lines = [line for line in s.split("\n") if line.strip()]
        if len(lines) != 6:
            raise ValueError("should have six rows")

        # Convert to float
        try:
            mat = [[float(x) for x in line.split()] for line in lines]
        except:
            raise ValueError("not all entries are numbers")
    return cls(mat)