Skip to content

Commit

Permalink
testSimulation function added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 20, 2024
1 parent 438aef8 commit 94005c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
37 changes: 36 additions & 1 deletion src/lib/analysis/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { TotalSummary, TotalSummaryWeights } from '../types/analysis';
import type { TypesConfig, WorldConfig } from '../types/config';
import { Simulation } from '../simulation';
import { Runner } from '../runner';
import { CompoundsAnalyzer } from '../analysis/compounds';
import { createFilledArray, normalizeMatrixColumnsUnion, repeatArrayValues } from '../math';
import { fullCopyObject } from "@/lib/utils/functions";
import { createPhysicModel, fullCopyObject } from '../utils/functions';
import { create2dRandomDistribution } from '../config/atoms';
import { averageMatrixColumns } from '../math/operations';
import { createDummyDrawer } from '../drawer/dummy';

export function createTransparentWeights(): TotalSummaryWeights {
return {
Expand Down Expand Up @@ -129,3 +136,31 @@ export function normalizeSummaryMatrix(matrix: number[][], typesCount: number):

return result;
}

export function testSimulation(worldConfig: WorldConfig, typesConfig: TypesConfig, steps: number[]): number[] {
const sim = new Simulation({
viewMode: '2d',
worldConfig: worldConfig,
typesConfig: typesConfig,
physicModel: createPhysicModel(worldConfig, typesConfig),
atomsFactory: create2dRandomDistribution,
drawer: createDummyDrawer(),
});

const runner = new Runner(sim);
const summaryMatrix: number[][] = [];

for (const stepsCount of steps) {
runner.runSteps(stepsCount);

const compounds = new CompoundsAnalyzer(sim.exportCompounds(), sim.atoms, typesConfig.FREQUENCIES.length);
const totalSummary: TotalSummary = {
WORLD: sim.summary,
COMPOUNDS: compounds.summary,
};
const rawMatrix = convertSummaryToSummaryMatrixRow(totalSummary);
summaryMatrix.push(rawMatrix);
}

return averageMatrixColumns(summaryMatrix);
}
38 changes: 4 additions & 34 deletions src/scripts/actions/test-simulation-parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Pool } from "multiprocess-pool";
import type { TypesConfig, WorldConfig } from '@/lib/types/config';
import { createBaseWorldConfig } from '@/lib/config/world';
import { createBaseTypesConfig } from '@/lib/config/types';
import type { TotalSummary } from "@/lib/types/analysis";
import {
convertWeightsToSummaryMatrixRow,
createTransparentWeights,
Expand All @@ -20,42 +19,13 @@ export const simulationTask = async (
worldConfig.TEMPERATURE_FUNCTION = () => 1;

const dirName = __dirname.replace('/node_modules/multiprocess-pool/dist', '/src');
const { createPhysicModel } = await import(`${dirName}/lib/utils/functions`);
const { create2dRandomDistribution } = await import(`${dirName}/lib/config/atoms`);
const { createDummyDrawer } = await import(`${dirName}/lib/drawer/dummy`);
const { Simulation } = await import(`${dirName}/lib/simulation`);
const { Runner } = await import(`${dirName}/lib/runner`);
const { CompoundsAnalyzer } = await import(`${dirName}/lib/analysis/compounds`);
const { convertSummaryToSummaryMatrixRow } = await import(`${dirName}/lib/analysis/helpers`);
const { averageMatrixColumns } = await import(`${dirName}/lib/math/operations`);

const sim = new Simulation({
viewMode: '2d',
worldConfig: worldConfig,
typesConfig: typesConfig,
physicModel: createPhysicModel(worldConfig, typesConfig),
atomsFactory: create2dRandomDistribution,
drawer: createDummyDrawer(),
});

const runner = new Runner(sim);
const summaryMatrix: number[][] = [];

for (const stepsCount of steps) {
runner.runSteps(stepsCount);

const compounds = new CompoundsAnalyzer(sim.exportCompounds(), sim.atoms, typesConfig.FREQUENCIES.length);
const totalSummary: TotalSummary = {
WORLD: sim.summary,
COMPOUNDS: compounds.summary,
};
const rawMatrix = convertSummaryToSummaryMatrixRow(totalSummary);
summaryMatrix.push(rawMatrix);
}
const { testSimulation } = await import(`${dirName}/lib/analysis/helpers`);

const result = testSimulation(worldConfig, typesConfig, steps);

console.log(`<- task ${id} finished in ${Date.now() - ts} ms`);

return averageMatrixColumns(summaryMatrix);
return result;
}

export const actionTestSimulationParallel = async (...args: string[]) => {
Expand Down

0 comments on commit 94005c6

Please sign in to comment.