0

I am using Sundials numerical solver to solve DAE equations. I was able to create a working code but I am not sure what is the best way to save solution vector efficiently. Below is the code snippet for solving equations and saving the solution in a text file using for loop.

  FILE* file;
  int i;
  tout = dt; 
  file = fopen("solution.txt", "w");
  /*
  for (iout = 1; iout < NOUT; iout++) {
      tout = iout * dt;   // dt : Time step
      retval = IDASolve(mem, tout, &tret, uu, up, IDA_NORMAL);  // Solve equation
      if (retval < 0) break;

       /* Save the solution vector in a text file*/
       // MGRID : length of vector, uu
      for (i = 0; i < MGRID; i++) {  
          fprintf(file, "%lf\t", NV_Ith_S(uu, i));  // access vector uu element using macro NV_Ith_S
      }
      fprintf(file, "\n");      
  }
  fclose(file);

Is there any other way to save a vector uu in a text file (or other efficient format) without inner 'for' loop ?

Derik
  • 57
  • 2
  • 7
  • Where does `MGRID` come from? – vmt Dec 12 '20 at 01:54
  • `MGRID` is an integer which is the vector `uu` length. – Derik Dec 12 '20 at 02:07
  • I gathered as much, however *where does it come from*? – vmt Dec 12 '20 at 02:09
  • It is defined like `#define MGRID 120 ` – Derik Dec 12 '20 at 02:28
  • Unrolling that by hand seems a tad excessive. I can't think of a very good reason for doing so. The compiler can probably unroll it for you with optimizations turned on. However, to answer your question, if the underlying buffer for the vector data is contiguous, then the most efficient method for storing it, is to just write the buffer as-is in one shot to disk. However, obviously it will not be in human-readable format. – vmt Dec 12 '20 at 02:30

0 Answers0