Masoud,
> Here is a part of the tutorial code for example 8 to calculate stiffness
> matrix of an element:
> Screenshot 2021-10-15 at 12.48.39.png
>
> And here is my equivalent code for the same purpose with a matrix-based viewpoint:
> Screenshot 2021-10-15 at 23.12.13.png
>
> it is based on the following formulation for a 3D elastic problem:
> thumbnail_image.png
> By "slow", I mean in comparison with the same code in tutorial (example 8). I
> measured the lines of the code, and I am sure the difference is for the
> mentioned part of the code. To be more accurate: the operations on matrices,
> as follow in the second image:
> B[I].Tmmult(tmpM,D);
> tmpM.mmult(BDB,B[J]);
You could probably make the code substantially faster already if you moved the
declaration of the B, tmpM, and BDB matrices out of the loop over the
quadrature points, and simply set them to zero inside the loop. Creating these
variables requires allocating memory every time you do one iteration over the
quadrature points, which is expensive.
But in the end, compare what work you are doing in your loop with what the
equivalent code in step-8 is doing, and you shouldn't be surprised by your
observation. step-8 only does 2 (and if i==j, then 3) tensor contractions for
each index i,j,q. You are allocating and releasing memory, and doing two
products between 6x3 and 3x3 matrices, plus a lot of index work. It does not
surprise me that this is slow.