Skip to content

Commit

Permalink
multi sensitivity run action and mutations - share some code with sen…
Browse files Browse the repository at this point in the history
…sitivity
  • Loading branch information
EmmaLRussell committed Sep 1, 2023
1 parent e3c3684 commit 63627c4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 47 deletions.
9 changes: 5 additions & 4 deletions app/static/src/app/store/multiSensitivity/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import userMessages from "../../userMessages";
import {SensitivityMutation} from "../sensitivity/mutations";
import {SensitivityGetter} from "../sensitivity/getters";
import {Dict} from "../../types/utilTypes";
import {SensitivityAction} from "../sensitivity/actions";
import {baseSensitivityActions, runSensitivity, SensitivityAction} from "../sensitivity/actions";
import {MultiSensitivityState} from "./state";
import {MultiSensitivityGetter} from "./getters";

export enum MultiSensitivityAction {
RunMultiSensitivity = "RunMultiSensitivity",
ComputeNext = "ComputeNext"
}

export const actions: ActionTree<MultiSensitivityState, AppState> = {
...baseSensitivityActions,
[MultiSensitivityAction.RunMultiSensitivity](context) {
const {rootState, getters} = context;
const {endTime} = rootState.run;
const batchPars = getters[SensitivityGetter.batchPars];
const batchPars = getters[MultiSensitivityGetter.batchPars];

//runSensitivity(batchPars, endTime, context);
runSensitivity(batchPars, endTime, context, true);
}
}
2 changes: 2 additions & 0 deletions app/static/src/app/store/multiSensitivity/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { MutationTree } from "vuex";
import { MultiSensitivityState } from "./state";
import { SensitivityParameterSettings } from "../sensitivity/state";
import {baseSensitivityMutations} from "../sensitivity/mutations";

export enum MultiSensitivityMutation {
SetParamSettings = "SetParamSettings"
}

export const mutations: MutationTree<MultiSensitivityState> = {
...baseSensitivityMutations,
[MultiSensitivityMutation.SetParamSettings](state:MultiSensitivityState, payload: SensitivityParameterSettings[]) {
state.paramSettings = payload;
}
Expand Down
61 changes: 37 additions & 24 deletions app/static/src/app/store/sensitivity/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
ActionContext, ActionTree, Commit, Dispatch
} from "vuex";
import { AppState, AppType } from "../appState/state";
import { SensitivityState } from "./state";
import {BaseSensitivityState, SensitivityState} from "./state";
import { SensitivityGetter } from "./getters";
import { SensitivityMutation } from "./mutations";
import {BaseSensitivityMutation, SensitivityMutation} from "./mutations";
import { RunAction } from "../run/actions";
import userMessages from "../../userMessages";
import { OdinSensitivityResult } from "../../types/wrapperTypes";
Expand All @@ -17,13 +17,18 @@ import { AdvancedSettings } from "../run/state";
import { convertAdvancedSettingsToOdin } from "../../utils";
import { WodinSensitivitySummaryDownload } from "../../excel/wodinSensitivitySummaryDownload";

export enum BaseSensitivityAction {
ComputeNext = "ComputeNext"
}

export enum SensitivityAction {
RunSensitivity = "RunSensitivity",
RunSensitivityOnRehydrate = "RunSensitivityOnRehydrate",
ComputeNext = "ComputeNext",
DownloadSummary = "DownloadSummary"
}

// TODO: move all these out to utils

const runModelIfRequired = (rootState: AppState, dispatch: Dispatch) => {
// Re-run model if required on sensitivity run so that plotted central traces are correct
if (rootState.run.runRequired) {
Expand Down Expand Up @@ -51,12 +56,17 @@ const batchRunDiscrete = (runner: OdinRunnerDiscrete,
dispatch: Dispatch,
commit: Commit): Batch => {
const batch = runner.batchRunDiscrete(odin, pars, 0, endTime, dt, replicates);
commit(SensitivityMutation.SetRunning, true);
dispatch(SensitivityAction.ComputeNext, batch);
commit(BaseSensitivityMutation.SetRunning, true);
dispatch(BaseSensitivityAction.ComputeNext, batch);
return batch;
};

const runSensitivity = (batchPars: BatchPars, endTime: number, context: ActionContext<SensitivityState, AppState>) => {
export const runSensitivity = (
batchPars: BatchPars,
endTime: number,
context: ActionContext<BaseSensitivityState, AppState>,
multiSensitivity = false
) => {
const {
rootState, commit, dispatch, getters, rootGetters
} = context;
Expand Down Expand Up @@ -89,9 +99,9 @@ const runSensitivity = (batchPars: BatchPars, endTime: number, context: ActionCo
detail: (e as Error).message
};
}
commit(SensitivityMutation.SetResult, payload);
commit(BaseSensitivityMutation.SetResult, payload);

if (getters.parameterSetSensitivityUpdateRequired && !isStochastic) {
if (!multiSensitivity && getters.parameterSetSensitivityUpdateRequired && !isStochastic) {
const parameterSetBatchPars = getters[SensitivityGetter.parameterSetBatchPars];
const parameterSetNames = Object.keys(parameterSetBatchPars);
const parameterSetResults = {} as Dict<OdinSensitivityResult>;
Expand Down Expand Up @@ -123,7 +133,7 @@ const runSensitivity = (batchPars: BatchPars, endTime: number, context: ActionCo
}

if (payload.batch !== null) {
commit(SensitivityMutation.SetUpdateRequired, {
commit(BaseSensitivityMutation.SetUpdateRequired, {
modelChanged: false,
parameterValueChanged: false,
endTimeChanged: false,
Expand All @@ -134,7 +144,25 @@ const runSensitivity = (batchPars: BatchPars, endTime: number, context: ActionCo
}
};

export const baseSensitivityActions: ActionTree<BaseSensitivityState, AppState> = {
[BaseSensitivityAction.ComputeNext](context, batch: Batch) {
const {
commit, dispatch, state
} = context;
const isComplete = batch.compute();
commit(BaseSensitivityMutation.SetResult, { ...state.result, batch });
if (isComplete) {
commit(BaseSensitivityMutation.SetRunning, false);
} else {
setTimeout(() => {
dispatch(BaseSensitivityAction.ComputeNext, batch);
}, 0);
}
}
};

export const actions: ActionTree<SensitivityState, AppState> = {
...baseSensitivityActions,
[SensitivityAction.RunSensitivity](context) {
const { rootState, getters } = context;
const { endTime } = rootState.run;
Expand All @@ -151,21 +179,6 @@ export const actions: ActionTree<SensitivityState, AppState> = {
runSensitivity(pars, endTime, context);
},

[SensitivityAction.ComputeNext](context, batch: Batch) {
const {
commit, dispatch, state
} = context;
const isComplete = batch.compute();
commit(SensitivityMutation.SetResult, { ...state.result, batch });
if (isComplete) {
commit(SensitivityMutation.SetRunning, false);
} else {
setTimeout(() => {
dispatch(SensitivityAction.ComputeNext, batch);
}, 0);
}
},

[SensitivityAction.DownloadSummary](context, filename: string) {
const { commit } = context;
commit(SensitivityMutation.SetDownloading, true);
Expand Down
45 changes: 26 additions & 19 deletions app/static/src/app/store/sensitivity/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MutationTree } from "vuex";
import {
BaseSensitivityState,
SensitivityParameterSettings,
SensitivityPlotExtreme,
SensitivityPlotType,
Expand All @@ -9,16 +10,19 @@ import {
import { OdinSensitivityResult } from "../../types/wrapperTypes";
import { Dict } from "../../types/utilTypes";

export enum BaseSensitivityMutation {
SetRunning = "SetRunning",
SetResult = "SetResult",
SetUpdateRequired = "SetUpdateRequired"
}

export enum SensitivityMutation {
SetParameterToVary = "SetParameterToVary",
SetParamSettings = "SetParamSettings",
SetResult = "SetResult",
SetEndTime = "SetEndTime",
SetUpdateRequired = "SetUpdateRequired",
SetPlotType = "SetPlotType",
SetPlotExtreme = "SetPlotExtreme",
SetPlotTime = "SetPlotTime",
SetRunning = "SetRunning",
SetLoading = "SetLoading",
SetDownloading = "SetDownloading",
SetUserSummaryDownloadFileName = "SetUserSummaryDownloadFileName",
Expand All @@ -28,7 +32,26 @@ export enum SensitivityMutation {
ParameterSetSwapped = "ParameterSetSwapped"
}

export const baseSensitivityMutations: MutationTree<BaseSensitivityState> = {
[BaseSensitivityMutation.SetResult](state: BaseSensitivityState, payload: OdinSensitivityResult) {
state.result = payload;
},

[BaseSensitivityMutation.SetUpdateRequired](state: BaseSensitivityState,
payload: Partial<SensitivityUpdateRequiredReasons>) {
state.sensitivityUpdateRequired = {
...state.sensitivityUpdateRequired,
...payload
};
},

[BaseSensitivityMutation.SetRunning](state: BaseSensitivityState, payload: boolean) {
state.running = payload;
}
};

export const mutations: MutationTree<SensitivityState> = {
...baseSensitivityMutations,
[SensitivityMutation.SetParameterToVary](state: SensitivityState, payload: string | null) {
state.paramSettings.parameterToVary = payload;
state.sensitivityUpdateRequired = {
Expand All @@ -45,18 +68,6 @@ export const mutations: MutationTree<SensitivityState> = {
};
},

[SensitivityMutation.SetResult](state: SensitivityState, payload: OdinSensitivityResult) {
state.result = payload;
},

[SensitivityMutation.SetUpdateRequired](state: SensitivityState,
payload: Partial<SensitivityUpdateRequiredReasons>) {
state.sensitivityUpdateRequired = {
...state.sensitivityUpdateRequired,
...payload
};
},

[SensitivityMutation.SetEndTime](state: SensitivityState, payload: number) {
const prevEndTime = state.result?.inputs?.endTime ? state.result.inputs.endTime : -1;
state.sensitivityUpdateRequired = {
Expand All @@ -77,10 +88,6 @@ export const mutations: MutationTree<SensitivityState> = {
state.plotSettings.time = payload;
},

[SensitivityMutation.SetRunning](state: SensitivityState, payload: boolean) {
state.running = payload;
},

[SensitivityMutation.SetLoading](state: SensitivityState, payload: boolean) {
state.loading = payload;
},
Expand Down

0 comments on commit 63627c4

Please sign in to comment.