# Save od ofstream stifness matrix asn file.txt

**URL:** <https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045>\
**Category:** General Discussion\
**Created:** [August 22, 2025, 10:40am UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045 "2025-08-22T10:40:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karzan](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@karzan](https://community.freefem.org/u/karzan)\
**Post date:** [August 22, 2025, 10:40am UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045/1 "2025-08-22T10:40:58Z")

</div>

I have a question regarding save or record Stiffness Matrix in Freefem++! here is my question and freefem++ code

To save all entries in the matrix A or get the stiffness matrix or sparse matrix from, I wrote this code, my question what the wrong with this code or how I can fix it to get whole matrix A  
///////////////////////////////////////////////////////////////////Freefem++code/////////

int n = 20;  
mesh Th=square(n,n);  
fespace Vh(Th,P1);

Vh u,v;  
real err, hh;

func f = 5.0/4.0 \* pi \* pi \* sin(pi \* x) \* sin(pi \* y / 2.0);  
func h = (-pi)/2.0 \* sin(pi \* x);  
func g = sin(pi \* x) \* sin(pi \* y / 2.0);  
// for error estimation  
func sol = sin(pi \* x) \* sin(pi \* y / 2.0);  
func solx = pi \* cos(pi \* x) \* sin(pi \* y / 2.0);  
func soly = (pi / 2.0) \* sin(pi \* x) \* cos(pi \* y / 2.0);

varf aa(u,v) = int2d(Th)( dx(u)\*dx(v)+dy(u)\*dy(v) )  
+ on(2,3,4,u=g); // u=1 is enough to say which is Dirichlet node  
varf external(u,v) = int2d(Th)( f\*v ) + int1d(Th,1) (h \* v)  
+ on(2,3,4,u=g); // inhomogenoues Dirichlet data are given  
real tgv=1.0e+30;  
matrix A = aa(Vh,Vh,tgv=tgv,solver=CG);  
real[int] ff = external(0,Vh,tgv=tgv); // containing Dirichlet data with tgv

u = A^-1 \* ff;

hh = 1.0 / real(n) \* sqrt(2.0);

// int2d uses qf5pT : 5th order integration quadrature  
err = int2d(Th)( (dx(u) - solx) \* (dx(u) - solx) +  
(dy(u) - soly) \* (dy(u) - soly) +  
(u - sol) \* (u - sol));  
err = sqrt(err);

cout \<\< “DOF=” \<\< u.n \<\< “\t h=” \<\< hh \<\< " err-H1=" \<\< err \<\< endl;  
// I Need to symmetrize first so all coefficients exist  
A = A + A’;

int n = Vh.ndof;  
ofstream fout(“stiffness\_matrix\_dense.txt”);  
fout \<\< n \<\< " " \<\< n \<\< endl;  
for (int i=0; i\<n; i++) {  
for (int j=0; j\<n; j++) {  
fout \<\< A(i,j) \<\< " ";  
}  
fout \<\< endl;  
}  
/////////////////////////////////////////  
by the way in this link you don’t mentioned the way how save the whole Stiffness matrix

> **[stiffness\_matrix](https://people.sc.fsu.edu/~jburkardt/freefem_src/stiffness_matrix/stiffness_matrix.html)**

---

<div class="post-metadata">

**Author:** ![vlaude](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/vlaude/32/939_2.png) [@vlaude](https://community.freefem.org/u/vlaude)\
**Post date:** [August 25, 2025, 7:32am UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045/2 "2025-08-25T07:32:41Z")

</div>

Hello karzan,

A is a sparse matrix and an object of type ‘matrix’. The internal storage is not the way you wrote it (A(i,j)), because this is very inefficient. Have a look at the documentation: [https://doc.freefem.org/references/types.html#matrix](https://doc.freefem.org/references/types.html#matrix)

If you want to access matrix elements directly, consider looking at code examples in [https://doc.freefem.org/examples/developers.html#examplematrixoperations](https://doc.freefem.org/examples/developers.html#examplematrixoperations)

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [August 25, 2025, 9:30am UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045/3 "2025-08-25T09:30:28Z")

</div>

remark, i can save sparse matrix see

> <https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/plugin/SaveHB_sample.edp>

---

<div class="post-metadata">

**Author:** ![karzan](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@karzan](https://community.freefem.org/u/karzan)\
**Post date:** [August 25, 2025, 3:05pm UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045/4 "2025-08-25T15:05:44Z")

</div>

Many thanks, dear Prof. Frédéric Hecht, for your quick answer! I tried this code, but the issue comes from my FreeFEM++ version! I was using a very old one, in which the command load “SaveHB” is undefined. Now I wrote the code like this do you think this right?

//////////////////////////////////////

// Build FE mesh and space  
mesh Th = square(5,5);  
fespace Vh(Th,P1);  
Vh u, v;

// Assemble stiffness matrix (sparse)  
varf a(u,v) = int2d(Th)( dx(u)\*dx(v) + dy(u)\*dy(v) );  
matrix A = a(Vh,Vh);

// === Dense export (full matrix with zeros) ===  
// 1) Extract triplets (row, col, value)  
int[int] I, J;  
real[int] C;  
[I, J, C] = A;

// 2) Dimensions (use ndof of the space)  
int n = Vh.ndof;

// 3) Build dense matrix initialized to 0  
real[int,int] Adense(n,n); // zeros by default

// 4) Scatter triplets into dense array  
for (int k = 0; k \< C.n; ++k) {  
int i = I[k];  
int j = J[k];  
// Guard in case I/J come from boundary eliminations etc.  
if (0 \<= i && i \< n && 0 \<= j && j \< n) {  
Adense(i,j) = C[k];  
}  
}

// 5) Save dense matrix to txt  
ofstream fd(“stiffness\_matrix\_full.txt”);  
fd \<\< n \<\< " " \<\< n \<\< endl; // header: rows cols  
for (int i = 0; i \< n; ++i) {  
for (int j = 0; j \< n; ++j) {  
fd \<\< Adense(i,j) \<\< " ";  
}  
fd \<\< endl;  
}

//////////////////////////////////////////////////

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [August 27, 2025, 9:24am UTC](https://community.freefem.org/t/save-od-ofstream-stifness-matrix-asn-file-txt/4045/5 "2025-08-27T09:24:00Z")

</div>

I think, upgrade to last version.

or

try

ofstream fd(“stiffness\_matrix\_full.txt”);

A.COO; // put the sparce matrix in COO format .  
fd \<\< A \<\< endl;  
}
