Save od ofstream stifness matrix asn file.txt

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

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

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

remark, i can save sparse matrix see

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;
}

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

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;
}