Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm to github #67

Merged
merged 11 commits into from
May 2, 2022
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
2 changes: 1 addition & 1 deletion 3rd_party/simbody
Submodule simbody updated 0 files
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,5 @@ if(ACTIVATE_CUDA)
add_subdirectory(cases_test_gpu)
endif()

if(NOT SPH_ONLY_STATIC_BUILD)
add_subdirectory(modules)
endif()
add_subdirectory(modules)

2 changes: 1 addition & 1 deletion SPHINXsys/src/shared/io_system/in_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace SPH
stream << "</VTKFile>\n";
}
//=============================================================================================//
const BodyStatesRecordingToVtpString::VtuStringData& BodyStatesRecordingToVtpString::GetVtuData() const
const VtuStringData& BodyStatesRecordingToVtpString::GetVtuData() const
{
return _vtuData;
}
Expand Down
7 changes: 5 additions & 2 deletions SPHINXsys/src/shared/io_system/in_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace fs = boost::filesystem;
namespace fs = std::experimental::filesystem;
#endif

using VtuStringData = std::map<std::string, std::string>;

namespace SPH
{

Expand Down Expand Up @@ -297,9 +299,10 @@ namespace SPH
: BodyStatesRecording(in_output, bodies) {};
virtual ~BodyStatesRecordingToVtpString() = default;

using VtuStringData = std::map<std::string, std::string>;

const VtuStringData& GetVtuData() const;
void clear() {
_vtuData.clear();
}
protected:
virtual void writeWithFileName(const std::string& sequence) override;
virtual void writeVtu(std::ostream& stream, SPHBody* body) const;
Expand Down
223 changes: 141 additions & 82 deletions modules/structural_simulation/structural_simulation_class.cpp

Large diffs are not rendered by default.

92 changes: 57 additions & 35 deletions modules/structural_simulation/structural_simulation_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,34 @@ using PositionScaleSolidBodyTuple = tuple<int, Real, Real, Real>;
using TranslateSolidBodyTuple = tuple<int, Real, Real, Vec3d>;
using TranslateSolidBodyPartTuple = tuple<int, Real, Real, Vec3d, BoundingBox>;

#ifdef __EMSCRIPTEN__
struct StlData
{
string name;
uintptr_t ptr;
};

using StlList = vector<StlData>;
#else
using StlList = vector<string>;
#endif

class BodyPartFromMesh : public BodyRegionByParticle
{
public:
BodyPartFromMesh(SPHBody &body, const string &body_part_name, TriangleMeshShape &triangle_mesh_shape);
~BodyPartFromMesh(){};
};

class SolidBodyFromMesh : public SolidBody
{
public:
SolidBodyFromMesh(SPHSystem &system, const string &body_name, TriangleMeshShape &triangle_mesh_shape,
SharedPtr<SPHAdaptation> particle_adaptation, StdLargeVec<Vecd> &pos_0, StdLargeVec<Real> &volume);
~SolidBodyFromMesh(){};
SolidBodyFromMesh(SPHSystem &system, string body_name, TriangleMeshShape& triangle_mesh_shape, shared_ptr<SPHAdaptation> particle_adaptation);
};

class SolidBodyForSimulation
{
private:
SolidBodyFromMesh solid_body_from_mesh_;
//LinearElasticSolid material_model_;
ElasticSolidParticles elastic_solid_particles_;
BodyRelationInner inner_body_relation_;

Expand All @@ -66,19 +74,23 @@ class SolidBodyForSimulation
DampingWithRandomChoice<DampingPairwiseInner<Vec3d>> damping_random_;

public:
// no particle reload --> direct generator
SolidBodyForSimulation(
SPHSystem &system, string body_name, TriangleMeshShape& triangle_mesh_shape, shared_ptr<SPHAdaptation> particle_adaptation,
Real physical_viscosity, shared_ptr<LinearElasticSolid> material_model, StdLargeVec<Vecd>& pos_0, StdLargeVec<Real>& volume);
// particle reload
SolidBodyForSimulation(
SPHSystem &system, const string &body_name, TriangleMeshShape &triangle_mesh_shape, SharedPtr<SPHAdaptation> particle_adaptation,
Real physical_viscosity, SharedPtr<LinearElasticSolid> material_model, StdLargeVec<Vecd> &pos_0, StdLargeVec<Real> &volume);
~SolidBodyForSimulation(){};

SolidBodyFromMesh *getSolidBodyFromMesh() { return &solid_body_from_mesh_; };
ElasticSolidParticles *getElasticSolidParticles() { return &elastic_solid_particles_; };
BodyRelationInner *getInnerBodyRelation() { return &inner_body_relation_; };

solid_dynamics::CorrectConfiguration *getCorrectConfiguration() { return &correct_configuration_; };
solid_dynamics::StressRelaxationFirstHalf *getStressRelaxationFirstHalf() { return &stress_relaxation_first_half_; };
solid_dynamics::StressRelaxationSecondHalf *getStressRelaxationSecondHalf() { return &stress_relaxation_second_half_; };
DampingWithRandomChoice<DampingPairwiseInner<Vec3d>> *getDampingWithRandomChoice() { return &damping_random_; };
SPHSystem &system, string body_name, TriangleMeshShape& triangle_mesh_shape, shared_ptr<SPHAdaptation> particle_adaptation,
Real physical_viscosity, shared_ptr<LinearElasticSolid> material_model);

SolidBodyFromMesh* getSolidBodyFromMesh() { return &solid_body_from_mesh_; };
ElasticSolidParticles* getElasticSolidParticles() { return &elastic_solid_particles_; };
BodyRelationInner* getInnerBodyRelation() { return &inner_body_relation_; };

solid_dynamics::CorrectConfiguration* getCorrectConfiguration() { return &correct_configuration_; };
solid_dynamics::StressRelaxationFirstHalf* getStressRelaxationFirstHalf() { return &stress_relaxation_first_half_; };
solid_dynamics::StressRelaxationSecondHalf* getStressRelaxationSecondHalf() { return &stress_relaxation_second_half_; };
DampingWithRandomChoice<DampingPairwiseInner<Vec3d>>* getDampingWithRandomChoice() { return &damping_random_; };
};

void expandBoundingBox(BoundingBox *original, BoundingBox *additional);
Expand All @@ -103,7 +115,7 @@ class StructuralSimulationInput
{
public:
string relative_input_path_;
vector<string> imported_stl_list_;
StlList imported_stl_list_;
Real scale_stl_;
vector<Vec3d> translation_list_;
vector<Real> resolution_list_;
Expand Down Expand Up @@ -131,28 +143,23 @@ class StructuralSimulationInput
vector<TranslateSolidBodyPartTuple> translation_solid_body_part_tuple_;

StructuralSimulationInput(
const string &relative_input_path,
const vector<string> &imported_stl_list,
string relative_input_path,
StlList imported_stl_list,
Real scale_stl,
const vector<Vec3d> &translation_list,
const vector<Real> &resolution_list,
const vector<SharedPtr<LinearElasticSolid>> &material_model_list,
const StdVec<Real> &physical_viscosity,
const StdVec<IndexVector> &contacting_bodies_list);
vector<Vec3d> translation_list,
vector<Real> resolution_list,
vector<shared_ptr<LinearElasticSolid>> material_model_list,
StdVec<Real> physical_viscosity,
StdVec<IndexVector> contacting_bodies_list
);
};

class StructuralSimulation
{
private:
UniquePtrVectorKeeper<SolidBodyRelationContact> contact_relation_ptr_keeper_;
UniquePtrVectorKeeper<Gravity> gravity_ptr_keeper_;
UniquePtrVectorKeeper<TriangleMeshShape> tri_mesh_shape_ptr_keeper_;
UniquePtrVectorKeeper<BodyPartFromMesh> body_part_tri_mesh_ptr_keeper_;

protected:
// mandatory input
string relative_input_path_;
vector<string> imported_stl_list_;
StlList imported_stl_list_;
Real scale_stl_;
vector<Vec3d> translation_list_;
vector<Real> resolution_list_;
Expand All @@ -169,8 +176,8 @@ class StructuralSimulation
Real scale_system_boundaries_;
In_Output in_output_;

vector<TriangleMeshShape*> body_mesh_list_;
vector<SharedPtr<SPHAdaptation>> particle_adaptation_list_;
vector<shared_ptr<TriangleMeshShape>> body_mesh_list_;
vector<shared_ptr<SPHAdaptation>> particle_adaptation_list_;
vector<shared_ptr<SolidBodyForSimulation>> solid_body_list_;
vector<shared_ptr<solid_dynamics::UpdateElasticNormalDirection>> particle_normal_update_;

Expand Down Expand Up @@ -277,7 +284,7 @@ class StructuralSimulation
void runSimulationStep(Real &dt, Real &integration_time);

public:
explicit StructuralSimulation(StructuralSimulationInput &input);
explicit StructuralSimulation(const StructuralSimulationInput &input);
~StructuralSimulation();

StdVec<shared_ptr<SolidBodyForSimulation>> get_solid_body_list_() { return solid_body_list_; };
Expand All @@ -290,4 +297,19 @@ class StructuralSimulation
double runSimulationFixedDurationJS(int number_of_steps);
};

class StructuralSimulationJS : public StructuralSimulation
{
public:
StructuralSimulationJS(const StructuralSimulationInput& input);
~StructuralSimulationJS() = default;

void runSimulationFixedDuration(int number_of_steps);

VtuStringData getVtuData();

private:
BodyStatesRecordingToVtpString write_states_;
Real dt;
};

#endif //SOLID_STRUCTURAL_SIMULATION_CLASS_H
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ endif()

if(BUILD_3D_EXAMPLES)
ADD_SUBDIRECTORY(3d_examples)
ADD_SUBDIRECTORY(webassembly_models)
endif()
5 changes: 5 additions & 0 deletions tests/webassembly_models/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})

FOREACH(subdir ${SUBDIRS})
ADD_SUBDIRECTORY(${subdir})
ENDFOREACH()
33 changes: 33 additions & 0 deletions tests/webassembly_models/bernoulli_beam/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${SPHINXSYS_PROJECT_DIR}/cmake) # main (top) cmake dir
set(CMAKE_VERBOSE_MAKEFILE on)

STRING( REGEX REPLACE ".*/(.*)" "\\1" CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} )
PROJECT("${CURRENT_FOLDER}")

include(ImportSPHINXsysFromSource_for_3D_build)

set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin/")
set(BUILD_INPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/input")
set(BUILD_RELOAD_PATH "${EXECUTABLE_OUTPUT_PATH}/reload")

file(MAKE_DIRECTORY ${BUILD_INPUT_PATH})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_INPUT_PATH})

set(FILES_STL "bernoulli_beam_20x.stl")
foreach(STL_FILE ${FILES_STL})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input/${STL_FILE} DESTINATION ${BUILD_INPUT_PATH})
endforeach()

aux_source_directory(. DIR_SRCS)
ADD_EXECUTABLE(${PROJECT_NAME} ${EXECUTABLE_OUTPUT_PATH} ${DIR_SRCS})
target_link_directories(${PROJECT_NAME} PRIVATE ${TBBLIBDIR})
target_link_libraries(${PROJECT_NAME} structural_simulation_module ${TBBLIB})

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/SPHinXsys/modules/structural_simulation ${SPHINXSYS_PROJECT_DIR}/3rd_party/eigen)

if(NOT EMSCRIPTEN)
add_test(NAME ${PROJECT_NAME}
COMMAND ${PROJECT_NAME} --r=true
WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
endif()
89 changes: 89 additions & 0 deletions tests/webassembly_models/bernoulli_beam/bernoulli_beam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @file total_artificial_heart.cpp
* @brief This is the example of total artificial heart implantation path simulation
* @author John Benjamin, Bence Rochlitz - Virtonomy GmbH
*/
#include "bernoulli_beam.h"

#ifdef __EMSCRIPTEN__

#include <emscripten.h>
#include <emscripten/bind.h>

EMSCRIPTEN_BINDINGS(SPHINXSYS)
{
emscripten::value_array<std::array<double, 3>>("ArrayDouble3")
.element(emscripten::index<0>())
.element(emscripten::index<1>())
.element(emscripten::index<2>());

emscripten::value_object<StlData>("StlData")
.field("name", &StlData::name)
.field("ptr", &StlData::ptr);

emscripten::register_vector<std::string>("StringVector");
emscripten::register_vector<double>("DoubleVector");
emscripten::register_vector<IndexVector::value_type>("UIntVector");
emscripten::register_vector<StlData>("StlsList");
emscripten::register_vector<IndexVector>("BodiesList");
emscripten::register_map<std::string, std::string>("VtuData");

emscripten::value_object<BernoulliBeamInput>("BernoulliBeamInput")
.field("scale_stl", &BernoulliBeamInput::scale_stl)
.field("resolution", &BernoulliBeamInput::resolution)
.field("rho_0", &BernoulliBeamInput::rho_0)
.field("poisson", &BernoulliBeamInput::poisson)
.field("Youngs_modulus", &BernoulliBeamInput::Youngs_modulus)
.field("physical_viscosity", &BernoulliBeamInput::physical_viscosity)
.field("translation", &BernoulliBeamInput::translation)
.field("stls", &BernoulliBeamInput::stls)
.field("relative_input_path", &BernoulliBeamInput::relative_input_path);

emscripten::class_<BernoulliBeamJS>("BernoulliBeam")
.constructor<BernoulliBeamInput>()
.function("runSimulation", &BernoulliBeamJS::runSimulation)
.function("onError", &BernoulliBeamJS::onError)
.property("vtuData", &BernoulliBeamJS::getVtuData);
}

#else

int main()
{
BernoulliBeamInput input;
input.scale_stl = 0.001;
input.resolution = { 1.5 };
input.rho_0 = 1e3;
input.poisson = 0.3;
input.Youngs_modulus = 5e8;
input.physical_viscosity = 5e6;
input.translation = { 0, 0, 0 };

string beam_stl = "bernoulli_beam_20x.stl";

input.stls = { beam_stl };
input.relative_input_path = "./input/";

/* DOWNLOAD STLs files at this point */
#ifdef __EMSCRIPTEN__
try
{
// set up the simulation
BernoulliBeamJS bernoulli_beam(input);
int number_of_steps = 700;
std::cout << "About to run the simulation" << std::endl;
bernoulli_beam.runSimulation(number_of_steps);
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return 1;
}
#else
BernoulliBeam bernoulli_beam(input);
bernoulli_beam.runCompleteSimulation(0.1);
#endif //__EMSCRIPTEN__
return 0;
}

#endif
Loading