Skip to content

Commit

Permalink
implemented getHeight function
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 12, 2024
1 parent 7656837 commit 881dd63
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 32 deletions.
2 changes: 1 addition & 1 deletion include/communicator_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace xlpmg
inline const Message GET_MAX_TIMESTEPS_MESSAGE = {MessageType::FUNCTION_CALL, "get_max_timesteps"};

//! Tells the server to start sending height data. (buffered)
inline const Message GET_HEIGHT_DATA_MESSAGE = {MessageType::SERVER__CALL, "get_height_data"};
inline const Message GET_HEIGHT_DATA_MESSAGE = {MessageType::FUNCTION_CALL, "get_height_data"};

//! Tells the client that a buffered sending operation has finished.
inline const Message BUFFERED_SEND_FINISHED = {MessageType::SERVER_RESPONSE, "buff_send_finished"};
Expand Down
73 changes: 53 additions & 20 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void exitSimulationThread()
// wait until thread has finished
m_simulationThread.join();
std::cout << "Thread terminated." << std::endl;
//prepare for next run
// prepare for next run
m_isSimulationRunning = false;
simulator->shouldExit(false);
}
Expand All @@ -53,56 +53,89 @@ int main(int i_argc, char *i_argv[])
l_communicator.startServer(m_PORT);
while (!m_EXIT)
{
std::string rawData = l_communicator.receiveFromClient();
std::string l_rawData = l_communicator.receiveFromClient();
// check if client sent valid json, go back to reading if not
if (!json::accept(rawData))
if (!json::accept(l_rawData))
{
continue;
}

json parsedData = json::parse(rawData);
xlpmg::Message message = xlpmg::jsonToMessage(parsedData);
xlpmg::MessageType type = message.type;
std::string key = message.key;
json args = message.args;
if (type == xlpmg::SERVER__CALL)
json l_parsedData = json::parse(l_rawData);
xlpmg::Message l_message = xlpmg::jsonToMessage(l_parsedData);
xlpmg::MessageType l_type = l_message.type;
std::string l_key = l_message.key;
json l_args = l_message.args;
if (l_type == xlpmg::SERVER__CALL)
{
if (key == xlpmg::SHUTDOWN_SERVER_MESSAGE.key)
if (l_key == xlpmg::SHUTDOWN_SERVER_MESSAGE.key)
{
m_EXIT = true;
l_communicator.stopServer();
exitSimulationThread();
}
else if (key == xlpmg::START_SIMULATION_MESSAGE.key)
else if (l_key == xlpmg::START_SIMULATION_MESSAGE.key)
{
std::string config = parsedData.at(xlpmg::ARGS);
std::string l_config = l_parsedData.at(xlpmg::ARGS);
if (!m_isSimulationRunning)
{
m_simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, config);
m_simulationThread = std::thread(&tsunami_lab::Simulator::start, simulator, l_config);
m_isSimulationRunning = true;
}
}
else if (key == xlpmg::KILL_SIMULATION_MESSAGE.key)
else if (l_key == xlpmg::KILL_SIMULATION_MESSAGE.key)
{
exitSimulationThread();
}
else if (key == xlpmg::RECOMPILE_MESSAGE.key)
else if (l_key == xlpmg::RECOMPILE_MESSAGE.key)
{
// Shutdown server
m_EXIT = true;
l_communicator.stopServer();
exitSimulationThread();
// execute recompilation
std::string env = parsedData.at(xlpmg::ARGS).value("ENV", ""); // environment var
std::string opt = parsedData.at(xlpmg::ARGS).value("OPT", ""); // compiler opt
std::string env = l_args.value("ENV", ""); // environment var
std::string opt = l_args.value("OPT", ""); // compiler opt
exec("chmod +x runServer.sh");
exec("./runServer.sh \"" + env + "\" \"" + opt + "\" &");
}
}
else if (type == xlpmg::FUNCTION_CALL)
else if (l_type == xlpmg::FUNCTION_CALL)
{
if(key == xlpmg::GET_TIMESTEP_MESSAGE.key){
l_communicator.sendToClient(std::to_string(simulator->getTimeStep()));
if (l_key == xlpmg::GET_TIMESTEP_MESSAGE.key)
{
xlpmg::Message response = {xlpmg::SERVER_RESPONSE, "time_step_data", simulator->getTimeStep()};
l_communicator.sendToClient(xlpmg::messageToJsonString(response));
}
else if (l_key == xlpmg::GET_HEIGHT_DATA_MESSAGE.key)
{
xlpmg::Message heightDataMsg = {xlpmg::SERVER_RESPONSE, "height_data", nullptr};
unsigned long l_headerSize = sizeof(heightDataMsg);
// calculate the remaining space left for one message
unsigned long l_sendDataSpace = xlpmg::BUFF_SIZE - l_headerSize;
l_sendDataSpace = 100;
// get data from simulation
if (simulator->getWaveProp() != nullptr)
{
tsunami_lab::patches::WavePropagation *waveprop = simulator->getWaveProp();
const tsunami_lab::t_real *heightData = waveprop->getHeight();
// calculate array size
tsunami_lab::t_idx l_ncellsX, l_ncellsY;
simulator->getCellAmount(l_ncellsX, l_ncellsY);
unsigned long totalCells = l_ncellsX * l_ncellsY;

unsigned long cellCounter = 0;
while (totalCells > 0)
{
while (sizeof(heightDataMsg.args) < l_sendDataSpace && totalCells > 0)
{
heightDataMsg.args.push_back(heightData[cellCounter]);
totalCells--;
}
l_communicator.sendToClient(xlpmg::messageToJsonString(heightDataMsg));
heightDataMsg.args = "";
}
l_communicator.sendToClient(xlpmg::messageToJsonString(xlpmg::BUFFERED_SEND_FINISHED));
}
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,21 @@ void tsunami_lab::Simulator::runCalculation()

void tsunami_lab::Simulator::freeMemory()
{
delete m_setup;
delete m_waveProp;
if (m_setup != nullptr)
{
delete m_setup;
}
if (m_waveProp != nullptr)
{
delete m_waveProp;
}
if (m_useFileIO)
{
std::filesystem::remove(m_checkPointFilePathString);
delete m_netCdf;
if (m_netCdf != nullptr)
{
delete m_netCdf;
}
for (tsunami_lab::io::Station *l_s : m_stations)
{
delete l_s;
Expand Down
7 changes: 7 additions & 0 deletions src/Simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ class tsunami_lab::Simulator
return m_timeStep;
}

void getCellAmount(tsunami_lab::t_idx &o_ncellsX,
tsunami_lab::t_idx &o_ncellsY)
{
o_ncellsX = m_nx;
o_ncellsY = m_ny;
}

//------------------------------------------//
//-----------------SETTERS------------------//
//------------------------------------------//
Expand Down
45 changes: 37 additions & 8 deletions src/ui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "GUI.h"
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include "Communicator.hpp"
#include "communicator_api.h"
#include "../constants.h"

#define GL_SILENCE_DEPRECATION
#if defined(IMGUI_IMPL_OPENGL_ES2)
Expand All @@ -25,13 +25,14 @@
#endif

// c headers
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <string>
#include <filesystem>
#include "communicator_api.h"
#include <fstream>
#include <sstream>
#include <chrono>
#include "Communicator.hpp"

// ui components
#include "RTCustWindow.h"
Expand Down Expand Up @@ -313,6 +314,7 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)
{
m_communicator.sendToServer(messageToJsonString(xlpmg::SHUTDOWN_SERVER_MESSAGE));
}

if (ImGui::Button("Recompile"))
{
xlpmg::Message recompileMsg = xlpmg::RECOMPILE_MESSAGE;
Expand All @@ -324,10 +326,38 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)
recompileMsg.args = compileArgs;
m_communicator.sendToServer(messageToJsonString(recompileMsg));
}

if (ImGui::Button("get height data"))
{
m_communicator.sendToServer(messageToJsonString(xlpmg::GET_HEIGHT_DATA_MESSAGE));
bool l_finished = false;
std::string heightArrayString = "";
while (!l_finished)
{
std::string data = m_communicator.receiveFromServer();
if (json::accept(data))
{
xlpmg::Message msg = xlpmg::jsonToMessage(json::parse(data));
if (msg.key == xlpmg::BUFFERED_SEND_FINISHED.key)
{
l_finished = true;
}
else
{
heightArrayString += msg.args;
}
}
}
}
if (ImGui::Button("Get time step"))
{
m_communicator.sendToServer(messageToJsonString(xlpmg::GET_TIMESTEP_MESSAGE));
std::cout << m_communicator.receiveFromServer() << std::endl;
std::string response = m_communicator.receiveFromServer();
if (json::accept(response))
{
xlpmg::Message responseMessage = xlpmg::jsonToMessage(json::parse(response));
std::cout << responseMessage.args << std::endl;
}
}

// if (ImGui::Button("file io true"))
Expand Down Expand Up @@ -368,7 +398,6 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)
// getHeightsArraySizeMsg[xlpmg::MESSAGE_TYPE] = xlpmg::FUNCTION_CALL;
// getHeightsArraySizeMsg[xlpmg::KEY] = xlpmg::KEY_GET_HEIGHTS;


// json getHeightsMsg;
// getHeightsMsg[xlpmg::MESSAGE_TYPE] = xlpmg::FUNCTION_CALL;
// getHeightsMsg[xlpmg::KEY] = xlpmg::KEY_GET_HEIGHTS;
Expand All @@ -381,7 +410,7 @@ int tsunami_lab::ui::GUI::launch(int i_PORT)
// if(parsedData.at(xlpmg::KEY) ==xlpmg::KEY_END_OF_HEIGHTS){
// l_finishedReading = true;
// }else{

// }
// }
// }
Expand Down

0 comments on commit 881dd63

Please sign in to comment.