diff --git a/src/include/meta_properties/pseudopotential.js b/src/include/meta_properties/pseudopotential.js index 62457e4..6928312 100644 --- a/src/include/meta_properties/pseudopotential.js +++ b/src/include/meta_properties/pseudopotential.js @@ -65,18 +65,26 @@ export class Pseudopotential extends Property { } /** - * @summary Exclusive filter of raw data by all fields of the passed object - * @param {Array} rawData + * @summary Exclusive filter of raw pseudopotential array by approximation and functional + * @param {Pseudopotential[]} pseudos * @param {Object} exchangeCorrelation * @param {String} exchangeCorrelation.approximation * @param {String} exchangeCorrelation.functional */ - static filterRawDataByExchangeCorrelation(rawData, exchangeCorrelation) { - const { functional } = exchangeCorrelation; + static filterRawDataByExchangeCorrelation(pseudos, exchangeCorrelation) { + const { approximation, functional } = exchangeCorrelation; + if (!functional && !approximation) return pseudos; + + if (!functional) { + return pseudos.filter( + (item) => item.exchangeCorrelation?.approximation === approximation, + ); + } + const isCompatibleWithOther = Object.keys(this.compatibleExchangeCorrelation).includes( functional, ); - return rawData.filter((item) => { + return pseudos.filter((item) => { return isCompatibleWithOther ? this.compatibleExchangeCorrelation[functional].includes( item.exchangeCorrelation?.functional, @@ -95,16 +103,19 @@ export class Pseudopotential extends Property { return Pseudopotential.filterUnique(this.filterByAppName(array, appName)); } - static filterRawDataByPath(rawData, pseudoPath = "") { + static filterRawDataByPath(pseudos, pseudoPath = "") { + if (!pseudoPath) return pseudos; const regexp = new RegExp(pseudoPath); - return rawData.filter((el) => el.path.match(regexp)); + return pseudos.filter((el) => el.path.match(regexp)); } static filterByAppName(pseudos, appName) { + if (!appName) return pseudos; return pseudos.filter((pseudo) => pseudo.apps.includes(appName)); } static filterByElements(pseudos, elements) { + if (!elements || elements.length === 0) return pseudos; return pseudos.filter((pseudo) => elements.includes(pseudo.element)); } diff --git a/tests/pseudopotential.test.js b/tests/pseudopotential.test.js index 5379014..613f668 100644 --- a/tests/pseudopotential.test.js +++ b/tests/pseudopotential.test.js @@ -154,6 +154,23 @@ describe("Pseudopotentials", () => { expect(sortedPseudos).to.have.length(3); // there are 3 PBE pseudos above expect(sortedPseudos.map((p) => p.exchangeCorrelation.functional)).to.include("pbe"); }); + it("can be filtered by exchange-correlation approximation only", () => { + const exchangeCorrelation = { approximation: "lda", functional: "" }; + const sortedPseudos = Pseudopotential.filterRawDataByExchangeCorrelation( + pseudos, + exchangeCorrelation, + ); + expect(sortedPseudos).to.have.length(1); // there is 1 LDA pseudo above + expect(sortedPseudos.map((p) => p.exchangeCorrelation.functional)).to.include("pz"); + }); + it("should return original array if approximation and functional are falsy", () => { + const exchangeCorrelation = { approximation: "", functional: "" }; + const sortedPseudos = Pseudopotential.filterRawDataByExchangeCorrelation( + pseudos, + exchangeCorrelation, + ); + expect(sortedPseudos).to.have.length(PSEUDO_CONFIGS.length); // there is 1 LDA pseudo above + }); it("can be filtered by pseudopotential type", () => { const filtered = Pseudopotential.filterByType(pseudos, "paw"); const filteredWithObject = Pseudopotential.applyPseudoFilters(pseudos, { type: "paw" });