diff --git a/Core/Samples/inc/MaterialManager.h b/Core/Samples/inc/MaterialManager.h index acaa4670175534a439eee8ac2bb3944b5ab8797b..d09e412f7293172482c5542c0b37b4582f149c70 100644 --- a/Core/Samples/inc/MaterialManager.h +++ b/Core/Samples/inc/MaterialManager.h @@ -26,7 +26,7 @@ //! Manager of materials used in simulation. //! //! It is a singleton which provides common and unique interface for -//! material creation and access. No thread safety. +//! material creation and access. class MaterialManager: public ISingleton<MaterialManager> { @@ -36,15 +36,15 @@ public: //! definition of materials container typedef std::map<std::string, IMaterial *> materials_t; - //! return material from database + //! return material from container static const IMaterial *getMaterial(const std::string &name) { return instance().this_getMaterial(name); } - //! add material to the database + //! add material to the container static const IMaterial *getHomogeneousMaterial(const std::string &name, const complex_t &refractive_index) { return instance().this_getHomogeneousMaterial(name, refractive_index); } - //! add material to the database + //! add material to the container static const IMaterial *getHomogeneousMaterial(const std::string &name, double refractive_index_real, double refractive_index_imag) { return instance().this_getHomogeneousMaterial(name, refractive_index_real, refractive_index_imag); } diff --git a/Core/Samples/src/MaterialManager.cpp b/Core/Samples/src/MaterialManager.cpp index ebc606fe47c8e5d50e2839101a67d1e4cb4213ec..f79cb7d31e0cdf6d1f60e9c254d67d5ff149f756 100644 --- a/Core/Samples/src/MaterialManager.cpp +++ b/Core/Samples/src/MaterialManager.cpp @@ -16,10 +16,13 @@ #include "MaterialManager.h" #include "Exceptions.h" #include "MessageSvc.h" +#include <boost/thread.hpp> // clean material database void MaterialManager::clear() { + static boost::mutex single_mutex; + boost::unique_lock<boost::mutex> single_lock( single_mutex ); for(materials_t::iterator it = m_materials.begin(); it!= m_materials.end(); ++it) { if( (*it).second ) delete (*it).second; } @@ -29,6 +32,8 @@ void MaterialManager::clear() { // get material const IMaterial *MaterialManager::this_getMaterial(const std::string &name) { + static boost::mutex single_mutex; + boost::unique_lock<boost::mutex> single_lock( single_mutex ); materials_t::const_iterator pos = m_materials.find(name); if( pos != m_materials.end()) { return pos->second; @@ -41,6 +46,8 @@ const IMaterial *MaterialManager::this_getMaterial(const std::string &name) // Create material and add into database using name of material as indentifier. const IMaterial *MaterialManager::this_getHomogeneousMaterial(const std::string &name, const complex_t &refractive_index) { + static boost::mutex single_mutex; + boost::unique_lock<boost::mutex> single_lock( single_mutex ); const IMaterial *mat = getMaterial(name); if( mat ) { // check if user is trying to create material with same name but different parameters diff --git a/Core/Tools/inc/ISingleton.h b/Core/Tools/inc/ISingleton.h index a130d6c82bfaaa93358698d17707d7cda2c4d528..ff1b556a8f01a850d077403b8fb4690e691a74a6 100644 --- a/Core/Tools/inc/ISingleton.h +++ b/Core/Tools/inc/ISingleton.h @@ -19,7 +19,7 @@ #include <stdexcept> #include <iostream> #include <typeinfo> -//#include <boost/thread.hpp> +#include <boost/thread.hpp> template <class T> class ISingleton @@ -28,8 +28,8 @@ public: static T &instance() { -// static boost::mutex single_mutex; -// boost::unique_lock<boost::mutex> single_lock( single_mutex ); + static boost::mutex single_mutex; + boost::unique_lock<boost::mutex> single_lock( single_mutex ); if( !m_instance) { if( m_destroyed ) { onDeadReference(); diff --git a/GUI/coregui/Views/Components/SampleEditor/SampleFactory.h b/GUI/coregui/Views/Components/SampleEditor/SampleFactory.h index 4700d3912df3bd1c0ad9d41a236105463038c05d..864aa36023153062cf930e96ea844797f64c8038 100644 --- a/GUI/coregui/Views/Components/SampleEditor/SampleFactory.h +++ b/GUI/coregui/Views/Components/SampleEditor/SampleFactory.h @@ -25,8 +25,8 @@ public: complex_t n_air(1.0, 0.0); complex_t n_substrate(1.0-6e-6, 2e-8); complex_t n_particle(1.0-6e-4, 2e-8); - const IMaterial *p_air_material = MaterialManager::instance().addHomogeneousMaterial("Air", n_air); - const IMaterial *p_substrate_material = MaterialManager::instance().addHomogeneousMaterial("Substrate", n_substrate); + const IMaterial *p_air_material = MaterialManager::getHomogeneousMaterial("Air", n_air); + const IMaterial *p_substrate_material = MaterialManager::getHomogeneousMaterial("Substrate", n_substrate); Layer air_layer; air_layer.setMaterial(p_air_material); Layer substrate_layer; diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs01.py b/Tests/FunctionalTests/TestPyCore/isgisaxs01.py index 7330ef1a18afea0227a7630ae8a2a82b6b78cc41..d7165c0cc61874b0cc72ad3a6653c80b788674c6 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs01.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs01.py @@ -16,9 +16,8 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs02.py b/Tests/FunctionalTests/TestPyCore/isgisaxs02.py index 3d2c7e180cc92d7399bf4c14226c142b0fddc1fc..2926f8a0f87fc05d99cc242add9f1db88e3fd513 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs02.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs02.py @@ -16,8 +16,7 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) radius1 = 5.0*nanometer diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs03.py b/Tests/FunctionalTests/TestPyCore/isgisaxs03.py index 9da3180f5c46ac15870ad906a0f308ecbf8c4059..24b29a51651d41b1becf8eb2a191c114161e4572 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs03.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs03.py @@ -16,9 +16,8 @@ from libBornAgainCore import * def RunSimulationDWBA(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) @@ -51,9 +50,8 @@ def RunSimulationDWBA(): # ---------------------------------- def RunSimulationBA(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) @@ -83,9 +81,8 @@ def RunSimulationBA(): # ---------------------------------- def RunSimulationBA_Size(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) multi_layer = MultiLayer() diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs04.py b/Tests/FunctionalTests/TestPyCore/isgisaxs04.py index e27d05b787e6ad2c9feff5e5b75fdf96894de96f..4b7a38087505b4d88cbab70c94b16355cfb3a463 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs04.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs04.py @@ -15,9 +15,8 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation1(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) @@ -49,9 +48,8 @@ def RunSimulation1(): # ---------------------------------- def RunSimulation2(): # # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs06.py b/Tests/FunctionalTests/TestPyCore/isgisaxs06.py index c732716393cbf0ed97d47ba3b972ffb5f2d555e0..e2e4f037449fbb8e51e0da628c48851edf33c47c 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs06.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs06.py @@ -17,9 +17,8 @@ M_PI = numpy.pi # ---------------------------------- def RunSimulation_lattice(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles lattice_params = Lattice2DIFParameters() lattice_params.m_length_1 = 10.0*nanometer @@ -71,9 +70,8 @@ def RunSimulation_lattice(): # ---------------------------------- def RunSimulation_centered(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles lattice_params = Lattice2DIFParameters() lattice_params.m_length_1 = 10.0*nanometer @@ -130,9 +128,8 @@ def RunSimulation_centered(): # ---------------------------------- def RunSimulation_rotated(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles lattice_params = Lattice2DIFParameters() lattice_params.m_length_1 = 10.0*nanometer @@ -219,9 +216,8 @@ def RunSimulation_variants(): # IsGISAXS6 functional test sample builder for varying xi angle def buildSample(xi_value): n_particle = complex(1.0-6e-4, 2e-8) - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) air_layer = Layer(mAmbience) substrate_layer = Layer(mSubstrate) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs07.py b/Tests/FunctionalTests/TestPyCore/isgisaxs07.py index 10091a66f3f23739d08d4dc26ee200aaad35d7bb..92f27abb483f70f1c4bebce60f1cabe3c9a22d8b 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs07.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs07.py @@ -16,8 +16,7 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) particle_decoration = ParticleDecoration() diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs08.py b/Tests/FunctionalTests/TestPyCore/isgisaxs08.py index cb8274ee1d89e1ed86d678bb76137dd96a1c7321..9a0c71a9fbd1759e992867c83e7502bb50239afc 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs08.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs08.py @@ -16,9 +16,8 @@ M_PI = numpy.pi # ---------------------------------- def RunSimulation1(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) @@ -55,9 +54,8 @@ def RunSimulation1(): # ---------------------------------- def RunSimulation2(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs09.py b/Tests/FunctionalTests/TestPyCore/isgisaxs09.py index 80b78fa5283c32223b10870904c0e666398d9396..010534e2010613fbe317a3eccf81fef2079e3219 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs09.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs09.py @@ -15,9 +15,8 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation1(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) pyramid_ff = FormFactorPyramid(5*nanometer, 5*nanometer, deg2rad(54.73 ) ) @@ -49,9 +48,8 @@ def RunSimulation1(): # ---------------------------------- def RunSimulation2(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8) pyramid_ff = FormFactorPyramid(5*nanometer, 5*nanometer, deg2rad(54.73 ) ) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs10.py b/Tests/FunctionalTests/TestPyCore/isgisaxs10.py index 630e64ed08cdd6a9ea5f49e0096221542d2ccbb2..81fcd94dfe683c89289fbacda2ecf9dcc971dbce 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs10.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs10.py @@ -16,9 +16,8 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) - mSubstrate = matMng.addHomogeneousMaterial("Substrate", 1.0-5e-6, 2e-8 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) + mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-5e-6, 2e-8 ) # collection of particles n_particle = complex(1.0-5e-5, 2e-8) cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs11.py b/Tests/FunctionalTests/TestPyCore/isgisaxs11.py index 1d4e4782a5472c35baca10bdf551ff31e202f818..629c5031c49f0bdf863e6955dce0a26d9188af69 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs11.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs11.py @@ -16,8 +16,7 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) # collection of particles n_particle_shell = complex(1.0-1e-4, 2e-8) diff --git a/Tests/FunctionalTests/TestPyCore/isgisaxs15.py b/Tests/FunctionalTests/TestPyCore/isgisaxs15.py index dc77f4639ce8d3918ae03659728a8dcaa4d83839..74816e7906535fc47c08160065502f6604437022 100644 --- a/Tests/FunctionalTests/TestPyCore/isgisaxs15.py +++ b/Tests/FunctionalTests/TestPyCore/isgisaxs15.py @@ -15,8 +15,7 @@ from libBornAgainCore import * # ---------------------------------- def RunSimulation(): # defining materials - matMng = MaterialManager.instance() - mAmbience = matMng.addHomogeneousMaterial("Air", 1.0, 0.0 ) + mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 ) # collection of particles n_particle = complex(1.0-6e-4, 2e-8)