+ Reply to Thread
Results 1 to 3 of 3

VBA code for transforming a 4th rank tensor represented by a 6x6 matrix

  1. #1
    Registered User
    Join Date
    02-07-2023
    Location
    London
    MS-Off Ver
    Excel 365
    Posts
    2

    VBA code for transforming a 4th rank tensor represented by a 6x6 matrix

    I have written a code in VBA (derived from python) that takes a 6x6 matrix input and rotates it by 45 degrees. Unfortunately the code returns nothing and I can't see where I have gone wrong:

    Function transform_elastic_tensor_2(elastic_tensor As Variant)

    Dim transform(1 To 3, 1 To 3) As Double
    Dim C_ijkl(1 To 3, 1 To 3, 1 To 3, 1 To 3) As Double
    Dim new_C_ijkl(1 To 3, 1 To 3, 1 To 3, 1 To 3) As Double
    Dim indices(1 To 9, 1 To 2) As Integer
    Dim index_matrix(1 To 3, 1 To 3) As Integer
    Dim new_elastic_tensor(1 To 6, 1 To 6) As Double
    Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer, n As Integer, ij(1 To 2) As Integer, kl(1 To 2) As Integer

    'Rotation Matrix
    transform(1, 1) = 0.7071068
    transform(1, 2) = -0.7071068
    transform(1, 3) = 0
    transform(2, 1) = 0.7071068
    transform(2, 2) = 0.7071068
    transform(2, 3) = 0
    transform(3, 1) = 0
    transform(3, 2) = 0
    transform(3, 3) = 1

    'Defining 4th rank tensor
    For i = 1 To 3
    For j = 1 To 3
    For k = 1 To 3
    For l = 1 To 3
    C_ijkl(i, j, k, l) = 0
    Next l
    Next k
    Next j
    Next i

    'Relation of 4th rank tensor to 6x6 2D representation
    indices(1, 1) = 0: indices(1, 2) = 0
    indices(2, 1) = 1: indices(2, 2) = 1
    indices(3, 1) = 2: indices(3, 2) = 2
    indices(4, 1) = 1: indices(4, 2) = 2
    indices(5, 1) = 2: indices(5, 2) = 0
    indices(6, 1) = 0: indices(6, 2) = 1
    indices(7, 1) = 2: indices(7, 2) = 1
    indices(8, 1) = 0: indices(8, 2) = 2
    indices(9, 1) = 1: indices(9, 2) = 0

    index_matrix(1, 1) = 1: index_matrix(1, 2) = 6: index_matrix(1, 3) = 5
    index_matrix(2, 1) = 6: index_matrix(2, 2) = 2: index_matrix(2, 3) = 4
    index_matrix(3, 1) = 5: index_matrix(3, 2) = 4: index_matrix(3, 3) = 3

    For i = 1 To 3
    For j = 1 To 3
    For k = 1 To 3
    For l = 1 To 3
    m = index_matrix(i, j) - 1
    n = index_matrix(k, l) - 1
    C_ijkl(i, j, k, l) = elastic_tensor(m, n)
    Next l
    Next k
    Next j
    Next i

    'Transformation of 4th rank tensor
    For i = 1 To 3
    For j = 1 To 3
    For k = 1 To 3
    For l = 1 To 3
    For m = 1 To 3
    For n = 1 To 3
    new_C_ijkl(i, j, k, l) = new_C_ijkl(i, j, k, l) + transform(m, i) * transform(n, j) * C_ijkl(m, n, k, l)
    Next n
    Next m
    Next l
    Next k
    Next j
    Next i

    'Relation of transformed 4th rank tensor to 6x6 2D representation
    For i = 1 To 9
    ij(1) = indices(i, 1) + 1
    ij(2) = indices(i, 2) + 1

    For j = 1 To 9
    kl(1) = indices(j, 1) + 1
    kl(2) = indices(j, 2) + 1

    For k = 1 To 3
    For l = 1 To 3
    For m = 1 To 6
    For n = 1 To 6
    new_elastic_tensor(index_matrix(ij(1), ij(2)), index_matrix(kl(1), kl(2))) = new_C_ijkl(ij(1), ij(2), k, l)
    Next n
    Next m
    Next l
    Next k
    Next j
    Next i

    transform_elastic_tensor_2 = new_elastic_tensor
    End Function

    The analogous python code is:

    def transform_elastic_tensor(elastic_tensor):

    #Rotation Matrix
    transform = np.array([[0.7071068, -0.7071068, 0],
    [0.7071068, 0.7071068, 0],
    [0, 0, 1]])




    #Defining 4th rank tensor.
    C_ijkl = np.zeros((3, 3, 3, 3))

    indices = [(0,0), (1,1), (2,2), (1,2), (2,0), (0,1), (2,1), (0,2), (1,0)]

    index_matrix = np.array([[1, 6, 5],
    [6, 2, 4],
    [5, 4, 3]])
    #Relation of 4th rank tensor to 6x6 2D representation.
    for i in range(3):
    for j in range(3):
    for k in range(3):
    for l in range(3):
    m = index_matrix[i, j] - 1
    n = index_matrix[k, l] - 1
    C_ijkl[i,j,k,l] = elastic_tensor[m,n]

    new_C_ijkl = np.einsum('ip,jq,kr,ls,pqrs->ijkl', transform, transform, transform, transform, C_ijkl)
    new_elastic_tensor = np.zeros((6,6))
    for m in range(elastic_tensor.shape[0]):
    for n in range(elastic_tensor.shape[1]):
    ij = indices[m]
    kl = indices[n]
    new_elastic_tensor[m, n] = new_C_ijkl[ij + kl]

    return new_elastic_tensor

    #Elastic tensor to be rotated, input tensor values
    elastic_tensor = np.array([[200, 120, 120, 0, 0, 0],
    [120, 200, 120, 0, 0, 0],
    [120, 120, 200, 0, 0, 0],
    [0, 0, 0, 90, 0, 0],
    [0, 0, 0, 0, 90, 0],
    [0, 0, 0, 0, 0, 90]])



    new_elastic_tensor = transform_elastic_tensor(elastic_tensor)
    print(np.round_(new_elastic_tensor, 2))


    The correct answer is:
    [[250. 70. 120. 0. 0. 0.]
    [ 70. 250. 120. 0. 0. 0.]
    [120. 120. 200. 0. 0. 0.]
    [ 0. 0. 0. 90. 0. 0.]
    [ 0. 0. 0. 0. 90. 0.]
    [ 0. 0. 0. 0. 0. 40.]]

    Any help would be greatly appreciated.

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA code for transforming a 4th rank tensor represented by a 6x6 matrix

    Hello. You don't need to complicate your life because Excel already has matrix multiplication.

    You multiply the rotation matrix by the original tensor and you're done.
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  3. #3
    Registered User
    Join Date
    02-07-2023
    Location
    London
    MS-Off Ver
    Excel 365
    Posts
    2

    Re: VBA code for transforming a 4th rank tensor represented by a 6x6 matrix

    Unfortunately for this matrix multiplication you would have to first convert the 6x6 matrix into a 3x3x3x3 tensor. Then rotate each individual component of the tensor and convert back to the new 6x6 matrix.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Progress matrix to be represented in Graph with % completion
    By MaheshK5277 in forum Excel General
    Replies: 5
    Last Post: 01-14-2020, 06:40 AM
  2. Replies: 1
    Last Post: 12-16-2015, 09:57 AM
  3. [SOLVED] Transforming formula into a VBA code
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-23-2012, 12:27 PM
  4. Numbers represented by words
    By saniveva in forum Excel General
    Replies: 3
    Last Post: 02-23-2011, 12:33 AM
  5. true or false being represented
    By thedon_1 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-04-2007, 09:14 AM
  6. identifying the character represented by a square box
    By broro183 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2006, 10:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1