I want to write a file which contains values for each cell of a CFD computation. This will be an input file to the program which runs the computation.
Currently, the most dubious section of my code looks like:
for (int y = 0; y < ycells; y++)
{
double yPos = yMin + (cellToY * (y + 0.5));
for (int x=0; x<xcells; x++)
{
double deltaY = yProfileHeight[x] - yPos;
if (deltaY < -cellToY)
{
file << "1\n";
}
else if (deltaY > cellToY)
{
file << "0\n";
}
else
{
double value = (0.5 + (-0.5 * (deltaY / cellToY)));
file << value << "\n";
}
}
}
}
For most of the file these if
checks are going to be slow/useless - since depending on the value of yPos
, they will all evaluate to true/false for large regions.
I can rewrite it like this (with appropriate min/max functions):
for (int y = 0; y < ycells; y++)
{
double yPos = yMin + (cellToY * (y + 0.5));
if (yPos < yHeightMin)
{
for (int x=0; x<xcells; x++)
{
file << "0\n";
}
}
else if (yPos > yHeightMax)
{
for (int x=0; x<xcells; x++)
{
file << "1\n";
}
}
else
{
for (int x=0; x<xcells; x++)
{
double deltaY = yProfileHeight[x] - yPos;
if (deltaY < -cellToY)
{
file << "1\n";
}
else if (deltaY > cellToY)
{
file << "0\n";
}
else
{
double value = (0.5 + (-0.5 * (deltaY / cellToY)));
file << value << "\n";
}
}
}
}
Now my if
conditions are evaluated less frequently, but I'm still left with segments like
for (int x=0; x<xcells; x++) // where xcells is ~ 1-10k
{
file << "1\n";
}
Can I rewrite this in a way that doesn't require several thousand separate (identical) writes to a single file?
double value = (0.5 + (-0.5 * (deltaY / cellToY)));
? 0 or 1 also? – Surt Nov 13 '14 at 16:30if
condition is true. – chrisb2244 Nov 14 '14 at 2:37