Skip to content
Merged
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
37 changes: 35 additions & 2 deletions examples/src/Viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Viewer extends React.Component<InputParams, ViewerState> {
private panMode = false;
private focusMode = true;
private orthoMode = false;
private smoldynInput = "100";
private netConnectionSettings: NetConnectionParams;

public constructor(props: InputParams) {
Expand Down Expand Up @@ -353,6 +354,28 @@ class Viewer extends React.Component<InputParams, ViewerState> {
});
}

public loadSmoldynSim() {
simulariumController.checkServerHealth(
this.onHealthCheckResponse,
this.netConnectionSettings
);
const fileName = "smoldyn_sim" + uuidv4() + ".simularium"
simulariumController
.startSmoldynSim(this.netConnectionSettings, fileName, this.smoldynInput)
.then(() => {
this.clearPendingFile();
})
.then(() => {
simulariumController.initNewFile(
{ netConnectionSettings: this.netConnectionSettings, },
true,
)
})
.catch((err) => {
console.error(err);
});
}

public clearPendingFile() {
this.setState({ filePending: null });
}
Expand Down Expand Up @@ -430,11 +453,10 @@ class Viewer extends React.Component<InputParams, ViewerState> {

public receiveConvertedFile(): void {
simulariumController
.changeFile(
.initNewFile(
{
netConnectionSettings: this.netConnectionSettings,
},
this.state.conversionFileName
)
.then(() => {
simulariumController.gotoTime(0);
Expand Down Expand Up @@ -773,6 +795,17 @@ class Viewer extends React.Component<InputParams, ViewerState> {
Load a smoldyn trajectory
</button>
<br />
<label>
Initial Rabbit Count:
<input
defaultValue="100"
onChange={(event) => {this.smoldynInput = event.target.value}}
/>
</label>
<button onClick={() => this.loadSmoldynSim()}>
Run Smoldyn
</button>
<br />
Comment on lines +798 to +808
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: disable/otherwise indicate when a smoldyn-live sim is not loaded in trajectory select field?

<button onClick={() => simulariumController.resume()}>
Play
</button>
Expand Down
50 changes: 43 additions & 7 deletions src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export default class SimulariumController {
this.setFocusMode = this.setFocusMode.bind(this);
this.convertTrajectory = this.convertTrajectory.bind(this);
this.setCameraType = this.setCameraType.bind(this);
this.startSmoldynSim = this.startSmoldynSim.bind(this);
this.cancelCurrentFile = this.cancelCurrentFile.bind(this);
this.initNewFile = this.initNewFile.bind(this);
}

private createSimulatorConnection(
Expand Down Expand Up @@ -240,6 +243,8 @@ export default class SimulariumController {
fileType: TrajectoryType,
providedFileName?: string
): Promise<void> {
const fileName = providedFileName ?? `${uuidv4()}.simularium`;
this.cancelCurrentFile(fileName);
try {
if (!this.isRemoteOctopusClientConfigured()) {
this.configureNetwork(netConnectionConfig);
Expand All @@ -250,7 +255,6 @@ export default class SimulariumController {
if (!this.simulator) {
throw new Error("Simulator not initialized");
}
const fileName = providedFileName ?? `${uuidv4()}.simularium`;
return this.octopusClient.convertTrajectory(
dataToConvert,
fileType,
Expand Down Expand Up @@ -278,6 +282,28 @@ export default class SimulariumController {
}
}

public startSmoldynSim(
netConnectionConfig: NetConnectionParams,
fileName: string,
smoldynInput: string
): Promise<void> {
this.cancelCurrentFile(fileName);
try {
if (!this.isRemoteOctopusClientConfigured()) {
this.configureNetwork(netConnectionConfig);
}
if (!this.octopusClient) {
throw new Error("Octopus client not configured");
}
if (!this.simulator) {
throw new Error("Simulator not initialized");
}
return this.octopusClient.sendSmoldynData(fileName, smoldynInput);
} catch (e) {
return Promise.reject(e);
}
}

public gotoTime(time: number): void {
// If in the middle of changing files, ignore any gotoTime requests
if (this.isFileChanging || !this.simulator) return;
Expand Down Expand Up @@ -331,12 +357,7 @@ export default class SimulariumController {
}
}

public changeFile(
connectionParams: SimulatorConnectionParams,
// TODO: push newFileName into connectionParams
newFileName: string,
keepRemoteConnection = false
): Promise<FileReturn> {
public cancelCurrentFile(newFileName: string): void {
this.isFileChanging = true;
this.playBackFile = newFileName;

Expand All @@ -345,7 +366,12 @@ export default class SimulariumController {

this.visData.WaitForFrame(0);
this.visData.clearForNewTrajectory();
}

public initNewFile(
connectionParams: SimulatorConnectionParams,
keepRemoteConnection = false
): Promise<FileReturn> {
const shouldConfigureNewSimulator = !(
keepRemoteConnection && this.isRemoteOctopusClientConfigured()
);
Expand Down Expand Up @@ -391,6 +417,16 @@ export default class SimulariumController {
});
}

public changeFile(
connectionParams: SimulatorConnectionParams,
// TODO: push newFileName into connectionParams
newFileName: string,
keepRemoteConnection = false
): Promise<FileReturn> {
this.cancelCurrentFile(newFileName);
return this.initNewFile(connectionParams, keepRemoteConnection);
}

public markFileChangeAsHandled(): void {
this.isFileChanging = false;
}
Expand Down
16 changes: 16 additions & 0 deletions src/simularium/OctopusClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ export class OctopusServicesClient {
"Request server health check"
);
}

public async sendSmoldynData(
outFileName: string,
smoldynInput: string
): Promise<void> {
await this.webSocketClient.connectToRemoteServer();
this.lastRequestedFile = outFileName;
this.webSocketClient.sendWebSocketRequest(
{
msgType: NetMessageEnum.ID_START_SMOLDYN,
fileName: outFileName,
smoldynInputVal: smoldynInput ?? undefined,
},
"Start smoldyn simulation"
);
}
}
2 changes: 2 additions & 0 deletions src/simularium/WebsocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const enum NetMessageEnum {
ID_CHECK_HEALTH_REQUEST = 22,
ID_SERVER_HEALTHY_RESPONSE = 23,
ID_CANCEL_CONVERSION = 24,
ID_START_SMOLDYN = 25,
// insert new values here before LENGTH
LENGTH,
}
Expand All @@ -70,6 +71,7 @@ export const enum ServerErrorCodes {
FRAME_NOT_FOUND = 5,
FILENAME_MISMATCH = 6,
NO_RUNNING_SIMULATION = 7,
SMOLDYN_ERROR = 8,
LENGTH,
}

Expand Down
Loading