# Basic functions for producing output files

When Montjoie is included, there are some standalone functions that can be used either to read or write output files, with the following format :

• Matlab (.dat) : format that can be read with function loadND.m, this format is also readable in Python
• Vtk (.vtk) : format that can be read by Paraview
• Medit (.bb) : format that can be read by Medit

A basic example for using these functions is proposed below

```// definition of the display grid
GridInterpolationFull<Dimension2>l; grid;
// for instance : 2-D regular grid
Real_wp xmin = -2, xmax = 2, ymin = -2, ymax = 2;
int nbx = 100, nby = 100;
grid.SetPlaneOutput(xmin, xmax, ymin, ymax, nbx, nby);

// if you are unsure about node orderings
// you can compute position of the points of the grid
VectReal_wp teta; VectR2 points2d;
grid.GenerateGridPoints(points2d, teta);

// and use these position to express a function
VectReal_wp val(points2d.GetM()), val2;
for (int i = 0; i < points2d.GetM(); i++)
{
Real_wp x =  points2d(i)(0), y = points2d(i)(1);
val(i) = sin(x)*cos(y);
}

// then you can produce a Matlab file with these datas
WriteMatlab(val, grid, "output.dat");

// you can also read datas
// val should be equal to val2 after calling this function

// same stuff for vtk format :
val2.Clear();
WriteVtk(val, grid, "output.vtk");

// For vtk outputs, it is also possible to produce outputs on unstructed grid, for example with a mesh :
Mesh<Dimension2> mesh;

val.Reallocate(mesh.GetNbVertices());
for (int i = 0; i < val.GetM(); i++)
{
Real_wp x = mesh.Vertex(i)(0), y = mesh.Vertex(i)(1);
val(i) = sin(x)*cos(y);
}

mesh.Write("output.vtk", false, false);
// then you append to the file the datas
ofstream file_out("output.vtk", ios::app);
WriteVtk(val, "u", file_out, false, false);
file_out.close();
```

An implementation of classes for writing seismogramms with buffering (to prevent from writing on the disk at each iteration) is also proposed by the classes WriteOnTheGoWithBuffer and WriteOnTheGoWithTinyBuffer.

```WriteOnTheGoWithBuffer<Real_wp> buffer;
// the second argument is the number of vectors stored in the buffer
buffer.Init("sismo.dat". 100);

VectReal_wp x(N);
for (int nt = 0; nt < nb_iterations; nt++)
{
// evolution of x
// ...

// each time you want to print something on the file, you call AddVect