diff --git a/PuReMD/src/linear_solvers.c b/PuReMD/src/linear_solvers.c
index 39d2d42757429e4c0f7529d6e2be2e3e79899d2e..d1ad369b7a6bdf447c43d9a7996a6539014d6c0a 100644
--- a/PuReMD/src/linear_solvers.c
+++ b/PuReMD/src/linear_solvers.c
@@ -1235,7 +1235,12 @@ real sparse_approx_inverse( reax_system *system, simulation_data *data,
 
     X = smalloc( sizeof(int) * (system->bigN + 1),
             "sparse_approx_inverse::X", MPI_COMM_WORLD );
-    q = smalloc( sizeof(int) * system->N * 2,
+    //size of q should be equal to the maximum possible cardinalty 
+    //of the set formed by neighbors of neighbors of an atom
+    //i.e, maximum number of rows of dense matrix
+    //for water systems, this number is 34000
+    //for silica systems, it is 12000
+    q = smalloc( sizeof(int) * 50000,
             "sparse_approx_inverse::q", MPI_COMM_WORLD );
 
     for ( i = 0; i <= system->bigN; ++i )