Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ declare abstract class PointsGridFormDataProvider<N extends Schema["name"]> exte
private initInstanceFields;
private getDefaultGridMetricValue;
private resolveGridMetricValue;
/** Keep gridMetricValue and dimensions consistent (both preferGridMetric modes). */
private dataWithEffectiveGridMetric;
private applyGridMetricInstanceFields;
getData(): Data;
getDefaultData(): PointsGridDataProviderSchema;
protected get jsonSchemaPatchConfig(): {
Expand Down
74 changes: 35 additions & 39 deletions dist/js/context/providers/PointsGrid/PointsGridFormDataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,39 @@ class PointsGridFormDataProvider extends JSONSchemaFormDataProvider_1.default {
const isValid = gridMetricType === "KPPRA" ? gridMetricValue >= 1 : gridMetricValue > 0;
return isValid ? gridMetricValue : this.getDefaultGridMetricValue(gridMetricType);
}
getData() {
const data = super.getData();
const { preferGridMetric, gridMetricType, gridMetricValue } = data;
if (!preferGridMetric || !gridMetricType) {
return data;
/** Keep gridMetricValue and dimensions consistent (both preferGridMetric modes). */
dataWithEffectiveGridMetric(data) {
const { preferGridMetric, gridMetricType, gridMetricValue, dimensions } = data;
if (preferGridMetric && gridMetricType) {
const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
return {
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
};
}
const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
return {
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
};
if (!preferGridMetric && gridMetricType && dimensions.every((d) => typeof d === "number")) {
return {
...data,
gridMetricValue: this.calculateGridMetric(gridMetricType, dimensions),
};
}
return data;
}
applyGridMetricInstanceFields(data) {
const { gridMetricType, preferGridMetric, gridMetricValue } = data;
if (preferGridMetric !== undefined) {
this.preferGridMetric = preferGridMetric;
}
if (gridMetricType !== undefined) {
this.gridMetricType = gridMetricType;
}
if (gridMetricValue !== undefined) {
this.gridMetricValue = gridMetricValue;
}
}
getData() {
return this.dataWithEffectiveGridMetric(super.getData());
}
getDefaultData() {
const defaultData = {
Expand Down Expand Up @@ -236,34 +257,9 @@ class PointsGridFormDataProvider extends JSONSchemaFormDataProvider_1.default {
}
}
setData(data) {
const { dimensions, gridMetricType, preferGridMetric, gridMetricValue } = data;
if (preferGridMetric !== undefined) {
this.preferGridMetric = preferGridMetric;
}
if (gridMetricType !== undefined) {
this.gridMetricType = gridMetricType;
}
if (preferGridMetric && gridMetricType) {
const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
this.gridMetricValue = effectiveValue;
return super.setData({
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
});
}
if (!preferGridMetric && dimensions.every((d) => typeof d === "number")) {
const derivedMetric = this.calculateGridMetric(gridMetricType, dimensions);
this.gridMetricValue = derivedMetric;
return super.setData({
...data,
gridMetricValue: derivedMetric,
});
}
if (gridMetricValue !== undefined) {
this.gridMetricValue = gridMetricValue;
}
return super.setData(data);
const synced = this.dataWithEffectiveGridMetric(data);
this.applyGridMetricInstanceFields(synced);
return super.setData(synced);
}
}
(0, MaterialContextMixin_1.default)(PointsGridFormDataProvider.prototype);
Expand Down
12 changes: 12 additions & 0 deletions dist/js/context/providers/PointsGrid/kgridPrecision.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { PointsGridDataProviderSchema } from "@mat3ra/esse/dist/js/types";
import type { MaterialExternalContext } from "../../mixins/MaterialContextMixin";
type UnitContext = Array<{
name?: string;
data?: PointsGridDataProviderSchema;
}> | unknown;
export declare function getKgridDataFromUnitContext(context: UnitContext): PointsGridDataProviderSchema | undefined;
export declare function getEffectiveKgridPrecision(kgridData: PointsGridDataProviderSchema, externalContext: MaterialExternalContext): {
value: number;
metric: NonNullable<PointsGridDataProviderSchema["gridMetricType"]>;
};
export {};
31 changes: 31 additions & 0 deletions dist/js/context/providers/PointsGrid/kgridPrecision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getKgridDataFromUnitContext = getKgridDataFromUnitContext;
exports.getEffectiveKgridPrecision = getEffectiveKgridPrecision;
const KGridFormDataManager_1 = __importDefault(require("./KGridFormDataManager"));
function getKgridDataFromUnitContext(context) {
if (!context) {
return undefined;
}
if (Array.isArray(context)) {
const item = context.find((entry) => (entry === null || entry === void 0 ? void 0 : entry.name) === "kgrid");
return item === null || item === void 0 ? void 0 : item.data;
}
if (typeof context === "object" && context !== null && "kgrid" in context) {
return context.kgrid;
}
return undefined;
}
function getEffectiveKgridPrecision(kgridData, externalContext) {
var _a, _b;
const provider = new KGridFormDataManager_1.default({ name: "kgrid", data: kgridData }, externalContext);
provider.setData(provider.getData());
const normalized = provider.getData();
return {
value: (_a = normalized.gridMetricValue) !== null && _a !== void 0 ? _a : -1,
metric: (_b = normalized.gridMetricType) !== null && _b !== void 0 ? _b : "KPPRA",
};
}
2 changes: 2 additions & 0 deletions dist/js/context/providers/base/ContextProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ DataForRendering = S["data"]> {
setIsEdited(isEdited: boolean): void;
getData(): S["data"];
setData(data: S["data"]): void;
/** Re-run `setData` normalization before persisting (e.g. derive grid metric from dimensions). */
syncPersistentData(): void;
/**
* Derive template-facing `data` from persisted `data`. Override when the template needs fields
* that must not be stored (e.g. coordinates from symmetry point names + lattice).
Expand Down
4 changes: 4 additions & 0 deletions dist/js/context/providers/base/ContextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class ContextProvider {
setData(data) {
this.data = utils_1.Utils.clone.deepClone(data);
}
/** Re-run `setData` normalization before persisting (e.g. derive grid metric from dimensions). */
syncPersistentData() {
this.setData(this.getData());
}
/**
* Derive template-facing `data` from persisted `data`. Override when the template needs fields
* that must not be stored (e.g. coordinates from symmetry point names + lattice).
Expand Down
7 changes: 6 additions & 1 deletion dist/js/units/ExecutionUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const enums_1 = require("../enums");
const ExecutionUnitSchemaMixin_1 = require("../generated/ExecutionUnitSchemaMixin");
const BaseUnit_1 = __importDefault(require("./BaseUnit"));
const ExecutionUnitInput_1 = __importDefault(require("./ExecutionUnitInput"));
/** Context items always serialized on the unit so rupy can store them in scope after execution. */
const CONTEXT_SCOPE_ITEMS = new Set(["kgrid"]);
class ExecutionUnit extends BaseUnit_1.default {
static get jsonSchema() {
return JSONSchemasInterface_1.default.getSchemaById("workflow/unit/execution");
Expand Down Expand Up @@ -147,8 +149,11 @@ class ExecutionUnit extends BaseUnit_1.default {
});
}
savePersistentContext() {
this.contextProvidersInstances
.filter((p) => CONTEXT_SCOPE_ITEMS.has(p.name))
.forEach((p) => p.syncPersistentData());
const persistentItems = this.contextProvidersInstances.map((p) => p.getContextItemData());
this.context = persistentItems.filter((c) => c.isEdited);
this.context = persistentItems.filter((c) => c.isEdited || CONTEXT_SCOPE_ITEMS.has(c.name));
}
saveRenderingContext(externalContext) {
const renderingItems = this.contextProvidersInstances.map((p) => p.getContextItemDataForRendering());
Expand Down
83 changes: 37 additions & 46 deletions src/js/context/providers/PointsGrid/PointsGridFormDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,44 @@ abstract class PointsGridFormDataProvider<
return isValid ? gridMetricValue : this.getDefaultGridMetricValue(gridMetricType);
}

getData(): Data {
const data = super.getData();
const { preferGridMetric, gridMetricType, gridMetricValue } = data;
/** Keep gridMetricValue and dimensions consistent (both preferGridMetric modes). */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// as comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for this

private dataWithEffectiveGridMetric(data: Data): Data {
const { preferGridMetric, gridMetricType, gridMetricValue, dimensions } = data;

if (!preferGridMetric || !gridMetricType) {
return data;
if (preferGridMetric && gridMetricType) {
const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
return {
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
};
}

const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
if (!preferGridMetric && gridMetricType && dimensions.every((d) => typeof d === "number")) {
return {
...data,
gridMetricValue: this.calculateGridMetric(gridMetricType, dimensions),
};
}

return {
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
};
return data;
}

private applyGridMetricInstanceFields(data: Data) {
const { gridMetricType, preferGridMetric, gridMetricValue } = data;
if (preferGridMetric !== undefined) {
this.preferGridMetric = preferGridMetric;
}
if (gridMetricType !== undefined) {
this.gridMetricType = gridMetricType;
}
if (gridMetricValue !== undefined) {
this.gridMetricValue = gridMetricValue;
}
}

getData(): Data {
return this.dataWithEffectiveGridMetric(super.getData());
}

getDefaultData() {
Expand Down Expand Up @@ -328,41 +351,9 @@ abstract class PointsGridFormDataProvider<
}

setData(data: Data) {
const { dimensions, gridMetricType, preferGridMetric, gridMetricValue } = data;

if (preferGridMetric !== undefined) {
this.preferGridMetric = preferGridMetric;
}
if (gridMetricType !== undefined) {
this.gridMetricType = gridMetricType;
}

if (preferGridMetric && gridMetricType) {
const effectiveValue = this.resolveGridMetricValue(gridMetricType, gridMetricValue);
this.gridMetricValue = effectiveValue;

return super.setData({
...data,
gridMetricValue: effectiveValue,
dimensions: this.calculateDimensions(gridMetricType, effectiveValue),
});
}

if (!preferGridMetric && dimensions.every((d) => typeof d === "number")) {
const derivedMetric = this.calculateGridMetric(gridMetricType, dimensions);
this.gridMetricValue = derivedMetric;

return super.setData({
...data,
gridMetricValue: derivedMetric,
});
}

if (gridMetricValue !== undefined) {
this.gridMetricValue = gridMetricValue;
}

return super.setData(data);
const synced = this.dataWithEffectiveGridMetric(data);
this.applyGridMetricInstanceFields(synced);
return super.setData(synced);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/js/context/providers/base/ContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ abstract class ContextProvider<
this.data = Utils.clone.deepClone(data);
}

/** Re-run `setData` normalization before persisting (e.g. derive grid metric from dimensions). */
syncPersistentData() {
this.setData(this.getData());
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now for example if we use (2 2 2) kgrid on Si2, then the gridMetricValue is still shown "5" by default in UI -- which is not true, the "16" value is used in calculations.
Not isEdited=True -- thus it is not updated, while it would be if it was edited.


/**
* Derive template-facing `data` from persisted `data`. Override when the template needs fields
* that must not be stored (e.g. coordinates from symmetry point names + lattice).
Expand Down
9 changes: 8 additions & 1 deletion src/js/units/ExecutionUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type SetExecutableProps = {
flavorName?: string;
};

/** Context items always serialized on the unit so rupy can store them in scope after execution. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

const CONTEXT_SCOPE_ITEMS = new Set(["kgrid"]);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No premature optimization. Either storing full contexts on each unit -- which will be too much, or specifying those that are actually needed during the job running

class ExecutionUnit extends (BaseUnit as Base) implements Schema {
inputInstances: ExecutionUnitInput[] = [];

Expand Down Expand Up @@ -228,8 +231,12 @@ class ExecutionUnit extends (BaseUnit as Base) implements Schema {
}

savePersistentContext() {
this.contextProvidersInstances
.filter((p) => CONTEXT_SCOPE_ITEMS.has(p.name))
.forEach((p) => p.syncPersistentData());

const persistentItems = this.contextProvidersInstances.map((p) => p.getContextItemData());
this.context = persistentItems.filter((c) => c.isEdited);
this.context = persistentItems.filter((c) => c.isEdited || CONTEXT_SCOPE_ITEMS.has(c.name));
}

saveRenderingContext(externalContext: ExternalContext) {
Expand Down
Loading