Files
LBPM/IO/Reader.cpp

421 lines
17 KiB
C++
Raw Normal View History

#include "IO/Reader.h"
2021-03-17 10:24:14 -04:00
#include "IO/IOHelpers.h"
2014-11-04 21:58:20 -05:00
#include "IO/Mesh.h"
#include "IO/MeshDatabase.h"
#include "common/Utilities.h"
2017-01-31 08:22:29 -05:00
#ifdef USE_SILO
#include "IO/silo.h"
#endif
#include <ProfilerApp.h>
2021-03-17 10:24:14 -04:00
#include <cstdio>
#include <fstream>
#include <iostream>
2021-03-17 10:24:14 -04:00
#include <map>
#include <string.h>
#include <vector>
2014-11-04 21:58:20 -05:00
2015-02-13 11:43:41 -05:00
// Inline function to read line without a return argument
2021-03-17 10:24:14 -04:00
static inline void fgetl( char *str, int num, FILE *stream )
2015-02-13 11:43:41 -05:00
{
2021-03-17 10:24:14 -04:00
char *ptr = fgets( str, num, stream );
if ( 0 ) {
char *temp = (char *) &ptr;
temp++;
}
}
// Check if the file exists
bool fileExists( const std::string &filename )
{
std::ifstream ifile( filename.c_str() );
return ifile.good();
2015-02-13 11:43:41 -05:00
}
// Get the path to a file
2021-03-17 10:24:14 -04:00
std::string IO::getPath( const std::string &filename )
{
2021-03-17 10:24:14 -04:00
std::string file( filename );
size_t k1 = file.rfind( 47 );
size_t k2 = file.rfind( 92 );
if ( k1 == std::string::npos ) {
k1 = 0;
}
if ( k2 == std::string::npos ) {
k2 = 0;
}
return file.substr( 0, std::max( k1, k2 ) );
}
2021-03-16 17:21:52 -04:00
// List the timesteps in the given directory (dumps.LBPM)
2021-03-17 10:24:14 -04:00
std::vector<std::string> IO::readTimesteps( const std::string &path, const std::string &format )
{
2021-03-16 17:21:52 -04:00
// Get the name of the summary filename
std::string filename = path + "/";
2021-03-17 10:24:14 -04:00
if ( format == "old" || format == "new" ) {
2021-03-16 17:21:52 -04:00
filename += "summary.LBM";
2021-03-17 10:24:14 -04:00
} else if ( format == "silo" ) {
2021-03-16 17:21:52 -04:00
filename += "LBM.visit";
2021-03-17 10:24:14 -04:00
} else if ( format == "auto" ) {
bool test_old = fileExists( path + "/summary.LBM" );
bool test_silo = fileExists( path + "/LBM.visit" );
if ( test_old && test_silo ) {
ERROR( "Unable to determine format (both summary.LBM and LBM.visit exist)" );
} else if ( test_old ) {
filename += "summary.LBM";
} else if ( test_silo ) {
filename += "LBM.visit";
} else {
ERROR( "Unable to determine format (neither summary.LBM or LBM.visit exist)" );
}
} else {
2021-03-16 17:21:52 -04:00
ERROR( "Unknown format: " + format );
2021-03-17 10:24:14 -04:00
}
PROFILE_START( "readTimesteps" );
2021-03-16 17:21:52 -04:00
// Read the data
2021-03-17 10:24:14 -04:00
FILE *fid = fopen( filename.c_str(), "rb" );
if ( fid == NULL )
ERROR( "Error opening file" );
std::vector<std::string> timesteps;
char buf[1000];
2021-03-17 10:24:14 -04:00
while ( fgets( buf, sizeof( buf ), fid ) != NULL ) {
std::string line( buf );
line.resize( line.size() - 1 );
2017-01-31 08:22:29 -05:00
auto pos = line.find( "summary.silo" );
if ( pos != std::string::npos )
2021-03-17 10:24:14 -04:00
line.resize( pos );
if ( line.empty() )
continue;
2021-03-17 10:24:14 -04:00
timesteps.push_back( line );
}
2021-03-17 10:24:14 -04:00
fclose( fid );
PROFILE_STOP( "readTimesteps" );
return timesteps;
2021-03-16 17:21:52 -04:00
return timesteps;
}
2021-03-17 10:24:14 -04:00
// Get the maximum number of domains
int IO::maxDomains( const std::string &path, const std::string &format, const Utilities::MPI &comm )
{
int rank = comm.getRank();
int n_domains = 0;
if ( rank == 0 ) {
// Get the timesteps
auto timesteps = IO::readTimesteps( path, format );
ASSERT( !timesteps.empty() );
// Get the database for the first domain
auto db = IO::getMeshList( path, timesteps[0] );
for ( size_t i = 0; i < db.size(); i++ )
n_domains = std::max<int>( n_domains, db[i].domains.size() );
}
return comm.bcast( n_domains, 0 );
}
2021-03-16 17:21:52 -04:00
// Read the data for the given timestep
2021-03-17 10:24:14 -04:00
std::vector<IO::MeshDataStruct> IO::readData(
const std::string &path, const std::string &timestep, int rank )
2021-03-16 17:21:52 -04:00
{
// Get the mesh databases
auto db = IO::getMeshList( path, timestep );
// Create the data
2021-03-17 10:24:14 -04:00
std::vector<IO::MeshDataStruct> data( db.size() );
for ( size_t i = 0; i < data.size(); i++ ) {
2021-03-16 17:21:52 -04:00
data[i].precision = IO::DataType::Double;
2021-03-17 10:24:14 -04:00
data[i].meshName = db[i].name;
data[i].mesh = getMesh( path, timestep, db[i], rank );
2021-03-16 17:21:52 -04:00
data[i].vars.resize( db[i].variables.size() );
2021-03-17 10:24:14 -04:00
for ( size_t j = 0; j < db[i].variables.size(); j++ )
data[i].vars[j] = getVariable( path, timestep, db[i], rank, db[i].variables[j].name );
INSIST( data[i].check(), "Failed check of " + data[i].meshName );
2021-03-16 17:21:52 -04:00
}
return data;
}
// Read the list of variables for the given timestep
2021-03-17 10:24:14 -04:00
std::vector<IO::MeshDatabase> IO::getMeshList(
const std::string &path, const std::string &timestep )
{
std::string filename = path + "/" + timestep + "/LBM.summary";
return IO::read( filename );
}
// Read the given mesh domain
2021-03-17 10:24:14 -04:00
std::shared_ptr<IO::Mesh> IO::getMesh( const std::string &path, const std::string &timestep,
const IO::MeshDatabase &meshDatabase, int domain )
{
2021-03-17 10:24:14 -04:00
PROFILE_START( "getMesh" );
std::shared_ptr<IO::Mesh> mesh;
if ( meshDatabase.format == FileFormat::OLD ) {
// Old format (binary doubles)
2014-11-04 21:58:20 -05:00
std::string filename = path + "/" + timestep + "/" + meshDatabase.domains[domain].file;
2021-03-17 10:24:14 -04:00
FILE *fid = fopen( filename.c_str(), "rb" );
INSIST( fid != NULL, "Error opening file" );
fseek( fid, 0, SEEK_END );
2021-03-17 10:24:14 -04:00
size_t bytes = ftell( fid );
size_t N_max = bytes / sizeof( double ) + 1000;
double *data = new double[N_max];
2021-03-17 10:24:14 -04:00
fseek( fid, 0, SEEK_SET );
size_t count = fread( data, sizeof( double ), N_max, fid );
fclose( fid );
if ( count % 3 != 0 )
ERROR( "Error reading file" );
if ( meshDatabase.type == IO::MeshType::PointMesh ) {
size_t N = count / 3;
std::shared_ptr<PointList> pointlist( new PointList( N ) );
std::vector<Point> &P = pointlist->points;
for ( size_t i = 0; i < N; i++ ) {
P[i].x = data[3 * i + 0];
P[i].y = data[3 * i + 1];
P[i].z = data[3 * i + 2];
}
mesh = pointlist;
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.type == IO::MeshType::SurfaceMesh ) {
if ( count % 9 != 0 )
ERROR( "Error reading file (2)" );
size_t N_tri = count / 9;
std::shared_ptr<TriList> trilist( new TriList( N_tri ) );
std::vector<Point> &A = trilist->A;
std::vector<Point> &B = trilist->B;
std::vector<Point> &C = trilist->C;
for ( size_t i = 0; i < N_tri; i++ ) {
A[i].x = data[9 * i + 0];
A[i].y = data[9 * i + 1];
A[i].z = data[9 * i + 2];
B[i].x = data[9 * i + 3];
B[i].y = data[9 * i + 4];
B[i].z = data[9 * i + 5];
C[i].x = data[9 * i + 6];
C[i].y = data[9 * i + 7];
C[i].z = data[9 * i + 8];
}
mesh = trilist;
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.type == IO::MeshType::VolumeMesh ) {
// this was never supported in the old format
mesh = std::shared_ptr<DomainMesh>( new DomainMesh() );
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown mesh type" );
}
2021-03-17 10:24:14 -04:00
delete[] data;
} else if ( meshDatabase.format == FileFormat::NEW ||
meshDatabase.format == FileFormat::NEW_SINGLE ) {
2021-03-17 10:24:14 -04:00
const DatabaseEntry &database = meshDatabase.domains[domain];
std::string filename = path + "/" + timestep + "/" + database.file;
FILE *fid = fopen( filename.c_str(), "rb" );
fseek( fid, database.offset, SEEK_SET );
2014-11-04 21:58:20 -05:00
char line[1000];
2021-03-17 10:24:14 -04:00
fgetl( line, 1000, fid );
size_t i1 = find( line, ':' );
size_t i2 = find( &line[i1 + 1], ':' ) + i1 + 1;
size_t bytes = atol( &line[i2 + 1] );
char *data = new char[bytes];
size_t count = fread( data, 1, bytes, fid );
fclose( fid );
ASSERT( count == bytes );
if ( meshDatabase.meshClass == "PointList" ) {
2014-11-04 21:58:20 -05:00
mesh.reset( new IO::PointList() );
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "TriMesh" ) {
2014-11-04 21:58:20 -05:00
mesh.reset( new IO::TriMesh() );
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "TriList" ) {
2014-11-04 21:58:20 -05:00
mesh.reset( new IO::TriList() );
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "DomainMesh" ) {
mesh.reset( new IO::DomainMesh() );
2014-11-04 21:58:20 -05:00
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown mesh class" );
2014-11-04 21:58:20 -05:00
}
2021-03-17 10:24:14 -04:00
mesh->unpack( std::pair<size_t, void *>( bytes, data ) );
delete[] data;
} else if ( meshDatabase.format == FileFormat::SILO ) {
2017-01-31 08:22:29 -05:00
// Reading a silo file
#ifdef USE_SILO
2021-03-17 10:24:14 -04:00
const DatabaseEntry &database = meshDatabase.domains[domain];
std::string filename = path + "/" + timestep + "/" + database.file;
auto fid = silo::open( filename, silo::READ );
if ( meshDatabase.meshClass == "PointList" ) {
Array<double> coords = silo::readPointMesh<double>( fid, database.name );
2021-03-17 10:24:14 -04:00
ASSERT( coords.size( 1 ) == 3 );
std::shared_ptr<IO::PointList> mesh2( new IO::PointList( coords.size( 0 ) ) );
for ( size_t i = 0; i < coords.size( 1 ); i++ ) {
mesh2->points[i].x = coords( i, 0 );
mesh2->points[i].y = coords( i, 1 );
mesh2->points[i].z = coords( i, 2 );
2017-02-03 11:26:08 -05:00
}
mesh = mesh2;
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "TriMesh" || meshDatabase.meshClass == "TriList" ) {
2017-02-03 11:26:08 -05:00
Array<double> coords;
Array<int> tri;
silo::readTriMesh( fid, database.name, coords, tri );
2021-03-17 10:24:14 -04:00
ASSERT( tri.size( 1 ) == 3 && coords.size( 1 ) == 3 );
int N_tri = tri.size( 0 );
int N_point = coords.size( 0 );
2017-02-03 11:26:08 -05:00
std::shared_ptr<IO::TriMesh> mesh2( new IO::TriMesh( N_tri, N_point ) );
2021-03-17 10:24:14 -04:00
for ( int i = 0; i < N_point; i++ ) {
mesh2->vertices->points[i].x = coords( i, 0 );
mesh2->vertices->points[i].y = coords( i, 1 );
mesh2->vertices->points[i].z = coords( i, 2 );
2017-02-03 11:26:08 -05:00
}
2021-03-17 10:24:14 -04:00
for ( int i = 0; i < N_tri; i++ ) {
mesh2->A[i] = tri( i, 0 );
mesh2->B[i] = tri( i, 1 );
mesh2->C[i] = tri( i, 2 );
2017-02-03 11:26:08 -05:00
}
2021-03-17 10:24:14 -04:00
if ( meshDatabase.meshClass == "TriMesh" ) {
2017-02-03 11:26:08 -05:00
mesh = mesh2;
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "TriList" ) {
2017-02-03 11:26:08 -05:00
auto trilist = IO::getTriList( std::dynamic_pointer_cast<IO::Mesh>( mesh2 ) );
2021-03-17 10:24:14 -04:00
mesh = trilist;
2017-02-03 11:26:08 -05:00
}
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "DomainMesh" ) {
2017-01-31 08:22:29 -05:00
std::vector<double> range;
std::vector<int> N;
silo::readUniformMesh( fid, database.name, range, N );
2021-03-17 10:24:14 -04:00
auto rankinfo = silo::read<int>( fid, database.name + "_rankinfo" );
2017-01-31 08:22:29 -05:00
RankInfoStruct rank_data( rankinfo[0], rankinfo[1], rankinfo[2], rankinfo[3] );
2021-03-17 10:24:14 -04:00
mesh.reset( new IO::DomainMesh( rank_data, N[0], N[1], N[2], range[1] - range[0],
range[3] - range[2], range[5] - range[4] ) );
2017-01-31 08:22:29 -05:00
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown mesh class" );
2017-01-31 08:22:29 -05:00
}
silo::close( fid );
#else
2021-03-17 10:24:14 -04:00
ERROR( "Build without silo support" );
2017-01-31 08:22:29 -05:00
#endif
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown format" );
}
2021-03-17 10:24:14 -04:00
PROFILE_STOP( "getMesh" );
return mesh;
}
2014-11-04 21:58:20 -05:00
// Read the given variable for the given mesh domain
2021-03-17 10:24:14 -04:00
std::shared_ptr<IO::Variable> IO::getVariable( const std::string &path, const std::string &timestep,
const MeshDatabase &meshDatabase, int domain, const std::string &variable )
2014-11-04 21:58:20 -05:00
{
2021-03-17 10:24:14 -04:00
std::pair<std::string, std::string> key( meshDatabase.domains[domain].name, variable );
auto it = meshDatabase.variable_data.find( key );
2021-03-17 10:24:14 -04:00
if ( it == meshDatabase.variable_data.end() )
return std::shared_ptr<IO::Variable>();
2017-02-03 11:26:08 -05:00
std::shared_ptr<IO::Variable> var;
if ( meshDatabase.format == FileFormat::NEW || meshDatabase.format == FileFormat::NEW_SINGLE ) {
2021-03-17 10:24:14 -04:00
const DatabaseEntry &database = it->second;
std::string filename = path + "/" + timestep + "/" + database.file;
FILE *fid = fopen( filename.c_str(), "rb" );
fseek( fid, database.offset, SEEK_SET );
2017-02-03 11:26:08 -05:00
char line[1000];
2021-03-17 10:24:14 -04:00
fgetl( line, 1000, fid );
size_t i1 = find( line, ':' );
size_t i2 = find( &line[i1 + 1], ':' ) + i1 + 1;
std::vector<std::string> values = splitList( &line[i2 + 1], ',' );
ASSERT( values.size() == 5 );
int dim = atoi( values[0].c_str() );
auto type = values[1];
2021-03-17 10:24:14 -04:00
size_t N = atol( values[2].c_str() );
size_t bytes = atol( values[3].c_str() );
std::string precision = values[4];
var = std::shared_ptr<IO::Variable>( new IO::Variable() );
var->dim = dim;
var->type = getVariableType( type );
2021-03-17 10:24:14 -04:00
var->name = variable;
var->data.resize( N, dim );
if ( precision == "double" ) {
size_t count = fread( var->data.data(), sizeof( double ), N * dim, fid );
ASSERT( count * sizeof( double ) == bytes );
2017-02-03 11:26:08 -05:00
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Format not implimented" );
2017-02-03 11:26:08 -05:00
}
2021-03-17 10:24:14 -04:00
fclose( fid );
} else if ( meshDatabase.format == FileFormat::SILO ) {
2017-02-03 11:26:08 -05:00
// Reading a silo file
#ifdef USE_SILO
2021-03-17 10:24:14 -04:00
const auto &database = meshDatabase.domains[domain];
2017-02-03 11:26:08 -05:00
auto variableDatabase = meshDatabase.getVariableDatabase( variable );
2021-03-17 10:24:14 -04:00
std::string filename = path + "/" + timestep + "/" + database.file;
auto fid = silo::open( filename, silo::READ );
2017-02-03 11:26:08 -05:00
var.reset( new Variable( variableDatabase.dim, variableDatabase.type, variable ) );
2021-03-17 10:24:14 -04:00
if ( meshDatabase.meshClass == "PointList" ) {
var->data = silo::readPointMeshVariable<double>( fid, variable );
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "TriMesh" || meshDatabase.meshClass == "TriList" ) {
var->data = silo::readTriMeshVariable<double>( fid, variable );
2021-03-17 10:24:14 -04:00
} else if ( meshDatabase.meshClass == "DomainMesh" ) {
var->data = silo::readUniformMeshVariable<double>( fid, variable );
2017-02-03 11:26:08 -05:00
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown mesh class" );
2017-02-03 11:26:08 -05:00
}
silo::close( fid );
#else
2021-03-17 10:24:14 -04:00
ERROR( "Build without silo support" );
2017-02-03 11:26:08 -05:00
#endif
2014-11-05 16:54:18 -05:00
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown format" );
2014-11-05 16:54:18 -05:00
}
return var;
2014-11-04 21:58:20 -05:00
}
/****************************************************
2021-03-17 10:24:14 -04:00
* Reformat the variable to match the mesh *
****************************************************/
void IO::reformatVariable( const IO::Mesh &mesh, IO::Variable &var )
{
if ( mesh.className() == "DomainMesh" ) {
2021-03-17 10:24:14 -04:00
const IO::DomainMesh &mesh2 = dynamic_cast<const IO::DomainMesh &>( mesh );
if ( var.type == VariableType::NodeVariable ) {
2021-03-17 10:24:14 -04:00
size_t N2 =
var.data.length() / ( ( mesh2.nx + 1 ) * ( mesh2.ny + 1 ) * ( mesh2.nz + 1 ) );
ASSERT(
( mesh2.nx + 1 ) * ( mesh2.ny + 1 ) * ( mesh2.nz + 1 ) * N2 == var.data.length() );
var.data.reshape(
{ (size_t) mesh2.nx + 1, (size_t) mesh2.ny + 1, (size_t) mesh2.nz + 1, N2 } );
} else if ( var.type == VariableType::EdgeVariable ) {
2021-03-17 10:24:14 -04:00
ERROR( "Not finished" );
} else if ( var.type == VariableType::SurfaceVariable ) {
2021-03-17 10:24:14 -04:00
ERROR( "Not finished" );
} else if ( var.type == VariableType::VolumeVariable ) {
2021-03-17 10:24:14 -04:00
size_t N2 = var.data.length() / ( mesh2.nx * mesh2.ny * mesh2.nz );
ASSERT( mesh2.nx * mesh2.ny * mesh2.nz * N2 == var.data.length() );
var.data.reshape( { (size_t) mesh2.nx, (size_t) mesh2.ny, (size_t) mesh2.nz, N2 } );
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Invalid variable type" );
}
} else if ( mesh.className() == "PointList" ) {
2021-03-17 10:24:14 -04:00
const IO::PointList &mesh2 = dynamic_cast<const IO::PointList &>( mesh );
size_t N = mesh2.points.size();
size_t N_var = var.data.length() / N;
ASSERT( N * N_var == var.data.length() );
2017-02-03 11:26:08 -05:00
var.data.reshape( { N, N_var } );
2021-03-17 10:24:14 -04:00
} else if ( mesh.className() == "TriMesh" || mesh.className() == "TriList" ) {
std::shared_ptr<Mesh> mesh_ptr( const_cast<Mesh *>( &mesh ), []( void * ) {} );
2017-02-03 11:26:08 -05:00
std::shared_ptr<TriMesh> mesh2 = getTriMesh( mesh_ptr );
if ( var.type == VariableType::NodeVariable ) {
2021-03-17 10:24:14 -04:00
size_t N = mesh2->vertices->points.size();
size_t N_var = var.data.length() / N;
ASSERT( N * N_var == var.data.length() );
2017-02-03 11:26:08 -05:00
var.data.reshape( { N, N_var } );
} else if ( var.type == VariableType::EdgeVariable ) {
2021-03-17 10:24:14 -04:00
ERROR( "Not finished" );
} else if ( var.type == VariableType::SurfaceVariable ) {
2021-03-17 10:24:14 -04:00
ERROR( "Not finished" );
} else if ( var.type == VariableType::VolumeVariable ) {
2021-03-17 10:24:14 -04:00
size_t N = mesh2->A.size();
size_t N_var = var.data.length() / N;
ASSERT( N * N_var == var.data.length() );
2017-02-03 11:26:08 -05:00
var.data.reshape( { N, N_var } );
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Invalid variable type" );
2017-02-03 11:26:08 -05:00
}
} else {
2021-03-17 10:24:14 -04:00
ERROR( "Unknown mesh type" );
}
}