Write a text file to save vectors and other variables in C program

62 Views Asked by At

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 ?

0

There are 0 best solutions below