# About extracting Stiffness Matrix

**URL:** https://community.freefem.org/t/about-extracting-stiffness-matrix/2023
**Category:** General Discussion
**Created:** [September 25, 2022, 8:11am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023 "2022-09-25T08:11:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)
#### Post date: [September 25, 2022, 8:11am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/1 "2022-09-25T08:11:47Z")

</div>

Dear all，  
I want to use FreeFEM to implement 99 lines of code in topology optimization design.  
Code Reference： [An 89-line code for geometrically nonlinear topology optimization written in FreeFEM | SpringerLink](https://link.springer.com/article/10.1007/s00158-020-02733-x)

When extracting the stiffness matrix, it prompts the following errors.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/0/086344c7a4937566b14a6cc6b4b52b0ef39980b6.png)  
It seems that my vElasticity is not defined before. But I did define it. I don’t know why.  
Here’s my code  
[1.edp](https://community.freefem.org/uploads/short-url/wGLG8v8bmXTZkU1uWzQg5SSZgaK.edp) (2.1 KB)

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [September 26, 2022, 6:46am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/2 "2022-09-26T06:46:32Z")

</div>

Macros need to end with `//` symbols, e.g.,

```auto
macro Epsilon(ux, uy) [dx(ux), dy(uy), (dy(ux)+dx(uy))/sqrt2] //
macro Divergence(ux, uy) (dx(ux) + dy(uy)) //

```

is the proper way to define the macros.

---

<div class="post-metadata">

### Author: ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)
#### Post date: [September 27, 2022, 8:29am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/3 "2022-09-27T08:29:11Z")

</div>

Thank you very much. That is right.  
I have another question.I want to get [ux, uy] \* [ux, uy] '.  
So I entered the following code, but it didn’t seem to work.

sens = [ux,uy]\*[ux,uy]';  
Can you help me find out what the problem is？

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [September 27, 2022, 9:17am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/4 "2022-09-27T09:17:40Z")

</div>

Could you please share your code?

---

<div class="post-metadata">

### Author: ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)
#### Post date: [September 27, 2022, 1:53pm UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/5 "2022-09-27T13:53:59Z")

</div>

[7.edp](https://community.freefem.org/uploads/short-url/qkEqpswt4hHhkJNJjqxzoneiKI4.edp) (1.9 KB)

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [September 27, 2022, 2:09pm UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/6 "2022-09-27T14:09:57Z")

</div>

The correct way to calculate it is the following:

```auto
real[int] auxVec(ux[].n);
auxVec = k*ux[];
real com = auxVec'*ux[];
cout << "com=" << com << endl;

```

`[ux,uy]` is a vectorial finite element function, while `ux[]` is a vector that contains all the values at the gridpoints of `[ux,uy]`. Consult the documentation for more details.

---

<div class="post-metadata">

### Author: ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)
#### Post date: [September 28, 2022, 4:17am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/7 "2022-09-28T04:17:15Z")

</div>

I’m sorry to bother you again.  
I have the following code. I print ux uy, they are the same. I think they both represent all the values at the gridpoints of [ux,uy].As you said above.  
But if I modify the code as follows:  
Replace

fespace Vh(Th,[P1,P1]);  
Vh [ux,uy],[uu,vv];

as  
fespace Vh(Th,P1);  
Vh ux,uy,uu,vv;

The printed ux and uy seem different.  
What is the difference between the two?

---

<div class="post-metadata">

### Author: ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)
#### Post date: [September 28, 2022, 10:22am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/8 "2022-09-28T10:22:18Z")

</div>

What do you mean by printing?  
If you write

```auto
fespace Vh(Th,[P1,P1]);
Vh [ux,uy],[uu,vv];

```

either `ux[]` or `uy[]` is a vector containing all the function values at the grid points for a `[P1,P1]`. If you do

```auto
fespace Vh(Th,P1);
Vh ux,uy,uu,vv;

```

then `ux[]` and `uy[]` is each a vector of values of a `P1` FE space.

---

<div class="post-metadata">

### Author: ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)
#### Post date: [September 30, 2022, 9:37am UTC](https://community.freefem.org/t/about-extracting-stiffness-matrix/2023/9 "2022-09-30T09:37:09Z")

</div>

Oh, I see,thank you very much.
