CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

Export matrix of coefficients into a .txt file

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 8, 2020, 05:45
Default Export matrix of coefficients into a .txt file
  #1
New Member
 
Join Date: Sep 2020
Posts: 9
Rep Power: 5
AndreaCFD is on a distinguished road
Dear members,

I am new to OpenFoam and I am trying to modify the icoFoam solver to export the matrix of coefficients UEqn into a text file readable by Matlab.

What is the easiest way? I am really new to C++ too and I can't compile the solver without errors. May you help?

Thanks in advance,

Andrea
AndreaCFD is offline   Reply With Quote

Old   September 8, 2020, 08:17
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by AndreaCFD View Post
Dear members,

I am new to OpenFoam and I am trying to modify the icoFoam solver to export the matrix of coefficients UEqn into a text file readable by Matlab.

What is the easiest way? I am really new to C++ too and I can't compile the solver without errors. May you help?

Thanks in advance,

Andrea

Changing any solver to simply dump out a matrix of coefficients is really not a good idea. It falls apart when you want to try the same for a different field, for a slightly different solver, or when you upgrade versions. Provided that you do not yet wish to examine the coefficients for the pressure equation, you can implement your writing routine as a matrix constraint that does not modify the coefficients, but only prints them out. If you are new to OpenFOAM and to C++, it will not be completely easy in any case. You need to walk the OpenFOAM sparse matrix storage, and also handle processor-to-processor connections.

If you want a bit of code to start from, I have revived some old code (temporarily?) that probably does most of what you want, or at least can give you some ideas. It dumps coefficients in a MatrixMarket format. Although it is from 2018, it should compile OK with OpenFOAM-v1906,1912,2006. I cannot remember if there is anything missing or broken in it.


https://develop.openfoam.com/modules...src/dumpMatrix
olesen is offline   Reply With Quote

Old   September 9, 2020, 21:28
Default
  #3
New Member
 
Duc Anh
Join Date: Dec 2018
Posts: 22
Rep Power: 7
anhkenyt is on a distinguished road
Quote:
Originally Posted by AndreaCFD View Post
Dear members,

I am new to OpenFoam and I am trying to modify the icoFoam solver to export the matrix of coefficients UEqn into a text file readable by Matlab.

What is the easiest way? I am really new to C++ too and I can't compile the solver without errors. May you help?

Thanks in advance,

Andrea
Hi Andrea,
To start, the document below will give you some simple suggestions
https://www.foamacademy.com/wp-conte...2018matrix.pdf
anhkenyt is offline   Reply With Quote

Old   September 10, 2020, 12:30
Default
  #4
New Member
 
Join Date: Sep 2020
Posts: 9
Rep Power: 5
AndreaCFD is on a distinguished road
Quote:
Originally Posted by anhkenyt View Post
Hi Andrea,
To start, the document below will give you some simple suggestions
https://www.foamacademy.com/wp-conte...2018matrix.pdf
Dear Van Duc Anh,

thanks for the useful documentation. I tried to write the code as follows:

Code:
fvVectorMatrix UEqn
        (
			fvm::laplacian(nu, U)-
			fvm::div(phi_zero, U)-
			fvc::div(phi,U_zero)
        );

		
		label NC = mesh.nCells(); //Number of cells
		simpleMatrix<scalar> A(NC); //Coeff.matrix

		// Initialization of matrix
		for(label i=0; i<NC; i++)
			{
				A.source()[i] = 0.0;
				for(label j=0; j<NC; j++)
					{
						A[i][j] = 0.0;
					}
			}
	
		// Assigning diagonal coefficients
		for(label i=0; i<NC; i++)
			{
				A[i][i] = UEqn.diag()[i];
			}
	
		// Assigning off-diagonal coefficients
		for(label faceI=0; faceI<UEqn.lduAddr().lowerAddr().size(); faceI++)
			{
				label l = UEqn.lduAddr().lowerAddr()[faceI];
				label u = UEqn.lduAddr().upperAddr()[faceI];
				A[l][u] = UEqn.upper()[faceI];
				A[u][l] = UEqn.upper()[faceI];
			}
		// Assigning contribution from BC
		forAll(U.boundaryField(), patchI)
			{
				const fvPatch &pp = U.boundaryField()[patchI].patch();
				forAll(pp, faceI)
					{
						label cellI = pp.faceCells()[faceI];
						A[cellI][cellI] += UEqn.internalCoeffs()[patchI][faceI];
						A.source()[cellI] += UEqn.boundaryCoeffs()[patchI][faceI];
					}
			}
	
		Info << "\n==Coefficients of Matrix A=="<< endl;
		for(label i=0; i<NC; i++)
			{
				for(label j=0; j<NC; j++)
					{
						Info<< A[i][j] << " ";
					}
				Info<< A.source()[i] << endl;
			}
But I get an error I am not able to understand:

[/CODE] error: no match for ‘operator+=’ (operand types are ‘double’ and ‘Foam::Vector<double>’)
A[cellI][cellI] += UEqn.internalCoeffs()[patchI][faceI]; [/CODE]

How do I overcome it?
Thanks in Advance

Andrea
AndreaCFD is offline   Reply With Quote

Old   September 11, 2020, 02:55
Default
  #5
Senior Member
 
Domenico Lahaye
Join Date: Dec 2013
Posts: 729
Blog Entries: 1
Rep Power: 17
dlahaye is on a distinguished road
https://openfoamwiki.net/images/1/18/GdbOF-Manual.pdf
dlahaye is offline   Reply With Quote

Old   August 10, 2022, 13:54
Default
  #6
New Member
 
Join Date: Jun 2022
Posts: 19
Rep Power: 3
TommyJ is on a distinguished road
@Mr. Olesen, The link you provided is broken. Was there a problem with the code?

@Mr. Lahaye, can gdbOf print the coefficients in a parallel run?
TommyJ is offline   Reply With Quote

Old   August 11, 2022, 02:51
Default
  #7
Senior Member
 
Domenico Lahaye
Join Date: Dec 2013
Posts: 729
Blog Entries: 1
Rep Power: 17
dlahaye is on a distinguished road
@tommyJ: not sure.

Possibly yes: gdb seems to support parallel runs.

Possibly no: in parallel, OpenFoam stores the matrix per processor in separate (diagonal and off-diagonal blocks), and the macros provided in OFgdb might not extend to this situation. Possibly OFgdb can be extended to treat this case.
dlahaye is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[swak4Foam] Installation Problem with OF 6 version Aurel OpenFOAM Community Contributions 14 November 18, 2020 16:18
[foam-extend.org] Problems installing foam-extend-4.0 on openSUSE 42.2 and Ubuntu 16.04 ordinary OpenFOAM Installation 19 September 3, 2019 18:13
A CFX-POST error (ver 14.5.7) wangyflp88 CFX 2 July 22, 2017 00:17
[swak4Foam] swak4foam building problem GGerber OpenFOAM Community Contributions 54 April 24, 2015 16:02
[swak4Foam] build problem swak4Foam OF 2.2.0 mcathela OpenFOAM Community Contributions 14 April 23, 2013 13:59


All times are GMT -4. The time now is 09:51.