Skip to content

Commit

Permalink
Fix duplicate entries in isosurface generation dialog, fix fragment l…
Browse files Browse the repository at this point in the history
…abelling
  • Loading branch information
peterspackman committed Jan 20, 2025
1 parent 6a26513 commit b289773
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 263 deletions.
4 changes: 2 additions & 2 deletions resources/surface_description.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"orbital": {
"cmap": "RedWhiteBlue",
"displayName": "Orbital",
"occName": "mo",
"occName": "orbital",
"needsWavefunction": true,
"needsOrbital": true,
"units": "au",
Expand Down Expand Up @@ -249,7 +249,7 @@
},
"orbital": {
"displayName": "Orbital",
"occName": "mo",
"occName": "orbital",
"needsWavefunction": true,
"needsOrbital": true,
"needsIsovalue": true,
Expand Down
44 changes: 22 additions & 22 deletions src/core/chemicalstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,38 +1296,38 @@ QString ChemicalStructure::getFragmentLabelForAtoms(
QString ChemicalStructure::getFragmentLabel(const FragmentIndex &index) {
const auto &fragments = symmetryUniqueFragments();
if (m_fragmentLabels.size() != fragments.size()) {
ankerl::unordered_dense::map<QString, std::pair<QString, int>>
formulaToIndex;
char currentLabel = 'A';
int labelCount = 0;
struct LabelInfo {
int formula_id;
QString current_letter;
};
ankerl::unordered_dense::map<QString, LabelInfo> formulaToLabel;
int next_id = 1;

for (const auto &[fragmentIndex, fragment] : fragments) {
QString formula = formulaSumForAtoms(fragment.atomIndices, false);
auto it = formulaToIndex.find(formula);

if (it == formulaToIndex.end()) {
QString label = QString(1, currentLabel);
formulaToIndex[formula] = {label, 1};
m_fragmentLabels[fragmentIndex] = "1" + label;

labelCount++;
if (labelCount == 26) {
currentLabel = 'A';
labelCount = 0;
} else {
currentLabel++;
}
auto it = formulaToLabel.find(formula);

if (it == formulaToLabel.end()) {
formulaToLabel[formula] = {next_id++, "A"};
m_fragmentLabels[fragmentIndex] =
QString::number(formulaToLabel[formula].formula_id) +
formulaToLabel[formula].current_letter;
} else {
auto &[label, count] = it->second;
count++;
m_fragmentLabels[fragmentIndex] = QString::number(count) + label;
auto &labelInfo = it->second;
char nextChar =
static_cast<char>(labelInfo.current_letter[0].toLatin1() + 1);
labelInfo.current_letter = QString(QChar(nextChar));

m_fragmentLabels[fragmentIndex] =
QString::number(labelInfo.formula_id) + labelInfo.current_letter;
}
}
}

const auto kv = m_fragmentLabels.find(index);
if (kv != m_fragmentLabels.end())
if (kv != m_fragmentLabels.end()) {
return kv->second;
}
return "??";
}

Expand Down
21 changes: 10 additions & 11 deletions src/core/globalconfiguration.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "globalconfiguration.h"

// Initialize static membes
GlobalConfiguration *GlobalConfiguration::instance = nullptr;
QMutex GlobalConfiguration::mutex;

Expand All @@ -13,6 +12,9 @@ GlobalConfiguration *GlobalConfiguration::getInstance() {
}

bool GlobalConfiguration::load() {
if (m_haveData)
return m_haveData;
qDebug() << "Loading surface descriptions";
bool surfaceSuccess = isosurface::loadSurfaceDescriptionConfiguration(
surfacePropertyDescriptions, surfaceDescriptions,
surfaceResolutionLevels);
Expand All @@ -24,15 +26,16 @@ bool GlobalConfiguration::load() {
if (!colorMapSuccess) {
qWarning() << "Unable to load surface descriptions from file";
}
return surfaceSuccess && colorMapSuccess;
m_haveData = surfaceSuccess && colorMapSuccess;
return m_haveData;
}

const QMap<QString, isosurface::SurfacePropertyDescription> &
const isosurface::SurfacePropertyDescriptions &
GlobalConfiguration::getPropertyDescriptions() const {
return surfacePropertyDescriptions;
}

const QMap<QString, isosurface::SurfaceDescription> &
const isosurface::SurfaceDescriptions &
GlobalConfiguration::getSurfaceDescriptions() const {
return surfaceDescriptions;
}
Expand All @@ -42,16 +45,12 @@ GlobalConfiguration::getSurfaceResolutionLevels() const {
return surfaceResolutionLevels;
}


const QMap<QString, ColorMapDescription> GlobalConfiguration::getColorMapDescriptions() const {
const QMap<QString, ColorMapDescription>
GlobalConfiguration::getColorMapDescriptions() const {
return colorMapDescriptions;
}

QString GlobalConfiguration::getColorMapNameForProperty(
const QString &propertyName) const {
auto it = surfacePropertyDescriptions.find(propertyName);
if (it != surfacePropertyDescriptions.end()) {
return it->cmap;
}
return "Viridis";
return surfacePropertyDescriptions.get(propertyName).cmap;
}
13 changes: 6 additions & 7 deletions src/core/globalconfiguration.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "isosurface_parameters.h"
#include "colormap.h"
#include "isosurface_parameters.h"
#include <QMutex>
#include <QMutexLocker>

Expand All @@ -9,11 +9,11 @@ class GlobalConfiguration {
static GlobalConfiguration *instance;
static QMutex mutex;

QMap<QString, isosurface::SurfacePropertyDescription>
surfacePropertyDescriptions;
QMap<QString, isosurface::SurfaceDescription> surfaceDescriptions;
isosurface::SurfacePropertyDescriptions surfacePropertyDescriptions;
isosurface::SurfaceDescriptions surfaceDescriptions;
QMap<QString, double> surfaceResolutionLevels;
QMap<QString, ColorMapDescription> colorMapDescriptions;
bool m_haveData{false};

protected:
GlobalConfiguration() {} // Constructor must be protected or private
Expand All @@ -29,10 +29,9 @@ class GlobalConfiguration {

bool load();

const QMap<QString, isosurface::SurfacePropertyDescription> &
const isosurface::SurfacePropertyDescriptions &
getPropertyDescriptions() const;
const QMap<QString, isosurface::SurfaceDescription> &
getSurfaceDescriptions() const;
const isosurface::SurfaceDescriptions &getSurfaceDescriptions() const;
const QMap<QString, double> &getSurfaceResolutionLevels() const;
QString getColorMapNameForProperty(const QString &) const;

Expand Down
65 changes: 39 additions & 26 deletions src/core/isosurface_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ QString kindToString(Kind kind) {
return "electron_density";
case Kind::DeformationDensity:
return "deformation_density";
case Kind::Orbital:
return "orbital";
default:
return "unknown";
};
Expand All @@ -120,6 +122,8 @@ QString defaultPropertyForKind(Kind kind) {
return "dnorm";
case Kind::DeformationDensity:
return "None";
case Kind::Orbital:
return "Isovalue";
default:
return "unknown";
};
Expand All @@ -142,13 +146,15 @@ Kind stringToKind(const QString &s) {
else if (s == "def" || s == "deformation_density" ||
s == "Deformation Density")
return Kind::DeformationDensity;
else if (s == "mo" || s == "orbital" || s == "Orbital")
return Kind::Orbital;
else
return Kind::Unknown;
}

QMap<QString, SurfacePropertyDescription>
inline SurfacePropertyDescriptions
loadPropertyDescriptions(const nlohmann::json &json) {
QMap<QString, SurfacePropertyDescription> properties;
SurfacePropertyDescriptions properties;
auto s = [](const std::string &str) { return QString::fromStdString(str); };
qDebug() << "Load property descriptions";

Expand All @@ -161,10 +167,11 @@ loadPropertyDescriptions(const nlohmann::json &json) {
try {
SurfacePropertyDescription spd;
from_json(item.value(), spd);
properties.insert(s(item.key()), spd);
properties.descriptions.insert(spd.displayName, spd);
// allow referring by the occName or displayName as well
properties.insert(spd.occName, spd);
properties.insert(spd.displayName, spd);
properties.displayNameLookup.insert(s(item.key()), spd.displayName);
properties.displayNameLookup.insert(spd.occName, spd.displayName);
properties.displayNameLookup.insert(spd.displayName, spd.displayName);
} catch (nlohmann::json::exception &e) {
qWarning() << "Failed to parse property" << s(item.key()) << ":"
<< e.what();
Expand All @@ -173,9 +180,8 @@ loadPropertyDescriptions(const nlohmann::json &json) {
return properties;
}

QMap<QString, SurfaceDescription>
loadSurfaceDescriptions(const nlohmann::json &json) {
QMap<QString, SurfaceDescription> surfaces;
inline SurfaceDescriptions loadSurfaceDescriptions(const nlohmann::json &json) {
SurfaceDescriptions surfaces;

if (!json.contains("surfaces") || !json["surfaces"].is_object()) {
qWarning() << "JSON does not contain a 'surfaces' object";
Expand All @@ -188,9 +194,11 @@ loadSurfaceDescriptions(const nlohmann::json &json) {
try {
SurfaceDescription sd;
from_json(item.value(), sd);
surfaces.insert(s(item.key()), sd);
surfaces.insert(sd.occName, sd);
surfaces.insert(sd.displayName, sd);
surfaces.descriptions.insert(sd.displayName, sd);

surfaces.displayNameLookup.insert(s(item.key()), sd.displayName);
surfaces.displayNameLookup.insert(sd.occName, sd.displayName);
surfaces.displayNameLookup.insert(sd.displayName, sd.displayName);
} catch (nlohmann::json::exception &e) {
qWarning() << "Failed to parse surface" << s(item.key()) << ":"
<< e.what();
Expand Down Expand Up @@ -222,9 +230,8 @@ QMap<QString, double> loadResolutionLevels(const nlohmann::json &json) {
}

bool loadSurfaceDescriptionConfiguration(
QMap<QString, SurfacePropertyDescription> &propertyDescriptions,
QMap<QString, SurfaceDescription> &descriptions,
QMap<QString, double> &resolutions) {
SurfacePropertyDescriptions &propertyDescriptions,
SurfaceDescriptions &descriptions, QMap<QString, double> &resolutions) {
QFile file(":/resources/surface_description.json");
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open config file.");
Expand All @@ -247,32 +254,38 @@ bool loadSurfaceDescriptionConfiguration(
return true;
}

SurfaceDescription SurfaceDescriptions::get(const QString &s) const {
auto loc = displayNameLookup.find(s);
if (loc != displayNameLookup.end())
return descriptions.value(*loc);
return {};
}

SurfacePropertyDescription
SurfacePropertyDescriptions::get(const QString &s) const {
auto loc = displayNameLookup.find(s);
if (loc != displayNameLookup.end())
return descriptions.value(*loc);
return {};
}

SurfaceDescription getSurfaceDescription(Kind kind) {
QString s = kindToString(kind);
const auto &descriptions =
GlobalConfiguration::getInstance()->getSurfaceDescriptions();
auto loc = descriptions.find(s);
if (loc != descriptions.end())
return *loc;
return {};
return descriptions.get(s);
}

QString getSurfaceDisplayName(QString s) {
const auto &descriptions =
GlobalConfiguration::getInstance()->getSurfaceDescriptions();
auto loc = descriptions.find(s);
if (loc != descriptions.end())
return (*loc).displayName;
return s;
return descriptions.get(s).displayName;
}

QString getSurfacePropertyDisplayName(QString s) {
const auto &descriptions =
GlobalConfiguration::getInstance()->getPropertyDescriptions();
auto loc = descriptions.find(s);
if (loc != descriptions.end())
return (*loc).displayName;
return s;
return descriptions.get(s).displayName;
}

} // namespace isosurface
56 changes: 36 additions & 20 deletions src/core/isosurface_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ inline const char *resolutionToString(Resolution res) {
}

inline Resolution stringToResolution(const QString &res) {
if (res.compare("Very Low", Qt::CaseInsensitive) == 0) return Resolution::VeryLow;
if (res.compare("Low", Qt::CaseInsensitive) == 0) return Resolution::Low;
if (res.compare("Medium", Qt::CaseInsensitive) == 0) return Resolution::Medium;
if (res.compare("High", Qt::CaseInsensitive) == 0) return Resolution::High;
if (res.compare("Very High", Qt::CaseInsensitive) == 0) return Resolution::VeryHigh;
if (res.compare("Absurd", Qt::CaseInsensitive) == 0) return Resolution::Absurd;
return Resolution::Custom; // Default case
if (res.compare("Very Low", Qt::CaseInsensitive) == 0)
return Resolution::VeryLow;
if (res.compare("Low", Qt::CaseInsensitive) == 0)
return Resolution::Low;
if (res.compare("Medium", Qt::CaseInsensitive) == 0)
return Resolution::Medium;
if (res.compare("High", Qt::CaseInsensitive) == 0)
return Resolution::High;
if (res.compare("Very High", Qt::CaseInsensitive) == 0)
return Resolution::VeryHigh;
if (res.compare("Absurd", Qt::CaseInsensitive) == 0)
return Resolution::Absurd;
return Resolution::Custom; // Default case
}

enum class Kind {
Expand All @@ -68,9 +74,9 @@ enum class Kind {
};

struct OrbitalDetails {
QString label{"HOMO"};
int index{0};
bool occupied{true};
QString label{"HOMO"};
int index{0};
bool occupied{true};
};

struct Parameters {
Expand All @@ -82,7 +88,6 @@ struct Parameters {
MolecularWavefunction *wfn{nullptr};
Eigen::Isometry3d wfn_transform{Eigen::Isometry3d::Identity()};
QStringList additionalProperties;

};

struct Result {
Expand Down Expand Up @@ -122,18 +127,29 @@ struct SurfaceDescription {

SurfaceDescription getSurfaceDescription(Kind);

struct SurfaceDescriptions {
QMap<QString, SurfaceDescription> descriptions;
QMap<QString, QString> displayNameLookup;
SurfaceDescription get(const QString &) const;
};

struct SurfacePropertyDescriptions {
QMap<QString, SurfacePropertyDescription> descriptions;
QMap<QString, QString> displayNameLookup;
SurfacePropertyDescription get(const QString &) const;
};

QString getSurfaceDisplayName(QString);
QString getSurfacePropertyDisplayName(QString);

bool loadSurfaceDescriptionConfiguration(
QMap<QString, SurfacePropertyDescription> &,
QMap<QString, SurfaceDescription> &, QMap<QString, double> &);
bool loadSurfaceDescriptionConfiguration(SurfacePropertyDescriptions &,
SurfaceDescriptions &,
QMap<QString, double> &);

} // namespace isosurface

void to_json(nlohmann::json& j, const isosurface::SurfacePropertyDescription&);
void from_json(const nlohmann::json& j, isosurface::SurfacePropertyDescription&);
void to_json(nlohmann::json& j, const isosurface::SurfaceDescription&);
void from_json(const nlohmann::json& j, isosurface::SurfaceDescription&);


void to_json(nlohmann::json &j, const isosurface::SurfacePropertyDescription &);
void from_json(const nlohmann::json &j,
isosurface::SurfacePropertyDescription &);
void to_json(nlohmann::json &j, const isosurface::SurfaceDescription &);
void from_json(const nlohmann::json &j, isosurface::SurfaceDescription &);
Loading

0 comments on commit b289773

Please sign in to comment.