Commit bf7d3b5f authored by David C. Lonie's avatar David C. Lonie
Browse files

Move the BODR (periodic table) data into a private header.

Rather than install, attempt to locate, parse, etc the elements.xml
file directly, embed the data in the vtkBlueObeliskData translation
unit via the vtkBlueObeliskDataInternal header.

The header file is generated by the GenerateBlueObeliskHeader test
in Domains/Chemistry, which is built by enabling the CMake option
VTK_BUILD_BODR_DATA_GENERATOR.
parent 330714d2
Pipeline #23954 passed with stage
set(Module_SRCS set(Module_SRCS
vtkBlueObeliskData.cxx vtkBlueObeliskData.cxx
vtkBlueObeliskDataInternal.h
vtkBlueObeliskDataParser.cxx vtkBlueObeliskDataParser.cxx
vtkCMLMoleculeReader.cxx vtkCMLMoleculeReader.cxx
vtkGaussianCubeReader2.cxx vtkGaussianCubeReader2.cxx
...@@ -24,8 +25,16 @@ set_source_files_properties( ...@@ -24,8 +25,16 @@ set_source_files_properties(
set_source_files_properties( set_source_files_properties(
vtkBlueObeliskData.cxx vtkBlueObeliskData.cxx
vtkBlueObeliskDataInternal.h
vtkBlueObeliskDataParser.cxx vtkBlueObeliskDataParser.cxx
WRAP_EXCLUDE PROPERTIES
WRAP_EXCLUDE 1
)
set_source_files_properties(
vtkBlueObeliskDataInternal.h
PROPERTIES
WRAP_EXCLUDE_PYTHON 1
) )
# Generate configured header file # Generate configured header file
...@@ -34,6 +43,3 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/vtkChemistryConfigure.h.in" ...@@ -34,6 +43,3 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/vtkChemistryConfigure.h.in"
set(${vtk-module}_NO_HeaderTest 1) # TODO: Fix headers and enable test. set(${vtk-module}_NO_HeaderTest 1) # TODO: Fix headers and enable test.
vtk_module_library(${vtk-module} ${Module_SRCS}) vtk_module_library(${vtk-module} ${Module_SRCS})
install(FILES elements.xml COPYING
DESTINATION ${VTK_INSTALL_DATA_DIR}/vtkDomainsChemistry)
option(VTK_BUILD_BODR_DATA_GENERATOR
"If enabled, the test used to generate the vtkBlueObeliskDataInternal.h header will be built."
OFF
)
mark_as_advanced(VTK_BUILD_BODR_DATA_GENERATOR)
set(generator_test)
if(VTK_BUILD_BODR_DATA_GENERATOR)
set(generator_test
"GenerateBlueObeliskHeader.cxx,NO_VALID,NO_DATA,NO_OUTPUT"
)
endif()
set(TestVASPAnimationReader_ARGS DATA{../Data/VASP/NPT_Z_ANIMATE.out}) set(TestVASPAnimationReader_ARGS DATA{../Data/VASP/NPT_Z_ANIMATE.out})
set(TestVASPTessellationReader_ARGS DATA{../Data/VASP/NPT_Z_TESSELLATE.out}) set(TestVASPTessellationReader_ARGS DATA{../Data/VASP/NPT_Z_TESSELLATE.out})
vtk_add_test_cxx(${vtk-module}CxxTests tests vtk_add_test_cxx(${vtk-module}CxxTests tests
${generator_test}
TestBallAndStick.cxx TestBallAndStick.cxx
TestPDBBallAndStick.cxx TestPDBBallAndStick.cxx
TestBondColorModeDiscreteByAtom.cxx TestBondColorModeDiscreteByAtom.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkBlueObeliskData.h"
#include "vtkChemistryConfigure.h"
#include <fstream>
int GenerateBlueObeliskHeader(int, char *[])
{
std::ifstream xml(VTK_BODR_DATA_PATH_BUILD "/elements.xml");
if (!xml)
{
std::cerr << "Error opening file " VTK_BODR_DATA_PATH_BUILD
"/elements.xml.\n";
return EXIT_FAILURE;
}
std::cout << "// VTK/Domains/Chemistry/Testing/Cxx/GenerateBlueObeliskHeader.cxx\n";
if (!vtkBlueObeliskData::GenerateHeaderFromXML(xml, std::cout))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
...@@ -15,10 +15,12 @@ ...@@ -15,10 +15,12 @@
#include "vtkBlueObeliskData.h" #include "vtkBlueObeliskData.h"
#include "vtkAbstractArray.h" #include "vtkAbstractArray.h"
#include "vtkBlueObeliskDataInternal.h"
#include "vtkBlueObeliskDataParser.h" #include "vtkBlueObeliskDataParser.h"
#include "vtkFloatArray.h" #include "vtkFloatArray.h"
#include "vtkObjectFactory.h" #include "vtkObjectFactory.h"
#include "vtkStringArray.h" #include "vtkStringArray.h"
#include "vtkTypeTraits.h"
#include "vtkMutexLock.h" #include "vtkMutexLock.h"
#include "vtkUnsignedShortArray.h" #include "vtkUnsignedShortArray.h"
...@@ -170,6 +172,36 @@ void vtkBlueObeliskData::PrintSelfIfExists(const char * name, vtkObject *obj, ...@@ -170,6 +172,36 @@ void vtkBlueObeliskData::PrintSelfIfExists(const char * name, vtkObject *obj,
} }
} }
// Helpers for reading raw data from the private header into a VTK array.
namespace {
void LoadStringArray(vtkStringArray *array, const char *data[], vtkIdType size)
{
array->SetNumberOfTuples(size);
for (vtkIdType i = 0; i < size; ++i)
{
array->SetValue(i, data[i]);
}
}
template <int NumComps, typename ArrayT>
void LoadDataArray(ArrayT *array,
const typename ArrayT::ValueType data[][NumComps],
vtkIdType numTuples)
{
array->SetNumberOfTuples(numTuples);
for (vtkIdType t = 0; t < numTuples; ++t)
{
for (int c = 0; c < NumComps; ++c)
{
array->SetTypedComponent(t, c, data[t][c]);
}
}
}
} // End anon namespace
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void vtkBlueObeliskData::Initialize() void vtkBlueObeliskData::Initialize()
{ {
...@@ -180,13 +212,174 @@ void vtkBlueObeliskData::Initialize() ...@@ -180,13 +212,174 @@ void vtkBlueObeliskData::Initialize()
return; return;
} }
vtkNew<vtkBlueObeliskDataParser> parser; this->NumberOfElements = _vtkBlueObeliskData::numberOfElements;
parser->SetTarget(this); vtkIdType arraySize = this->NumberOfElements + 1; // 0 is dummy element
parser->Parse();
#define READARRAY(name) \
LoadStringArray(this->name.Get(), _vtkBlueObeliskData::name, arraySize)
READARRAY(Symbols);
READARRAY(LowerSymbols);
READARRAY(Names);
READARRAY(LowerNames);
READARRAY(PeriodicTableBlocks);
READARRAY(ElectronicConfigurations);
READARRAY(Families);
#undef READARRAY
#define READARRAY(numComps, name) \
LoadDataArray<numComps>(this->name.Get(), _vtkBlueObeliskData::name, arraySize)
READARRAY(1, Masses);
READARRAY(1, ExactMasses);
READARRAY(1, IonizationEnergies);
READARRAY(1, ElectronAffinities);
READARRAY(1, PaulingElectronegativities);
READARRAY(1, CovalentRadii);
READARRAY(1, VDWRadii);
READARRAY(3, DefaultColors);
READARRAY(1, BoilingPoints);
READARRAY(1, MeltingPoints);
READARRAY(1, Periods);
READARRAY(1, Groups);
#undef READARRAY
this->Initialized = true; this->Initialized = true;
} }
// Helpers for GenerateHeaderFromXML:
namespace {
void WriteStringArray(const std::string &name, vtkStringArray *data,
std::ostream &out)
{
out << "static const char *" << name
<< "[" << data->GetNumberOfTuples() << "] = {\n";
assert("Single component string array: " &&
data->GetNumberOfComponents() == 1);
vtkIdType numTuples = data->GetNumberOfTuples();
for (vtkIdType i = 0; i < numTuples; ++i)
{
out << " \"" << data->GetValue(i) << "\"";
if (i < numTuples - 1)
{
out << ",";
}
out << "\n";
}
out << "};\n\n";
}
template <typename T>
struct TypeTraits
{
static const char* Suffix() { return ""; }
static void PrepStream(std::ostream &out)
{
out.unsetf(std::ostream::floatfield);
}
};
template <>
struct TypeTraits<float>
{
// Float literals need the 'f' suffix:
static const char* Suffix() { return "f"; }
// Need to make sure that we have a decimal point when using 'f' suffix:
static void PrepStream(std::ostream &out) { out << std::scientific; }
};
template <typename ArrayT>
void WriteDataArray(const std::string &name, ArrayT *data, std::ostream &out)
{
typedef typename ArrayT::ValueType ValueType;
vtkIdType numTuples = data->GetNumberOfTuples();
int numComps = data->GetNumberOfComponents();
TypeTraits<ValueType>::PrepStream(out);
out << "static const " << vtkTypeTraits<ValueType>::Name()
<< " " << name << "[" << numTuples << "][" << numComps << "] = {\n";
for (vtkIdType t = 0; t < numTuples; ++t)
{
out << " { ";
for (int c = 0; c < numComps; ++c)
{
out << data->GetTypedComponent(t, c)
<< TypeTraits<ValueType>::Suffix();
if (c < numComps - 1)
{
out << ",";
}
out << " ";
}
out << "}";
if (t < numTuples - 1)
{
out << ",";
}
out << "\n";
}
out << "};\n\n";
}
} // end anon namespace
//----------------------------------------------------------------------------
bool vtkBlueObeliskData::GenerateHeaderFromXML(std::istream &xml,
std::ostream &out)
{
vtkNew<vtkBlueObeliskData> data;
vtkNew<vtkBlueObeliskDataParser> parser;
parser->SetStream(&xml);
parser->SetTarget(data.Get());
if (!parser->Parse())
{
return false;
}
out << "// Autogenerated by vtkBlueObeliskData::GenerateHeaderFromXML.\n"
"// Do not edit. Any modifications may be lost.\n"
"\n"
"namespace _vtkBlueObeliskData {\n"
"\n"
"const static unsigned int numberOfElements = "
<< data->GetNumberOfElements() << ";\n\n";
#define DUMPARRAY(type, name) \
Write ## type ## Array(#name, data->Get ## name(), out)
DUMPARRAY(String, Symbols);
DUMPARRAY(String, LowerSymbols);
DUMPARRAY(String, Names);
DUMPARRAY(String, LowerNames);
DUMPARRAY(String, PeriodicTableBlocks);
DUMPARRAY(String, ElectronicConfigurations);
DUMPARRAY(String, Families);
DUMPARRAY(Data, Masses);
DUMPARRAY(Data, ExactMasses);
DUMPARRAY(Data, IonizationEnergies);
DUMPARRAY(Data, ElectronAffinities);
DUMPARRAY(Data, PaulingElectronegativities);
DUMPARRAY(Data, CovalentRadii);
DUMPARRAY(Data, VDWRadii);
DUMPARRAY(Data, DefaultColors);
DUMPARRAY(Data, BoilingPoints);
DUMPARRAY(Data, MeltingPoints);
DUMPARRAY(Data, Periods);
DUMPARRAY(Data, Groups);
#undef DUMPARRAY
out << "} // end namespace _vtkBlueObeliskData\n";
return true;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int vtkBlueObeliskData::Allocate(vtkIdType sz, vtkIdType ext) int vtkBlueObeliskData::Allocate(vtkIdType sz, vtkIdType ext)
{ {
......
...@@ -98,6 +98,11 @@ class VTKDOMAINSCHEMISTRY_EXPORT vtkBlueObeliskData : public vtkObject ...@@ -98,6 +98,11 @@ class VTKDOMAINSCHEMISTRY_EXPORT vtkBlueObeliskData : public vtkObject
vtkGetNewMacro(Periods, vtkUnsignedShortArray); vtkGetNewMacro(Periods, vtkUnsignedShortArray);
vtkGetNewMacro(Groups, vtkUnsignedShortArray); vtkGetNewMacro(Groups, vtkUnsignedShortArray);
// Description:
// Static method to generate the data header file used by this class from the
// BODR elements.xml. See the GenerateBlueObeliskHeader test in this module.
static bool GenerateHeaderFromXML(std::istream &xml, std::ostream &header);
protected: protected:
friend class vtkBlueObeliskDataParser; friend class vtkBlueObeliskDataParser;
......
This diff is collapsed.
...@@ -51,22 +51,6 @@ vtkBlueObeliskDataParser::vtkBlueObeliskDataParser() ...@@ -51,22 +51,6 @@ vtkBlueObeliskDataParser::vtkBlueObeliskDataParser()
CurrentElectronicConfiguration(new vtkStdString), CurrentElectronicConfiguration(new vtkStdString),
CurrentFamily(new vtkStdString) CurrentFamily(new vtkStdString)
{ {
// Find elements.xml. Check the share directory first, then the source dir
// if running from the build directory before installing.
struct stat buf;
if (stat(VTK_BODR_DATA_PATH "/elements.xml", &buf) == 0)
{
this->SetFileName(VTK_BODR_DATA_PATH "/elements.xml");
}
else if (stat(VTK_BODR_DATA_PATH_BUILD "/elements.xml", &buf) == 0)
{
this->SetFileName(VTK_BODR_DATA_PATH_BUILD "/elements.xml");
}
else
{
vtkErrorMacro(<<"Cannot find elements.xml. Checked " VTK_BODR_DATA_PATH
" and " VTK_BODR_DATA_PATH_BUILD)
}
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
......
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
#ifndef vtkDomainsChemistryConfigure_h #ifndef vtkDomainsChemistryConfigure_h
#define vtkDomainsChemistryConfigure_h #define vtkDomainsChemistryConfigure_h
/* Where the Blue Obelisk Data Repository files are installed */ /* Where the Blue Obelisk Data Repository element.xml file is found. */
#define VTK_BODR_DATA_PATH "@CMAKE_INSTALL_PREFIX@/@VTK_INSTALL_DATA_DIR@/vtkDomainsChemistry"
#define VTK_BODR_DATA_PATH_BUILD "@VTK_SOURCE_DIR@/Domains/Chemistry" #define VTK_BODR_DATA_PATH_BUILD "@VTK_SOURCE_DIR@/Domains/Chemistry"
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment