@mat3ra/made 2024.3.27-0 → 2024.3.30-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,7 @@ export declare class Basis {
60
60
  static get defaultCell(): [import("@mat3ra/esse/dist/js/types").ArrayOf3NumberElementsSchema, import("@mat3ra/esse/dist/js/types").ArrayOf3NumberElementsSchema, import("@mat3ra/esse/dist/js/types").ArrayOf3NumberElementsSchema];
61
61
  /**
62
62
  * Serialize class instance to JSON.
63
+ * @param skipRounding - Whether to skip rounding the resulting lattice values, defaults to `false`.
63
64
  * @example As below:
64
65
  {
65
66
  "elements" : [
@@ -95,12 +96,12 @@ export declare class Basis {
95
96
  [
96
97
  1,
97
98
  0,
98
- 6.12323399573677e-17
99
+ 0
99
100
  ],
100
101
  [
101
- 1.60812264967664e-16,
102
+ 0,
102
103
  1,
103
- 6.12323399573677e-17
104
+ 0
104
105
  ],
105
106
  [
106
107
  0,
@@ -110,7 +111,14 @@ export declare class Basis {
110
111
  ]
111
112
  }
112
113
  */
113
- toJSON(): BasisSchema;
114
+ toJSON(skipRounding?: boolean): BasisSchema;
115
+ /** Return coordinates rounded to default precision */
116
+ get coordinatesRounded(): {
117
+ id: number;
118
+ value: number[];
119
+ }[];
120
+ /** Return cell with vectors values rounded to default precision */
121
+ get cellRounded(): number[][];
114
122
  /**
115
123
  * Create an identical copy of the class instance.
116
124
  * @param extraContext - Extra context to be passed to the new class instance on creation.
@@ -45,6 +45,7 @@ class Basis {
45
45
  }
46
46
  /**
47
47
  * Serialize class instance to JSON.
48
+ * @param skipRounding - Whether to skip rounding the resulting lattice values, defaults to `false`.
48
49
  * @example As below:
49
50
  {
50
51
  "elements" : [
@@ -80,12 +81,12 @@ class Basis {
80
81
  [
81
82
  1,
82
83
  0,
83
- 6.12323399573677e-17
84
+ 0
84
85
  ],
85
86
  [
86
- 1.60812264967664e-16,
87
+ 0,
87
88
  1,
88
- 6.12323399573677e-17
89
+ 0
89
90
  ],
90
91
  [
91
92
  0,
@@ -95,12 +96,12 @@ class Basis {
95
96
  ]
96
97
  }
97
98
  */
98
- toJSON() {
99
+ toJSON(skipRounding = false) {
99
100
  const json = {
100
101
  elements: this.elements,
101
- coordinates: this.coordinates,
102
+ coordinates: skipRounding ? this.coordinates : this.coordinatesRounded,
102
103
  units: this.units,
103
- cell: this.cell,
104
+ cell: skipRounding ? this.cell : this.cellRounded,
104
105
  };
105
106
  if (!underscore_1.default.isEmpty(this.labels)) {
106
107
  return JSON.parse(JSON.stringify({
@@ -110,6 +111,19 @@ class Basis {
110
111
  }
111
112
  return JSON.parse(JSON.stringify(json));
112
113
  }
114
+ /** Return coordinates rounded to default precision */
115
+ get coordinatesRounded() {
116
+ return this.coordinates.map((coordinate) => {
117
+ return {
118
+ id: coordinate.id,
119
+ value: coordinate.value.map((x) => math_1.default.precise(math_1.default.roundToZero(x))),
120
+ };
121
+ });
122
+ }
123
+ /** Return cell with vectors values rounded to default precision */
124
+ get cellRounded() {
125
+ return this.cell.map((vector) => vector.map((x) => math_1.default.precise(math_1.default.roundToZero(x))));
126
+ }
113
127
  /**
114
128
  * Create an identical copy of the class instance.
115
129
  * @param extraContext - Extra context to be passed to the new class instance on creation.
@@ -332,9 +346,12 @@ class Basis {
332
346
  get atomicLabelsArray() {
333
347
  var _a;
334
348
  const labelsArray = Array.from({ length: this.elements.length }, (_) => "");
335
- (_a = this.labels) === null || _a === void 0 ? void 0 : _a.map((item) => {
336
- labelsArray[item.id] = item.value.toString();
337
- });
349
+ // https://dev.to/maafaishal/benchmarking-for-while-forof-and-arrayforeach-using-performancenow-1jjg
350
+ if ((_a = this.labels) === null || _a === void 0 ? void 0 : _a.length) {
351
+ for (let i = 0; i < this.labels.length; i++) {
352
+ labelsArray[this.labels[i].id] = this.labels[i].value.toString();
353
+ }
354
+ }
338
355
  return labelsArray;
339
356
  }
340
357
  /* Returns array of elements with labels E.g., ["Fe1", "Fe2", "O", "O"] */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mat3ra/made",
3
- "version": "2024.3.27-0",
3
+ "version": "2024.3.30-0",
4
4
  "description": "MAterials DEsign library",
5
5
  "scripts": {
6
6
  "lint": "eslint --cache src/js tests/js && prettier --write src/js tests/js",
@@ -101,6 +101,7 @@ export class Basis {
101
101
 
102
102
  /**
103
103
  * Serialize class instance to JSON.
104
+ * @param skipRounding - Whether to skip rounding the resulting lattice values, defaults to `false`.
104
105
  * @example As below:
105
106
  {
106
107
  "elements" : [
@@ -136,12 +137,12 @@ export class Basis {
136
137
  [
137
138
  1,
138
139
  0,
139
- 6.12323399573677e-17
140
+ 0
140
141
  ],
141
142
  [
142
- 1.60812264967664e-16,
143
+ 0,
143
144
  1,
144
- 6.12323399573677e-17
145
+ 0
145
146
  ],
146
147
  [
147
148
  0,
@@ -151,12 +152,12 @@ export class Basis {
151
152
  ]
152
153
  }
153
154
  */
154
- toJSON(): BasisSchema {
155
+ toJSON(skipRounding = false): BasisSchema {
155
156
  const json = {
156
157
  elements: this.elements,
157
- coordinates: this.coordinates,
158
+ coordinates: skipRounding ? this.coordinates : this.coordinatesRounded,
158
159
  units: this.units,
159
- cell: this.cell,
160
+ cell: skipRounding ? this.cell : this.cellRounded,
160
161
  };
161
162
 
162
163
  if (!_.isEmpty(this.labels)) {
@@ -171,6 +172,21 @@ export class Basis {
171
172
  return JSON.parse(JSON.stringify(json));
172
173
  }
173
174
 
175
+ /** Return coordinates rounded to default precision */
176
+ get coordinatesRounded() {
177
+ return this.coordinates.map((coordinate) => {
178
+ return {
179
+ id: coordinate.id,
180
+ value: coordinate.value.map((x) => math.precise(math.roundToZero(x))),
181
+ };
182
+ });
183
+ }
184
+
185
+ /** Return cell with vectors values rounded to default precision */
186
+ get cellRounded() {
187
+ return this.cell.map((vector) => vector.map((x) => math.precise(math.roundToZero(x))));
188
+ }
189
+
174
190
  /**
175
191
  * Create an identical copy of the class instance.
176
192
  * @param extraContext - Extra context to be passed to the new class instance on creation.
@@ -424,10 +440,12 @@ export class Basis {
424
440
  /* Returns array of atomic labels E.g., ["1", "2", "", ""] */
425
441
  get atomicLabelsArray(): string[] {
426
442
  const labelsArray = Array.from({ length: this.elements.length }, (_) => "");
427
- this.labels?.map((item: { id: number; value: number }) => {
428
- labelsArray[item.id] = item.value.toString();
429
- });
430
-
443
+ // https://dev.to/maafaishal/benchmarking-for-while-forof-and-arrayforeach-using-performancenow-1jjg
444
+ if (this.labels?.length) {
445
+ for (let i = 0; i < this.labels.length; i++) {
446
+ labelsArray[this.labels[i].id] = this.labels[i].value.toString();
447
+ }
448
+ }
431
449
  return labelsArray;
432
450
  }
433
451