Page 1 of 2

Compute or store the CRS matrices

Posted: 08 Nov 2023, 18:49
by kbatra
Greetings,

I am writing to seek your assistance in computing and storing the CRS matrices. I have successfully run a waveguide problem, compiled the vectorhelmholtz.f90 file, executed the .sif file, and visualized the vtu file in ParaView. Now, I would like to learn how to extract the CRS matrices from my results. I'm sure that experts in the forum can provide valuable guidance, and I would greatly appreciate their assistance.

Thank you in advance.

Best regards,
K. Batra

Re: Compute or store the CRS matrices

Posted: 09 Nov 2023, 18:50
by kbatra
Good evening,

Just an update that I tried to make some modifications in the VectorHelmholtz.F90 (see attached) in order to save the CRS matrix. It complies well and provides VectorHelmholtz.so and vectorhelmholtzutils.mod binary files. But later when I execute ElmerSolver case.sif, it crashes and render error "Program received signal SIGSEGV: Segmentation fault - invalid memory reference". Hope you can assist me to find my mistake and sort out my problem.

Thanks!
K Batra

P.S. I attach case.sif and mesh file as well

Re: Compute or store the CRS matrices

Posted: 09 Nov 2023, 22:48
by kevinarden
Are you sure you called the files you compiled? The sif is the same as the one that would call the Elmer routines. Did you replace the compiled version in your standard Elmer Library? Normally I would change the name of the compiled solver to make sure I was calling the correct one.

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 11:40
by kbatra
Thanks for your answer, Yes I did replace the name.

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 13:19
by kevinarden
I added some debug statements and they show ! are my comments

calling save CRS Matrix ! program calls the new subroutine
In SaveCRS subroutine ! program is in the new subroutine
A= 1756 ! there are 1756 rows
num_elements= 7 ! there are 7 elements

! error occurs before subroutine exits.

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 13:26
by kevinarden
it fails on this line, no success is reported. so cols(j) causes seg fault.
earlier in the code cols is used Cols(A % Rows(i):A % Rows(i+1)-1)
if I do a write(*,*) cols
cols is 0

rows => A % Cols(A % Rows(i):A % Rows(i+1)-1)
write(*,*) "num_elements=",num_elements
! Write matrix elements for the current row
DO j = 1, num_elements
row = i
write(*,*) "check this cols(j)"
col = cols(j)
Write(*,*) "success"

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 13:41
by kevinarden
Don't know if this the write matrix but it runs without error, need to decide how to assign col

! Loop over rows
DO i = 1, A % NumberOfRows
! Write diagonal element for each row
WRITE(10, *) 'Diagonal element for row ', i, ' is ', A % Values(A % Diag(i))

! Get the row indices and values for the current row
num_elements = A % Rows(i+1) - A % Rows(i)
rows => A % Cols(A % Rows(i):A % Rows(i+1)-1)
write(*,*) "num_elements=",num_elements
! Write matrix elements for the current row
DO j = 1, num_elements
row = i
col = j
val = A % Values(rows(j))
WRITE(10, *) 'Matrix element at position: ', row, col, ' is ', val
END DO
END DO

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 13:53
by kbatra
Thanks Kevin for your efforts. Really appreciate it. In the mean time I worked as well and please have a look at mine (seems worked)

! Call SaveCRSMatrix to save the CRS matrix information
CALL SaveCRSMatrix(Solver % Matrix, 'matrix_output.txt')

!------------------------------------------------------------------------------
END SUBROUTINE GlobalSol
!------------------------------------------------------------------------------

SUBROUTINE SaveCRSMatrix(A, gamma)
TYPE(Matrix_t), POINTER :: A
CHARACTER(LEN=*), INTENT(IN) :: gamma
INTEGER :: i, j, num_elements
INTEGER, DIMENSION(:), POINTER :: rows

! Open files for writing
OPEN(UNIT=10, FILE=gamma // '_rows.txt', STATUS='replace', ACTION='write', FORM='formatted')
OPEN(UNIT=20, FILE=gamma // '_cols.txt', STATUS='replace', ACTION='write', FORM='formatted')
OPEN(UNIT=30, FILE=gamma // '_vals.txt', STATUS='replace', ACTION='write', FORM='formatted')

! Write total rows, cols, vals to the summary files
WRITE(10, *) 'Total Rows: ', A % NumberOfRows
WRITE(20, *) 'Total Columns: ', A % NumberOfRows
WRITE(30, *) 'Total Values: ', A % NumberOfRows

! Loop over rows
DO i = 1, A % NumberOfRows
! Get the row indices and values for the current row
num_elements = A % Rows(i+1) - A % Rows(i)
rows => A % Cols(A % Rows(i):A % Rows(i+1)-1)

! Write the column indices (A % Cols) and values (A % Values) for the current row
DO j = 1, num_elements
WRITE(10, *) rows(j)
WRITE(20, *) A % Cols(A % Rows(i) + j - 1)
WRITE(30, *) A % Values(A % Rows(i) + j - 1)
END DO
END DO

! Close the files
CLOSE(UNIT=10)
CLOSE(UNIT=20)
CLOSE(UNIT=30)
END SUBROUTINE SaveCRSMatrix

But I would like to simply it better.. utilizing size (replacing with A % NumberOfRows)
for example:
DO i = 1, size(A % Rows)
WRITE(10, *) A % Rows(i) and this should go for cols and vals as well. So far it didnt work out, maybe you can help here.

Thanks
K Batra

Re: Compute or store the CRS matrices

Posted: 10 Nov 2023, 16:01
by mika
kbatra wrote: 08 Nov 2023, 18:49 I would like to learn how to extract the CRS matrices
You might try to use the ready subroutine SaveLinearSystem contained in the file SolverUtils.F90. For example, you could try to add the following lines to the solver section

linear system save = true
linear system save slot = assembly

so that the Elmer solver would call it.

-- Mika

Re: Compute or store the CRS matrices

Posted: 17 Nov 2023, 17:20
by kbatra
Thank you for your valuable input. I will try and test it :)

Best,
K Batra