The top line the P3D file shows how many x, y, and z nodes there are in the lattice. Then, all of the X values are listed, all of the Y values are listed, and all of the Z values are listed. The order for the points is X increasing while Y and Z stays the same. At the end of the max X, increase to the next Y value continue until all X values are listed for all Y values at the lowest Z. Then, increase to the next Z and repeat the listing of X values at each Y. After all X values are listed, list all Y values. This is the Y value associated with the X value at the same location. Then, all Z values are listed.
The Q file contains the same header row and then four cells of zeros, the component information for each node (in the same order as the XYZ data), and then another set of zeros. To determine how many zeros to include at the end of the file, multiple the grid size by 4.
Attached is an example of a 2x3x4 regular lattice (P3D and Q files).
Updated July 17, 2018