EduardBargues / NonLinearEquationsSolver

This repository aim to provide with an easy-to-use library to solve non-linear systems of equations.
25 stars 7 forks source link

Support for SparseMatrix #2

Closed epsi1on closed 4 years ago

epsi1on commented 4 years ago

Hi, It would be great to have support for sparse matrices in your library. As you know, large structures have a big stiffness matrix and not possible to have their stiffness matrix in dense format. A good implementation is the CSparse.NET, which does handles several features such as ordering, factorization etc.

Thanks

EduardBargues commented 4 years ago

Definitely something to consider. I'll look into it!!

Btw, do you have any other feedback?

epsi1on commented 4 years ago

Thanks for sharing library to opensource, and i think it would be good to have a separated project for unit tests (e.g. NonLinearEquationsSolver.Tests) also a brief example documentation on usage would be very good. I was working on a finite element project named BriefFiniteElemement for some years, which is only a brief linear finite element analysis implementation for solids and structures. Now I'm looking for some opportunities to switch to nonlinear and do make a nonlinear framework.

EduardBargues commented 4 years ago

good for you !! I created this project after my master thesis and did not continue inmproving it because my professional path took me somewhere else. I created another issue to create unit tests. Have a good day!

epsi1on commented 4 years ago

Thanks, Let me know if i can do any help.

EduardBargues commented 4 years ago

Let me first update the repo. It-s quite old. Then, maybe I can split the work so that you can also contribute.

EduardBargues commented 4 years ago

Hello @epsi1on, I invited you to be a collaborator in this repo so you can review the newly created pull-request. Let me know in your comments if you think the issue might be solved with the pull-request 👍

epsi1on commented 4 years ago

OK, Thanks... I think we should do few changes, First is to replace MathNet Matrix<double> with CSparse Matrix which is base class for both dense and sparse matrices in CSparse, it also implements ILinearOperator. What do you think @wo80 ? i can make the pull-request. Thanks

wo80 commented 4 years ago

Well, I'm not sure if Eduard wants to move away from MathNet, but here are some points to consider.

Regarding the first point: there are a couple of places where NonLinearEquationsSolver uses the MathNet vectors with operator notation, for example https://github.com/EduardBargues/NonLinearEquationsSolver/blob/69f44cb4f6ad01e0e2b4cfbbdf9b0f62efe3e15e/NLES/Correction/Corrector.cs#L60 Those would have to be replaced with something like

equilibrium = info.InitialLoad.Clone();
equilibrium.Add(newState.Lambda, info.ReferenceLoad, equilibrium); // axpy
equilibrium.Add(-1.0, reaction, equilibrium); // axpy

It's more verbose, but reducing pressure on the GC, since it involves allocation of only one array, while operator notation involves 3 allocations.

EduardBargues commented 4 years ago

@epsi1on the pull request i did removes any usage of a Matrix and uses an ILinearSolver interface. That, I think, is enough to use any implementation you want to optimize allocation (CSparse) or computation (anything different from Math-Net).

hello @wo80, i was thinking to do the same for the Vector. That way I'll be able to move away from Math.Net and allow you to use any implementation.

What do you think guys?

epsi1on commented 4 years ago

@epsi1on the pull request i did removes any usage of a Matrix and uses an ILinearSolver interface. That, I think, is enough to use any implementation you want to optimize allocation (CSparse) or computation (anything different from Math-Net).

I've forked this repo (available here) and am working on for supporting CSparse and using double array instead of Vector, once done will make it as pull request to main repo. I put several hours today to convert all Vector<double> to double[] and Matrix<double> to CSparse.Double.SparseMatrix. I have a suggestion, to improve performance it is good to use an ArrayPool for allocating new arrays in memory, As this is iterative based procedure and large arrays are allocated and destructed several times.

wo80 commented 4 years ago

@EduardBargues Removing the Math.NET dependency would be great. You could either use arrays like CSparse.NET (see Vector helper class) or implement a simple vector type which wraps a double array.

@epsi1on The ILinearSolver allows to choose whichever matrix implementation you like and the goal is to be completely independend of any third party library.

EDIT: introducing an array pool is a good idea, but might not be necessary. To eliminate the cloning of the array in the example code above, the Corrector would need to have a working array of the appropriate size (which could be initialized when the NonLinearSolver is built):

// equilibrium array/vector is a class member
info.InitialLoad.CopyTo(equilibrium);
equilibrium.Add(newState.Lambda, info.ReferenceLoad, equilibrium);
equilibrium.Add(-1.0, reaction, equilibrium);
EduardBargues commented 4 years ago

Hello @epsi1on and @wo80 . I'm going to close this issue because we already achieved the goal through the ILinearSolver interface. I'll open a new one to remove the Math.Net dependency.

Thank you!!