diff --git a/CMakeLists.txt b/CMakeLists.txt index d00bc3c33..b9a569d6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,11 @@ if(SKBUILD) message(STATUS "The project is built using scikit-build") endif() +# it determines the size of the PATH up to librascal and +# is used to output relative path information in the code using __FILE__ +string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE) +add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}") + add_subdirectory(src) enable_testing() diff --git a/bindings/rascal/models/kernels.py b/bindings/rascal/models/kernels.py index 96801794d..732f623f4 100644 --- a/bindings/rascal/models/kernels.py +++ b/bindings/rascal/models/kernels.py @@ -9,9 +9,12 @@ from .sparse_points import SparsePoints import json +import logging import numpy as np +LOGGER = logging.getLogger(__name__) + class Kernel(BaseIO): @@ -109,7 +112,8 @@ def _set_data(self, data): super()._set_data(data) def _get_data(self): - return super()._get_data() + data = super()._get_data() + return data def __call__(self, X, Y=None, grad=(False, False), compute_neg_stress=False): """ diff --git a/bindings/rascal/models/krr.py b/bindings/rascal/models/krr.py index 6ec674554..12a9de84e 100644 --- a/bindings/rascal/models/krr.py +++ b/bindings/rascal/models/krr.py @@ -209,8 +209,13 @@ def predict_stress(self, managers, KNM=None): return -neg_stress - def get_weights(self): - return self.weights + @property + def weights(self): + return self._weights + + @weights.setter + def weights(self, weights): + self._weights = weights.reshape(-1) def _get_init_params(self): init_params = dict( diff --git a/bindings/rascal/representations/spherical_covariants.py b/bindings/rascal/representations/spherical_covariants.py index 57edbab04..045d27dc0 100644 --- a/bindings/rascal/representations/spherical_covariants.py +++ b/bindings/rascal/representations/spherical_covariants.py @@ -151,17 +151,18 @@ def __init__( cutoff_smooth_width, max_radial, max_angular, - gaussian_sigma_type, + gaussian_sigma_type="Constant", gaussian_sigma_constant=0.3, cutoff_function_type="ShiftedCosine", - normalize=True, - radial_basis="GTO", - optimization=None, - optimization_args=None, soap_type="LambdaSpectrum", inversion_symmetry=True, + radial_basis="GTO", + normalize=True, covariant_lambda=0, - cutoff_function_parameters=dict(), + optimization=None, + optimization_args=None, + global_species=None, + cutoff_function_parameters=None, ): """Construct a SphericalExpansion representation @@ -169,52 +170,51 @@ def __init__( class documentation """ self.name = "sphericalcovariants" - self.hypers = dict() - self.update_hyperparameters( + optimization = check_optimization_for_spherical_representations( + optimization, optimization_args + ) + + if global_species is None: + global_species = [] + elif isinstance(global_species, int): + global_species = list(global_species) + elif not (isinstance(global_species, list)): + raise ValueError( + "'global_species' should be None, an integer, an empty list or a list of atomic numbers" + ) + + if cutoff_function_parameters is None: + cutoff_function_parameters = dict() + elif not isinstance(cutoff_function_parameters, dict): + raise ValueError( + "'cutoff_function_parameters' should be None or a dictionary with 'rate', 'scale' and 'expontent'" + ) + + self.hypers = dict( + interaction_cutoff=interaction_cutoff, + cutoff_smooth_width=cutoff_smooth_width, max_radial=max_radial, max_angular=max_angular, + gaussian_sigma_type=gaussian_sigma_type, + gaussian_sigma_constant=gaussian_sigma_constant, + cutoff_function_type=cutoff_function_type, soap_type=soap_type, - normalize=normalize, inversion_symmetry=inversion_symmetry, + radial_basis=radial_basis, + normalize=normalize, covariant_lambda=covariant_lambda, - ) - - self.cutoff_function_parameters = deepcopy(cutoff_function_parameters) - - cutoff_function_parameters.update( - interaction_cutoff=interaction_cutoff, - cutoff_smooth_width=cutoff_smooth_width, - ) - cutoff_function = cutoff_function_dict_switch( - cutoff_function_type, **cutoff_function_parameters - ) - - gaussian_density = dict( - type=gaussian_sigma_type, - gaussian_sigma=dict(value=gaussian_sigma_constant, unit="AA"), - ) - optimization = check_optimization_for_spherical_representations( - optimization, optimization_args - ) - - radial_contribution = dict(type=radial_basis, optimization=optimization) - radial_contribution = dict( - type=radial_basis, - ) - - self.update_hyperparameters( - cutoff_function=cutoff_function, - gaussian_density=gaussian_density, - radial_contribution=radial_contribution, + optimization=optimization, + optimization_args=optimization_args, + global_species=global_species, + cutoff_function_parameters=cutoff_function_parameters, ) self.nl_options = [ - dict(name="centers", args=dict()), + dict(name="centers", args=[]), dict(name="neighbourlist", args=dict(cutoff=interaction_cutoff)), dict(name="centercontribution", args=dict()), dict(name="strict", args=dict(cutoff=interaction_cutoff)), ] - self.rep_options = dict(name=self.name, args=[self.hypers]) self._representation = CalculatorFactory(self.rep_options) @@ -232,18 +232,19 @@ def update_hyperparameters(self, **hypers): "max_angular", "gaussian_sigma_type", "gaussian_sigma_constant", + "cutoff_function_type", "soap_type", "inversion_symmetry", - "covariant_lambda", - "cutoff_function", + "radial_basis", "normalize", - "gaussian_density", - "radial_contribution", + "covariant_lambda", + "optimization", + "global_species", "cutoff_function_parameters", } + hypers_clean = {key: hypers[key] for key in hypers if key in allowed_keys} self.hypers.update(hypers_clean) - return def transform(self, frames): """Compute the representation. @@ -353,27 +354,7 @@ def get_keys(self, species): return keys def _get_init_params(self): - gaussian_density = self.hypers["gaussian_density"] - cutoff_function = self.hypers["cutoff_function"] - radial_contribution = self.hypers["radial_contribution"] - - init_params = dict( - interaction_cutoff=cutoff_function["cutoff"]["value"], - cutoff_smooth_width=cutoff_function["smooth_width"]["value"], - max_radial=self.hypers["max_radial"], - max_angular=self.hypers["max_angular"], - soap_type=self.hypers["soap_type"], - inversion_symmetry=self.hypers["inversion_symmetry"], - normalize=self.hypers["normalize"], - gaussian_sigma_type=gaussian_density["type"], - gaussian_sigma_constant=gaussian_density["gaussian_sigma"]["value"], - lam=self.hypers["covariant_lambda"], - cutoff_function_type=cutoff_function["type"], - radial_basis=radial_contribution["type"], - optimization=radial_contribution["optimization"], - cutoff_function_parameters=self.cutoff_function_parameters, - ) - return init_params + return self.hypers def _set_data(self, data): super()._set_data(data) diff --git a/bindings/rascal/representations/spherical_expansion.py b/bindings/rascal/representations/spherical_expansion.py index 305ad3165..168ec8dd1 100644 --- a/bindings/rascal/representations/spherical_expansion.py +++ b/bindings/rascal/representations/spherical_expansion.py @@ -170,16 +170,16 @@ def __init__( cutoff_smooth_width, max_radial, max_angular, - gaussian_sigma_type, + gaussian_sigma_type="Constant", gaussian_sigma_constant=0.3, cutoff_function_type="ShiftedCosine", radial_basis="GTO", + compute_gradients=False, + expansion_by_species_method="environment wise", optimization=None, optimization_args=None, - expansion_by_species_method="environment wise", global_species=None, - compute_gradients=False, - cutoff_function_parameters=dict(), + cutoff_function_parameters=None, ): """Construct a SphericalExpansion representation @@ -188,56 +188,52 @@ class documentation """ self.name = "sphericalexpansion" - self.hypers = dict() + + optimization = check_optimization_for_spherical_representations( + optimization, optimization_args + ) if global_species is None: global_species = [] - elif not isinstance(global_species, list): + elif isinstance(global_species, int): global_species = list(global_species) - - self.update_hyperparameters( + elif not (isinstance(global_species, list)): + raise ValueError( + "'global_species' should be None, an integer, an empty list or a list of atomic numbers" + ) + + if cutoff_function_parameters is None: + cutoff_function_parameters = dict() + elif not isinstance(cutoff_function_parameters, dict): + raise ValueError( + "'cutoff_function_parameters' should be None or a dictionary with 'rate', 'scale' and 'expontent'" + ) + + self.hypers = dict( + interaction_cutoff=interaction_cutoff, + cutoff_smooth_width=cutoff_smooth_width, max_radial=max_radial, max_angular=max_angular, + gaussian_sigma_type=gaussian_sigma_type, + gaussian_sigma_constant=gaussian_sigma_constant, + cutoff_function_type=cutoff_function_type, + radial_basis=radial_basis, + compute_gradients=compute_gradients, expansion_by_species_method=expansion_by_species_method, + optimization=optimization, + optimization_args=optimization_args, global_species=global_species, - compute_gradients=compute_gradients, - ) - self.cutoff_function_parameters = deepcopy(cutoff_function_parameters) - cutoff_function_parameters.update( - interaction_cutoff=interaction_cutoff, - cutoff_smooth_width=cutoff_smooth_width, - ) - cutoff_function = cutoff_function_dict_switch( - cutoff_function_type, **cutoff_function_parameters - ) - - gaussian_density = dict( - type=gaussian_sigma_type, - gaussian_sigma=dict(value=gaussian_sigma_constant, unit="A"), - ) - optimization = check_optimization_for_spherical_representations( - optimization, optimization_args - ) - - radial_contribution = dict(type=radial_basis, optimization=optimization) - - self.update_hyperparameters( - cutoff_function=cutoff_function, - gaussian_density=gaussian_density, - radial_contribution=radial_contribution, + cutoff_function_parameters=cutoff_function_parameters, ) self.nl_options = [ - dict(name="centers", args=dict()), + dict(name="centers", args=[]), dict(name="neighbourlist", args=dict(cutoff=interaction_cutoff)), dict(name="centercontribution", args=dict()), dict(name="strict", args=dict(cutoff=interaction_cutoff)), ] - self.rep_options = dict(name=self.name, args=[self.hypers]) - n_features = self.get_num_coefficients() - self._representation = CalculatorFactory(self.rep_options) def update_hyperparameters(self, **hypers): @@ -253,13 +249,14 @@ def update_hyperparameters(self, **hypers): "max_angular", "gaussian_sigma_type", "gaussian_sigma_constant", - "gaussian_density", - "cutoff_function", - "radial_contribution", + "cutoff_function_type", + "radial_basis", "compute_gradients", + "optimization", "cutoff_function_parameters", "expansion_by_species_method", "global_species", + "compute_gradients", } hypers_clean = {key: hypers[key] for key in hypers if key in allowed_keys} self.hypers.update(hypers_clean) @@ -305,26 +302,7 @@ def get_keys(self, species): return keys def _get_init_params(self): - gaussian_density = self.hypers["gaussian_density"] - cutoff_function = self.hypers["cutoff_function"] - radial_contribution = self.hypers["radial_contribution"] - - init_params = dict( - interaction_cutoff=cutoff_function["cutoff"]["value"], - cutoff_smooth_width=cutoff_function["smooth_width"]["value"], - max_radial=self.hypers["max_radial"], - max_angular=self.hypers["max_angular"], - expansion_by_species_method=self.hypers["expansion_by_species_method"], - global_species=self.hypers["global_species"], - compute_gradients=self.hypers["compute_gradients"], - gaussian_sigma_type=gaussian_density["type"], - gaussian_sigma_constant=gaussian_density["gaussian_sigma"]["value"], - cutoff_function_type=cutoff_function["type"], - radial_basis=radial_contribution["type"], - optimization=radial_contribution["optimization"], - cutoff_function_parameters=self.cutoff_function_parameters, - ) - return init_params + return self.hypers def _set_data(self, data): super()._set_data(data) diff --git a/bindings/rascal/representations/spherical_invariants.py b/bindings/rascal/representations/spherical_invariants.py index b0e785e76..744d994d1 100644 --- a/bindings/rascal/representations/spherical_invariants.py +++ b/bindings/rascal/representations/spherical_invariants.py @@ -211,19 +211,19 @@ def __init__( cutoff_smooth_width, max_radial, max_angular, - gaussian_sigma_type, + gaussian_sigma_type="Constant", gaussian_sigma_constant=0.3, cutoff_function_type="ShiftedCosine", soap_type="PowerSpectrum", inversion_symmetry=True, radial_basis="GTO", normalize=True, + compute_gradients=False, + expansion_by_species_method="environment wise", optimization=None, optimization_args=None, - expansion_by_species_method="environment wise", global_species=None, - compute_gradients=False, - cutoff_function_parameters=dict(), + cutoff_function_parameters=None, coefficient_subselection=None, ): """Construct a SphericalExpansion representation @@ -232,62 +232,61 @@ def __init__( class documentation """ self.name = "sphericalinvariants" - self.hypers = dict() + + optimization = check_optimization_for_spherical_representations( + optimization, optimization_args + ) + if global_species is None: global_species = [] - elif not isinstance(global_species, list): + elif isinstance(global_species, int): global_species = list(global_species) + elif not (isinstance(global_species, list)): + raise ValueError( + "'global_species' should be None, an integer, an empty list or a list of atomic numbers" + ) - self.update_hyperparameters( + if cutoff_function_parameters is None: + cutoff_function_parameters = dict() + elif not isinstance(cutoff_function_parameters, dict): + raise ValueError( + "'cutoff_function_parameters' should be None or a dictionary with 'rate', 'scale' and 'expontent'" + ) + + if coefficient_subselection is None: + coefficient_subselection = dict() + elif not isinstance(coefficient_subselection, dict): + raise ValueError( + "'coefficient_subselection' should be None or a dictionary with 'a', 'b', 'n1', 'n2' and 'l'" + ) + + self.hypers = dict( + interaction_cutoff=interaction_cutoff, + cutoff_smooth_width=cutoff_smooth_width, max_radial=max_radial, max_angular=max_angular, + gaussian_sigma_type=gaussian_sigma_type, + gaussian_sigma_constant=gaussian_sigma_constant, + cutoff_function_type=cutoff_function_type, soap_type=soap_type, - normalize=normalize, inversion_symmetry=inversion_symmetry, + radial_basis=radial_basis, + normalize=normalize, + compute_gradients=compute_gradients, expansion_by_species_method=expansion_by_species_method, + optimization=optimization, + optimization_args=optimization_args, global_species=global_species, - compute_gradients=compute_gradients, + cutoff_function_parameters=cutoff_function_parameters, coefficient_subselection=coefficient_subselection, ) - if self.hypers["coefficient_subselection"] is None: - del self.hypers["coefficient_subselection"] - - self.cutoff_function_parameters = deepcopy(cutoff_function_parameters) - cutoff_function_parameters.update( - interaction_cutoff=interaction_cutoff, - cutoff_smooth_width=cutoff_smooth_width, - ) - cutoff_function = cutoff_function_dict_switch( - cutoff_function_type, **cutoff_function_parameters - ) - - gaussian_density = dict( - type=gaussian_sigma_type, - gaussian_sigma=dict(value=gaussian_sigma_constant, unit="AA"), - ) - optimization = check_optimization_for_spherical_representations( - optimization, optimization_args - ) - - radial_contribution = dict(type=radial_basis, optimization=optimization) - - self.update_hyperparameters( - cutoff_function=cutoff_function, - gaussian_density=gaussian_density, - radial_contribution=radial_contribution, - ) - - if soap_type == "RadialSpectrum": - self.update_hyperparameters(max_angular=0) - self.nl_options = [ dict(name="centers", args=[]), dict(name="neighbourlist", args=dict(cutoff=interaction_cutoff)), dict(name="centercontribution", args=dict()), dict(name="strict", args=dict(cutoff=interaction_cutoff)), ] - self.rep_options = dict(name=self.name, args=[self.hypers]) self._representation = CalculatorFactory(self.rep_options) @@ -301,26 +300,24 @@ def update_hyperparameters(self, **hypers): allowed_keys = { "interaction_cutoff", "cutoff_smooth_width", + "cutoff_function_type", "max_radial", "max_angular", + "radial_basis", + "optimization", "gaussian_sigma_type", "gaussian_sigma_constant", "soap_type", "inversion_symmetry", - "cutoff_function", "normalize", - "gaussian_density", - "radial_contribution", "cutoff_function_parameters", "expansion_by_species_method", - "compute_gradients", "global_species", + "compute_gradients", "coefficient_subselection", } hypers_clean = {key: hypers[key] for key in hypers if key in allowed_keys} - self.hypers.update(hypers_clean) - return def transform(self, frames): """Compute the representation. @@ -446,33 +443,7 @@ def get_feature_index_mapping(self, managers): return feature_index_mapping def _get_init_params(self): - gaussian_density = self.hypers["gaussian_density"] - cutoff_function = self.hypers["cutoff_function"] - radial_contribution = self.hypers["radial_contribution"] - - init_params = dict( - interaction_cutoff=cutoff_function["cutoff"]["value"], - cutoff_smooth_width=cutoff_function["smooth_width"]["value"], - max_radial=self.hypers["max_radial"], - max_angular=self.hypers["max_angular"], - soap_type=self.hypers["soap_type"], - inversion_symmetry=self.hypers["inversion_symmetry"], - normalize=self.hypers["normalize"], - expansion_by_species_method=self.hypers["expansion_by_species_method"], - global_species=self.hypers["global_species"], - compute_gradients=self.hypers["compute_gradients"], - gaussian_sigma_type=gaussian_density["type"], - gaussian_sigma_constant=gaussian_density["gaussian_sigma"]["value"], - cutoff_function_type=cutoff_function["type"], - radial_basis=radial_contribution["type"], - optimization=radial_contribution["optimization"], - cutoff_function_parameters=self.cutoff_function_parameters, - ) - if "coefficient_subselection" in self.hypers: - init_params["coefficient_subselection"] = self.hypers[ - "coefficient_subselection" - ] - return init_params + return self.hypers def _set_data(self, data): super()._set_data(data) diff --git a/performance/benchmarks/benchmark_interpolator.hh b/performance/benchmarks/benchmark_interpolator.hh index 8dc2678d9..de70ecfd3 100644 --- a/performance/benchmarks/benchmark_interpolator.hh +++ b/performance/benchmarks/benchmark_interpolator.hh @@ -447,13 +447,12 @@ namespace rascal { data, "radial_angular", state); this->max_radial = std::get<0>(radial_angular); this->max_angular = std::get<1>(radial_angular); - json fc_hypers{{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.5}, {"unit", "A"}}}}; - json hypers{ - {"gaussian_density", fc_hypers}, - {"max_radial", this->max_radial}, - {"max_angular", this->max_angular}, - {"cutoff_function", {{"cutoff", {{"value", 2.0}, {"unit", "A"}}}}}}; + + json hypers{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.5}, + {"max_radial", this->max_radial}, + {"max_angular", this->max_angular}, + {"interaction_cutoff", 2.0}}; // we cannot copy radial contribution to the lambda function, because // the copying has been disabled this->radial_contr = @@ -567,24 +566,21 @@ namespace rascal { {"compute_gradients", this->compute_gradients}, {"soap_type", "PowerSpectrum"}}; - json fc_hypers{{"type", "ShiftedCosine"}, - {"cutoff", {{"value", this->cutoff}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.}, {"unit", "AA"}}}}; - json sigma_hypers{{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}; + hypers["cutoff_function_type"] = "ShiftedCosine"; + hypers["interaction_cutoff"] = this->cutoff; + hypers["cutoff_smooth_width"] = 0.; - hypers["cutoff_function"] = fc_hypers; - hypers["gaussian_density"] = sigma_hypers; + hypers["gaussian_sigma_type"] = "Constant"; + hypers["gaussian_sigma_constant"] = 0.4; if (this->use_interpolator) { - hypers["radial_contribution"] = { - {"type", "GTO"}, - {"optimization", - {{"type", "Spline"}, - {"accuracy", this->error_bound}, - {"range", {{"begin", 0.}, {"end", cutoff}}}}}}; + hypers["radial_basis"] = "GTO"; + hypers["optimization"] = { + {{"Spline", + {{"accuracy", this->error_bound}, + {"range", {{"begin", 0.}, {"end", cutoff}}}}}}}; } else { - hypers["radial_contribution"] = {{"type", "GTO"}}; + hypers["radial_basis"] = "GTO"; } this->hypers = hypers; this->representation_ptr = std::make_shared(hypers); diff --git a/reference_data/tests_only/kernel_reference.ubjson b/reference_data/tests_only/kernel_reference.ubjson index d15163c7b..1caf41450 100644 Binary files a/reference_data/tests_only/kernel_reference.ubjson and b/reference_data/tests_only/kernel_reference.ubjson differ diff --git a/reference_data/tests_only/simple_gap_model.json b/reference_data/tests_only/simple_gap_model.json index 2dcb57db9..a9fe797b3 100644 --- a/reference_data/tests_only/simple_gap_model.json +++ b/reference_data/tests_only/simple_gap_model.json @@ -5237,3 +5237,4 @@ "module_name": "rascal.models.krr", "version": "0.1" } + diff --git a/reference_data/tests_only/sparse_kernel_inputs.json b/reference_data/tests_only/sparse_kernel_inputs.json index 1c937c236..a28798ec9 100644 --- a/reference_data/tests_only/sparse_kernel_inputs.json +++ b/reference_data/tests_only/sparse_kernel_inputs.json @@ -3,13 +3,13 @@ "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": true, "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.8, "unit": "AA"}}, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.8, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 5}, "name": "neighbourlist"}, @@ -26,13 +26,13 @@ "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": true, "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"}}, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 5}, "name": "neighbourlist"}, @@ -48,10 +48,13 @@ { "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": true, "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 3.5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"}}, "radial_contribution": {"type": "GTO", "optimization": {}} + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 3.5}, "name": "neighbourlist"}, {"initialization_arguments": {}, "name": "centercontribution"}, {"initialization_arguments": {"cutoff": 3.5}, "name": "strict"} @@ -66,13 +69,13 @@ "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": false, "expansion_by_species_method": "environment wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.6, "unit": "AA"}}, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.6, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 5}, "name": "neighbourlist"}, @@ -89,13 +92,13 @@ "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": false, "expansion_by_species_method": "environment wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"}}, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 5}, "name": "neighbourlist"}, @@ -111,10 +114,13 @@ { "calculator" : { "max_radial": 3, "max_angular": 3, "soap_type": "PowerSpectrum", "normalize": false, "expansion_by_species_method": "environment wise", "global_species": [], "compute_gradients": true, - "cutoff_function": - {"type": "ShiftedCosine", "cutoff": {"value": 3.5, "unit": "AA"}, "smooth_width": {"value": 0.5, "unit": "AA"}}, - "gaussian_density": - {"type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"}}, "radial_contribution": {"type": "GTO", "optimization": {}} + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ {"initialization_arguments": {"cutoff": 3.5}, "name": "neighbourlist"}, {"initialization_arguments": {}, "name": "centercontribution"}, {"initialization_arguments": {"cutoff": 3.5}, "name": "strict"} diff --git a/reference_data/tests_only/sparsification_inputs.json b/reference_data/tests_only/sparsification_inputs.json index ba24a38be..03c3f356d 100755 --- a/reference_data/tests_only/sparsification_inputs.json +++ b/reference_data/tests_only/sparsification_inputs.json @@ -10,17 +10,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -41,17 +37,13 @@ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -75,17 +67,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -106,17 +94,13 @@ 2, 6, 7, 5, 11, 8, 10, 3, 1, 0, 4, 9 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -140,17 +124,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -169,17 +149,13 @@ "l": [1, 2, 2, 1, 1, 0, 1, 0], "selected_features_global_ids": [1, 5, 2, 7, 10, 3, 4, 6] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -203,17 +179,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -278,17 +250,13 @@ 116, 117, 118, 119 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -312,17 +280,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -387,17 +351,13 @@ 100, 72, 33, 62, 107 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -421,17 +381,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -450,17 +406,13 @@ "l": [1, 2, 0, 2, 0, 1, 2, 1], "selected_features_global_ids": [91, 38, 51, 23, 102, 115, 26, 49] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -484,17 +436,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -531,17 +479,13 @@ 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -565,17 +509,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -612,17 +552,13 @@ 17, 31, 30, 18, 28, 11, 34, 27, 24, 3, 9, 21, 19, 22, 4, 10, 16, 6 ] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { @@ -646,17 +582,13 @@ "expansion_by_species_method": "structure wise", "global_species": [], "compute_gradients": true, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "rep_sparse": { "max_radial": 2, @@ -675,17 +607,13 @@ "l": [1, 0, 0, 0, 0, 0, 1, 1], "selected_features_global_ids": [4, 9, 30, 15, 21, 3, 1, 13] }, - "cutoff_function": { - "type": "ShiftedCosine", - "cutoff": {"value": 3.5, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - }, - "gaussian_density": { - "type": "Constant", "gaussian_sigma": {"value": 0.4, "unit": "AA"} - }, - "radial_contribution": { - "type": "GTO", "optimization": {} - } + "cutoff_function_type": "ShiftedCosine", + "interaction_cutoff": 3.5, + "cutoff_smooth_width": 0.5, + "gaussian_sigma_type": "Constant", + "gaussian_sigma_constant": 0.4, + "radial_basis": "GTO", + "optimization": {} }, "adaptors": [ { diff --git a/reference_data/tests_only/spherical_covariants_reference.ubjson b/reference_data/tests_only/spherical_covariants_reference.ubjson index eff1b59b7..75648d5f0 100644 Binary files a/reference_data/tests_only/spherical_covariants_reference.ubjson and b/reference_data/tests_only/spherical_covariants_reference.ubjson differ diff --git a/reference_data/tests_only/spherical_expansion_reference.ubjson b/reference_data/tests_only/spherical_expansion_reference.ubjson index 07d034c2d..b06efbfe3 100644 Binary files a/reference_data/tests_only/spherical_expansion_reference.ubjson and b/reference_data/tests_only/spherical_expansion_reference.ubjson differ diff --git a/reference_data/tests_only/spherical_invariants_reference.ubjson b/reference_data/tests_only/spherical_invariants_reference.ubjson index eb6b2c67c..88eed4f61 100644 Binary files a/reference_data/tests_only/spherical_invariants_reference.ubjson and b/reference_data/tests_only/spherical_invariants_reference.ubjson differ diff --git a/src/rascal/representations/calculator_spherical_expansion.hh b/src/rascal/representations/calculator_spherical_expansion.hh index e7bbb5b90..a10dc14a7 100644 --- a/src/rascal/representations/calculator_spherical_expansion.hh +++ b/src/rascal/representations/calculator_spherical_expansion.hh @@ -31,6 +31,18 @@ #ifndef SRC_RASCAL_REPRESENTATIONS_CALCULATOR_SPHERICAL_EXPANSION_HH_ #define SRC_RASCAL_REPRESENTATIONS_CALCULATOR_SPHERICAL_EXPANSION_HH_ +// This variable stores the length of the absolute path of this file up to the +// librascal directory It is set in the CMakeLists.txt, but for clang > 4 it is +// not stored, so we set it here to zero in this case +#ifndef SOURCE_PATH_SIZE +#define SOURCE_PATH_SIZE 0 +#endif + +// relative path of file with respect to librascal directory +#ifndef __FILENAME__ +#define __FILENAME__ ((const char *)&__FILE__[SOURCE_PATH_SIZE]) +#endif + #include "rascal/math/bessel.hh" #include "rascal/math/gauss_legendre.hh" #include "rascal/math/hyp1f1.hh" @@ -131,8 +143,8 @@ namespace rascal { : AtomicSmearingSpecificationBase { using Hypers_t = typename AtomicSmearingSpecificationBase::Hypers_t; explicit AtomicSmearingSpecification(const Hypers_t & hypers) { - this->constant_gaussian_sigma = - hypers.at("gaussian_sigma").at("value").get(); + this->constant_gaussian_sigma = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "gaussian_sigma_constant", 0.3); if (this->constant_gaussian_sigma < 1e-2) { std::stringstream err_str{}; err_str << "Constant gaussian sigma is too small: " @@ -354,33 +366,32 @@ namespace rascal { this->max_radial = hypers.at("max_radial"); this->max_angular = hypers.at("max_angular"); - if (hypers.count("compute_gradients")) { - this->compute_gradients = hypers.at("compute_gradients").get(); - } else { // Default false (don't compute gradients) - this->compute_gradients = false; - } - - this->init_matrices(); + this->compute_gradients = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "compute_gradients", false); // find the cutoff radius of the representation - auto fc_hypers = hypers.at("cutoff_function").get(); this->interaction_cutoff = - fc_hypers.at("cutoff").at("value").get(); + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); // define the type of smearing to use - auto smearing_hypers = hypers.at("gaussian_density").get(); - auto smearing_type = smearing_hypers.at("type").get(); + std::string smearing_type{ + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "gaussian_sigma_type", + "Constant")}; if (smearing_type == "Constant") { this->atomic_smearing_type = AtomicSmearingType::Constant; this->atomic_smearing = - make_atomic_smearing( - smearing_hypers); + make_atomic_smearing(hypers); } else { throw std::logic_error( - "Requested Gaussian sigma type \'" + smearing_type + - "\' has not been implemented. Must be one of" + - ": \'Constant\'."); + "Requested Gaussian sigma type '" + smearing_type + + "' has not been implemented. Must be one of : 'Constant' at " + "file " + + std::string(__FILENAME__) + ":" + std::to_string(__LINE__)); } + + this->init_matrices(); } // initialize Eigen matrices/vectors, contains both @@ -767,40 +778,57 @@ namespace rascal { void set_hyperparameters(const Hypers_t & hypers) override { this->hypers = hypers; - this->max_radial = hypers.at("max_radial"); - this->max_angular = hypers.at("max_angular"); + this->compute_gradients = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "compute_gradients", false); - if (hypers.count("compute_gradients")) { - this->compute_gradients = hypers.at("compute_gradients").get(); - } else { // Default false (don't compute gradients) - this->compute_gradients = false; + int max_radial_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_radial")}; + if (max_radial_int <= 0) { + std::stringstream error{}; + error << "Parameter 'max_radial' has to be > 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); } + this->max_radial = static_cast(max_radial_int); + + int max_angular_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_angular")}; + if (max_angular_int < 0) { + std::stringstream error{}; + error << "Parameter 'max_angular' has to be >= 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); + } + this->max_angular = static_cast(max_angular_int); - this->init_matrices(); - - // find the cutoff radius of the representation - auto fc_hypers = hypers.at("cutoff_function").get(); this->interaction_cutoff = - fc_hypers.at("cutoff").at("value").get(); - this->smooth_width = - fc_hypers.at("smooth_width").at("value").get(); - + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); + this->cutoff_smooth_width = + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_smooth_width"); + + std::string smearing_type{ + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "gaussian_sigma_type", + "Constant")}; // define the type of smearing to use - auto smearing_hypers = hypers.at("gaussian_density").get(); - auto smearing_type = smearing_hypers.at("type").get(); if (smearing_type == "Constant") { this->atomic_smearing_type = AtomicSmearingType::Constant; + auto atomic_smearing_constant{std::make_shared< + AtomicSmearingSpecification>( + hypers)}; + this->smearing = atomic_smearing_constant->get_gaussian_sigma(); this->atomic_smearing = - make_atomic_smearing( - smearing_hypers); - this->smearing = - smearing_hypers.at("gaussian_sigma").at("value").get(); + std::static_pointer_cast( + atomic_smearing_constant); } else { throw std::logic_error( - "Requested Gaussian sigma type \'" + smearing_type + - "\' has not been implemented. Must be one of" + - ": \'Constant\'."); + "Requested Gaussian sigma type '" + smearing_type + + "' has not been implemented. Must be one of" + ": 'Constant'."); } + + this->init_matrices(); } // initialize Eigen matrices/vectors, contains both @@ -926,7 +954,7 @@ namespace rascal { Hypers_t hypers{}; // some useful parameters double interaction_cutoff{}; - double smooth_width{}; + double cutoff_smooth_width{}; double smearing{}; size_t max_radial{}; size_t max_angular{}; @@ -1105,10 +1133,8 @@ namespace rascal { } void init_interpolator(const Hypers_t & hypers) { - auto radial_contribution_hypers = - hypers.at("radial_contribution").template get(); - auto optimization_hypers = - radial_contribution_hypers.at("optimization").template get(); + json optimization_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "optimization"); double accuracy{this->get_interpolator_accuracy(optimization_hypers)}; // minimal distance such that it is still stable with the interpolated @@ -1135,20 +1161,15 @@ namespace rascal { } double get_interpolator_accuracy(const Hypers_t & optimization_hypers) { - auto spline_hypers = - optimization_hypers.at("Spline").template get(); - if (spline_hypers.count("accuracy")) { - return spline_hypers.at("accuracy").template get(); - } else { - std::stringstream err_str{}; - err_str << "No Spline accuracy was given."; - throw std::logic_error(err_str.str()); - } + json spline_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, optimization_hypers, "Spline"); + return json_io::template read_hyperparameter( + __FILENAME__, __LINE__, spline_hypers, "accuracy"); } double get_cutoff(const Hypers_t & hypers) { - auto fc_hypers = hypers.at("cutoff_function").template get(); - return fc_hypers.at("cutoff").at("value").template get(); + return json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); } double fac_a{}; @@ -1186,149 +1207,145 @@ namespace rascal { } void set_hyperparameters(const Hypers_t & hypers) override { - auto radial_contribution_hypers = - hypers.at("radial_contribution").template get(); - auto optimization_hypers = - radial_contribution_hypers.at("optimization").template get(); - auto radial_dim_reduction_hypers = - optimization_hypers.at("RadialDimReduction").template get(); - if (radial_dim_reduction_hypers.count("projection_matrices")) { - std::map>>> - json_projection_matrices{}; - try { - // hyper keys are converted to string so we also use here strings - // for species - json_projection_matrices = - radial_dim_reduction_hypers.at("projection_matrices") - .template get>>>>(); - } catch (const std::exception & e) { - std::stringstream err_str{}; - err_str << "Projection matrices should be of type " - "std::map>>>."; - throw std::logic_error(err_str.str()); - } - - this->n_species = json_projection_matrices.size(); - this->n_components = this->max_radial; - - if (json_projection_matrices.begin()->second.size() == 0) { - std::stringstream err_str{}; - err_str << "projection_matrices should have the shape " - "(max_angular+1, max_radial, expanded_max_radial)"; - throw std::logic_error(err_str.str()); - } - if (json_projection_matrices.begin()->second.at(0).size() == 0) { - std::stringstream err_str{}; - err_str << "projection_matrices should have the shape " - "(max_angular+1, max_radial, expanded_max_radial)"; - throw std::logic_error(err_str.str()); - } - if (json_projection_matrices.begin()->second.at(0).at(0).size() == - 0) { + json optimization_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "optimization"); + json radial_dim_reduction_hypers = + json_io::template read_hyperparameter(__FILENAME__, __LINE__, + optimization_hypers, + "RadialDimReduction"); + std::map>>> + json_projection_matrices{}; + // hyper keys are converted to string so we also use here strings + // for species + + json_projection_matrices = + json_io::template read_hyperparameter>>>>( + __FILENAME__, __LINE__, radial_dim_reduction_hypers, + "projection_matrices"); + + this->n_species = json_projection_matrices.size(); + this->n_components = this->max_radial; + + if (json_projection_matrices.begin()->second.size() == 0) { + std::stringstream err_str{}; + err_str << "projection_matrices should have the shape " + << "(max_angular+1, max_radial, expanded_max_radial)" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; + throw std::logic_error(err_str.str()); + } + if (json_projection_matrices.begin()->second.at(0).size() == 0) { + std::stringstream err_str{}; + err_str << "projection_matrices should have the shape " + << "(max_angular+1, max_radial, expanded_max_radial)" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; + throw std::logic_error(err_str.str()); + } + if (json_projection_matrices.begin()->second.at(0).at(0).size() == 0) { + std::stringstream err_str{}; + err_str << "projection_matrices should have the shape " + << "(max_angular+1, max_radial, expanded_max_radial)" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; + throw std::logic_error(err_str.str()); + } + // overwrite max_radial to expanded max_radial + this->max_radial = + json_projection_matrices.begin()->second.at(0).at(0).size(); + + // check projection matrices sizes + std::string species; + for (auto it = json_projection_matrices.begin(); + it != json_projection_matrices.end(); ++it) { + species = it->first; + if (json_projection_matrices.at(species).size() != + this->max_angular + 1) { std::stringstream err_str{}; - err_str << "projection_matrices should have the shape " - "(max_angular+1, max_radial, expanded_max_radial)"; + err_str << "Projection matrices at species=" << species + << " does not match the size max_angular=" + << this->max_angular << " but is " + << json_projection_matrices.at(species).size() + << ", should be max_angular+1" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; throw std::logic_error(err_str.str()); } - // overwrite max_radial to expanded max_radial - this->max_radial = - json_projection_matrices.begin()->second.at(0).at(0).size(); - - // check projection matrices sizes - std::string species; - for (auto it = json_projection_matrices.begin(); - it != json_projection_matrices.end(); ++it) { - species = it->first; - if (json_projection_matrices.at(species).size() != - this->max_angular + 1) { + for (size_t angular_l = 0; angular_l < this->max_angular + 1; + angular_l++) { + if (json_projection_matrices.at(species).at(angular_l).size() != + this->n_components) { std::stringstream err_str{}; - err_str << "Projection matrices at species=" << species - << " does not match the size max_angular=" - << this->max_angular << " but is " - << json_projection_matrices.at(species).size() - << ", should be max_angular+1"; + // be aware that `max_radial` in hyperparameters is + // this->n_components within in this class + err_str + << "Projection matrices at species=" << species + << " and angular_l=" << angular_l + << " does not match the max_radial=" << this->n_components + << " in hyperparameters but is " + << json_projection_matrices.at(species).at(angular_l).size() + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; throw std::logic_error(err_str.str()); } - for (size_t angular_l = 0; angular_l < this->max_angular + 1; - angular_l++) { - if (json_projection_matrices.at(species).at(angular_l).size() != - this->n_components) { + for (size_t radial_c = 0; radial_c < this->n_components; + radial_c++) { + if (json_projection_matrices.at(species) + .at(angular_l) + .at(radial_c) + .size() != this->max_radial) { std::stringstream err_str{}; // be aware that `max_radial` in hyperparameters is // this->n_components within in this class - err_str - << "Projection matrices at species=" << species - << " and angular_l=" << angular_l - << " does not match the max_radial=" << this->n_components - << " in hyperparameters but is " - << json_projection_matrices.at(species).at(angular_l).size() - << "."; + err_str << "Projection matrices at species=" << species + << ", angular_l=" << angular_l + << " and max_radial=" << this->n_components + << " is not consistent in expanded_max_radial=" + << this->max_radial + << " with species=0, angular_l=0 and max_radial=0" + << json_projection_matrices.at(species) + .at(angular_l) + .at(radial_c) + .size() + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; throw std::logic_error(err_str.str()); } - for (size_t radial_c = 0; radial_c < this->n_components; - radial_c++) { - if (json_projection_matrices.at(species) - .at(angular_l) - .at(radial_c) - .size() != this->max_radial) { - std::stringstream err_str{}; - // be aware that `max_radial` in hyperparameters is - // this->n_components within in this class - err_str << "Projection matrices at species=" << species - << ", angular_l=" << angular_l - << " and max_radial=" << this->n_components - << " is not consistent in expanded_max_radial=" - << this->max_radial - << " with species=0, angular_l=0 and max_radial=0" - << json_projection_matrices.at(species) - .at(angular_l) - .at(radial_c) - .size() - << "."; - throw std::logic_error(err_str.str()); - } - } } } + } - // init projection matrices - Matrix_t projection_matrix{}; - std::vector angular_projection_matrices(this->max_angular + - 1); - for (auto it = json_projection_matrices.begin(); - it != json_projection_matrices.end(); ++it) { - species = it->first; - for (size_t angular_l = 0; angular_l < this->max_angular + 1; - angular_l++) { - projection_matrix.resize(this->n_components, this->max_radial); - projection_matrix.setZero(); - for (size_t radial_c = 0; radial_c < this->n_components; - radial_c++) { - for (size_t radial_n = 0; radial_n < this->max_radial; - radial_n++) { - projection_matrix(radial_c, radial_n) = - json_projection_matrices.at(species) - .at(angular_l) - .at(radial_c) - .at(radial_n); - } + // init projection matrices + Matrix_t projection_matrix{}; + std::vector angular_projection_matrices(this->max_angular + + 1); + for (auto it = json_projection_matrices.begin(); + it != json_projection_matrices.end(); ++it) { + species = it->first; + for (size_t angular_l = 0; angular_l < this->max_angular + 1; + angular_l++) { + projection_matrix.resize(this->n_components, this->max_radial); + projection_matrix.setZero(); + for (size_t radial_c = 0; radial_c < this->n_components; + radial_c++) { + for (size_t radial_n = 0; radial_n < this->max_radial; + radial_n++) { + projection_matrix(radial_c, radial_n) = + json_projection_matrices.at(species) + .at(angular_l) + .at(radial_c) + .at(radial_n); } - angular_projection_matrices.at(angular_l) = projection_matrix; } - this->projection_matrices.insert( - std::pair>( - std::stoi(species), angular_projection_matrices)); + angular_projection_matrices.at(angular_l) = projection_matrix; } - - this->init_matrices(); - } else { - std::stringstream err_str{}; - err_str << "No projection matrices were given."; - throw std::logic_error(err_str.str()); + this->projection_matrices.insert( + std::pair>( + std::stoi(species), angular_projection_matrices)); } + + this->init_matrices(); Parent::init_matrices(); } @@ -1451,10 +1468,8 @@ namespace rascal { } void init_interpolator(const Hypers_t & hypers) { - auto radial_contribution_hypers = - hypers.at("radial_contribution").template get(); - auto optimization_hypers = - radial_contribution_hypers.at("optimization").template get(); + json optimization_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "optimization"); double accuracy{this->get_interpolator_accuracy(optimization_hypers)}; // minimal distance such that it is still stable with the interpolated @@ -1486,20 +1501,15 @@ namespace rascal { } double get_interpolator_accuracy(const Hypers_t & optimization_hypers) { - auto spline_hypers = - optimization_hypers.at("Spline").template get(); - if (spline_hypers.count("accuracy")) { - return spline_hypers.at("accuracy").template get(); - } else { - std::stringstream err_str{}; - err_str << "No Spline accuracy was given."; - throw std::logic_error(err_str.str()); - } + json spline_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, optimization_hypers, "Spline"); + return json_io::template read_hyperparameter( + __FILENAME__, __LINE__, spline_hypers, "accuracy"); } double get_cutoff(const Hypers_t & hypers) { - auto fc_hypers = hypers.at("cutoff_function").template get(); - return fc_hypers.at("cutoff").at("value").template get(); + return json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); } Matrix_t reduced_radial_integral_neighbour{}; @@ -1600,46 +1610,56 @@ namespace rascal { using internal::RadialBasisType; this->hypers = hypers; - this->max_radial = hypers.at("max_radial").get(); - this->max_angular = hypers.at("max_angular").get(); - if (hypers.count("compute_gradients")) { - this->compute_gradients = hypers.at("compute_gradients").get(); - } else { // Default false (don't compute gradients) - this->compute_gradients = false; - } - - if (hypers.count("expansion_by_species_method")) { - std::set possible_expansion_by_species{ - {"environment wise", "user defined", "structure wise"}}; - auto expansion_by_species_tmp = - hypers.at("expansion_by_species_method").get(); - if (possible_expansion_by_species.count(expansion_by_species_tmp)) { - this->expansion_by_species = expansion_by_species_tmp; - } else { - std::stringstream err_str{}; - err_str << "expansion_by_species_method provided:'" - << expansion_by_species_tmp - << "' is not part of the implemented methods: '"; - for (const auto & val : possible_expansion_by_species) { - err_str << val << "', "; - } - throw std::logic_error(err_str.str()); - } - } else { - // default value for backward compatibility - this->expansion_by_species = "environment wise"; + int max_radial_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_radial")}; + if (max_radial_int <= 0) { + std::stringstream error{}; + error << "Parameter 'max_radial' has to be > 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); } + this->max_radial = static_cast(max_radial_int); + + int max_angular_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_angular")}; + if (max_angular_int < 0) { + std::stringstream error{}; + error << "Parameter 'max_angular' has to be >= 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); + } + this->max_angular = static_cast(max_angular_int); + + this->compute_gradients = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "compute_gradients", false); + + this->expansion_by_species = + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "expansion_by_species_method", + "environment wise"); if (hypers.count("global_species")) { - auto species = hypers.at("global_species").get(); + Key_t species = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "global_species"); + if (species.size() == 0 && + this->expansion_by_species == "user defined") { + std::stringstream err_str{}; + err_str << "expansion_by_species is 'user defined'" + << " but global_species is empty" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; + throw std::logic_error(err_str.str()); + } for (const auto & sp : species) { this->global_species.insert({sp}); } } else { if (this->expansion_by_species == "user defined") { std::stringstream err_str{}; - err_str << "expansion_by_species_method is 'user defined'" - << " but global_species is not defined."; + err_str << "expansion_by_species is 'user defined'" + << " but global_species is not defined" + << " at file " << __FILENAME__ << ":" << __LINE__ + << std::endl; throw std::logic_error(err_str.str()); } this->global_species.clear(); @@ -1651,29 +1671,35 @@ namespace rascal { // create the class that will compute the radial terms of the // expansion. the atomic smearing is an integral part of the // radial contribution - auto smearing_hypers = hypers.at("gaussian_density").get(); - auto smearing_type = smearing_hypers.at("type").get(); + std::string smearing_type{ + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "gaussian_sigma_type", + "Constant")}; if (smearing_type == "Constant") { this->atomic_smearing_type = AtomicSmearingType::Constant; } else if (smearing_type == "PerSpecies") { - throw std::logic_error("Requested Smearing type \'PerSpecies\'" - "\' has not been implemented. Must be one of" - ": \'Constant\'."); + throw std::logic_error("Requested Smearing type 'PerSpecies'" + " has not been implemented. Must be one of" + ": 'Constant' at file " + + std::string(__FILENAME__) + ":" + + std::to_string(__LINE__)); } else if (smearing_type == "Radial") { - throw std::logic_error("Requested Smearing type \'Radial\'" - "\' has not been implemented. Must be one of" - ": \'Constant\'."); + throw std::logic_error("Requested Smearing type 'Radial'" + "' has not been implemented. Must be one of" + ": 'Constant' at file " + + std::string(__FILENAME__) + ":" + + std::to_string(__LINE__)); } else { - throw std::logic_error("Requested Smearing type \'" + smearing_type + - "\' is unknown. Must be one of" + - ": \'Constant\'."); + throw std::logic_error( + "Requested Smearing type '" + smearing_type + + "' is unknown. Must be one of" + ": 'Constant' at file " + + std::string(__FILENAME__) + ":" + std::to_string(__LINE__)); } - auto radial_contribution_hypers = - hypers.at("radial_contribution").get(); - auto radial_contribution_type = - radial_contribution_hypers.at("type").get(); + std::string radial_contribution_type{ + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "radial_basis", "GTO")}; // create the class that will compute the radial terms of the // expansion. the atomic smearing is an integral part of the @@ -1692,25 +1718,31 @@ namespace rascal { this->radial_integral = rc_shared; this->radial_integral_type = RadialBasisType::DVR; } else { - throw std::logic_error("Requested Radial contribution type \'" + - radial_contribution_type + - "\' has not been implemented. Must be one of" + - ": \'GTO\' or \'DVR\'. "); - } - - if (radial_contribution_hypers.count("optimization")) { - auto optimization_hypers = - radial_contribution_hypers.at("optimization").get(); - // Checks for all optimization args used for the computation of the - // spherical expansion - if (optimization_hypers.count("Spline") && - optimization_hypers.count("RadialDimReduction")) { - this->optimization_type = OptimizationType::RadialDimReductionSpline; - } else if (optimization_hypers.count("Spline")) { - this->optimization_type = OptimizationType::Spline; - } else if (optimization_hypers.count("RadialDimReduction")) { - throw std::logic_error("RadialDimReduction requires\'" - "\' Spline to be set."); + throw std::logic_error( + "Requested Radial contribution type '" + radial_contribution_type + + "' has not been implemented. Must be one of" + + ": 'GTO' or 'DVR' at file " + std::string(__FILENAME__) + ":" + + std::to_string(__LINE__)); + } + + if (hypers.count("optimization")) { + json optimization_hypers = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "optimization"); + if (optimization_hypers.size() != 0) { + // Checks for all optimization args used for the computation of the + // spherical expansion + if (optimization_hypers.count("Spline") && + optimization_hypers.count("RadialDimReduction")) { + this->optimization_type = + OptimizationType::RadialDimReductionSpline; + } else if (optimization_hypers.count("Spline")) { + this->optimization_type = OptimizationType::Spline; + } else if (optimization_hypers.count("RadialDimReduction")) { + throw std::logic_error("RadialDimReduction requires '" + "' Spline to be set at file " + + std::string(__FILENAME__) + ":" + + std::to_string(__LINE__)); + } } else { this->optimization_type = OptimizationType::None; } @@ -1720,22 +1752,28 @@ namespace rascal { this->set_radial_integral(hypers); - auto fc_hypers = hypers.at("cutoff_function").get(); - auto fc_type = fc_hypers.at("type").get(); - this->interaction_cutoff = fc_hypers.at("cutoff").at("value"); - this->cutoff_smooth_width = fc_hypers.at("smooth_width").at("value"); - if (fc_type == "ShiftedCosine") { + this->interaction_cutoff = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); + this->cutoff_smooth_width = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_smooth_width"); + + std::string cutoff_function_type_description{ + json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_function_type", + "ShiftedCosine")}; + if (cutoff_function_type_description == "ShiftedCosine") { this->cutoff_function_type = CutoffFunctionType::ShiftedCosine; this->cutoff_function = - make_cutoff_function(fc_hypers); - } else if (fc_type == "RadialScaling") { + make_cutoff_function(hypers); + } else if (cutoff_function_type_description == "RadialScaling") { this->cutoff_function_type = CutoffFunctionType::RadialScaling; this->cutoff_function = - make_cutoff_function(fc_hypers); + make_cutoff_function(hypers); } else { - throw std::logic_error("Requested cutoff function type \'" + fc_type + - "\' has not been implemented. Must be one of" + - ": \'ShiftedCosine\' or 'RadialScaling'."); + throw std::logic_error("Requested cutoff function type '" + + cutoff_function_type_description + + "' has not been implemented. Must be one of" + + ": 'ShiftedCosine' or 'RadialScaling'."); } this->set_name(hypers); @@ -2225,7 +2263,14 @@ namespace rascal { this->initialize_expansion_structure_wise( manager, expansions_coefficients, expansions_coefficients_gradient); } else { - throw std::runtime_error("should not arrive here"); + std::stringstream error{}; + error << "Requested expansion by species method '" + << this->expansion_by_species + << "' is unknown. Must be one of : 'environment wise', 'user " + "defined' or 'structure wise' file " + << __FILENAME__ << ":" << __LINE__ << std::endl; + + throw std::runtime_error(error.str()); } // coeff C^{ij}_{nlm} diff --git a/src/rascal/representations/calculator_spherical_invariants.hh b/src/rascal/representations/calculator_spherical_invariants.hh index ad543d970..06c0a22f8 100644 --- a/src/rascal/representations/calculator_spherical_invariants.hh +++ b/src/rascal/representations/calculator_spherical_invariants.hh @@ -31,6 +31,18 @@ #ifndef SRC_RASCAL_REPRESENTATIONS_CALCULATOR_SPHERICAL_INVARIANTS_HH_ #define SRC_RASCAL_REPRESENTATIONS_CALCULATOR_SPHERICAL_INVARIANTS_HH_ +// This variable stores the length of the absolute path of this file up to the +// librascal directory It is set in the CMakeLists.txt, but for clang > 4 it is +// not stored, so we set it here to zero in this case +#ifndef SOURCE_PATH_SIZE +#define SOURCE_PATH_SIZE 0 +#endif + +// relative path of file with respect to librascal directory +#ifndef __FILENAME__ +#define __FILENAME__ ((const char *)&__FILE__[SOURCE_PATH_SIZE]) +#endif + #include "rascal/math/utils.hh" #include "rascal/representations/calculator_base.hh" #include "rascal/representations/calculator_spherical_expansion.hh" @@ -257,20 +269,37 @@ namespace rascal { void set_hyperparameters(const Hypers_t & hypers) override { using internal::SphericalInvariantsType; - this->normalize = hypers.at("normalize").get(); - auto soap_type = hypers.at("soap_type").get(); - - if (hypers.find("compute_gradients") != hypers.end()) { - this->compute_gradients = hypers.at("compute_gradients").get(); - } else { // Default false (don't compute gradients) - this->compute_gradients = false; + this->normalize = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "normalize", true); + std::string soap_type{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "soap_type", "PowerSpectrum")}; + this->compute_gradients = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "compute_gradients", false); + + int max_radial_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_radial")}; + if (max_radial_int <= 0) { + std::stringstream error{}; + error << "CalculatorSphericalInvariants.set_hyperparameters: " + "'max_radial' has to be > 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); } - - this->max_radial = hypers.at("max_radial").get(); - this->max_angular = hypers.at("max_angular").get(); + this->max_radial = static_cast(max_radial_int); + + int max_angular_int{json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "max_angular")}; + if (max_angular_int < 0) { + std::stringstream error{}; + error << "CalculatorSphericalInvariants.set_hyperparameters: " + "'max_angular' has to be >= 0" + << " at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); + } + this->max_angular = static_cast(max_angular_int); if (soap_type == "PowerSpectrum") { - this->set_hyperparameters_powerspectrum(hypers); + this->set_coefficient_subselection(hypers); } else if (soap_type == "RadialSpectrum") { this->type = SphericalInvariantsType::RadialSpectrum; if (this->max_angular > 0) { @@ -278,13 +307,14 @@ namespace rascal { } } else if (soap_type == "BiSpectrum") { this->type = internal::SphericalInvariantsType::BiSpectrum; - this->inversion_symmetry = hypers.at("inversion_symmetry").get(); + this->inversion_symmetry = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "inversion_symmetry", true); this->wigner_w3js = internal::precompute_wigner_w3js( this->max_angular, this->inversion_symmetry); } else { throw std::logic_error( - "Requested SphericalInvariants type \'" + soap_type + - "\' has not been implemented. Must be one of" + + "Requested SphericalInvariants type '" + soap_type + + "' has not been implemented. Must be one of" + ": 'PowerSpectrum', 'RadialSpectrum', or 'BiSpectrum'."); } @@ -308,29 +338,46 @@ namespace rascal { * Note that values in 'n1', 'n2', 'l' should not exceed max_radial-1 * and max_angular. */ - void set_hyperparameters_powerspectrum(const Hypers_t & hypers) { + void set_coefficient_subselection(const Hypers_t & hypers) { using internal::SphericalInvariantsType; this->type = SphericalInvariantsType::PowerSpectrum; - if (hypers.find("coefficient_subselection") != hypers.end()) { + json coefficient_subselection; + bool is_coefficient_subselection_set{false}; + if (hypers.count("coefficient_subselection")) { + coefficient_subselection = json_io::template read_hyperparameter( + __FILENAME__, __LINE__, hypers, "coefficient_subselection"); + if (coefficient_subselection.size() != 0) { + is_coefficient_subselection_set = true; + } + } + + if (is_coefficient_subselection_set) { this->is_sparsified = true; - auto coefficient_subselection = - hypers.at("coefficient_subselection").get(); + // get the indices of the subselected PowerSpectrum coefficients: // p_{abn_1n_2l} where a and b refer to atomic species and should be // lexicographically sorted so that a <= b, n_1 and n_2 refer to // radial basis index and l refer to the angular index of the // spherical harmonic - auto sp_a = coefficient_subselection.at("a") - .get>(); - auto sp_b = coefficient_subselection.at("b") - .get>(); - auto radial_n1 = - coefficient_subselection.at("n1").get>(); - auto radial_n2 = - coefficient_subselection.at("n2").get>(); - auto angular_l = - coefficient_subselection.at("l").get>(); + std::vector sp_a, sp_b; + std::vector radial_n1, radial_n2, angular_l; + + sp_a = json_io::template read_hyperparameter< + std::vector>( + __FILENAME__, __LINE__, coefficient_subselection, "a"); + sp_b = json_io::template read_hyperparameter< + std::vector>( + __FILENAME__, __LINE__, coefficient_subselection, "b"); + radial_n1 = + json_io::template read_hyperparameter>( + __FILENAME__, __LINE__, coefficient_subselection, "n1"); + radial_n2 = + json_io::template read_hyperparameter>( + __FILENAME__, __LINE__, coefficient_subselection, "n2"); + angular_l = + json_io::template read_hyperparameter>( + __FILENAME__, __LINE__, coefficient_subselection, "l"); std::uint32_t angular_max{ *std::max_element(angular_l.begin(), angular_l.end())}; @@ -656,9 +703,9 @@ namespace rascal { size_t max_angular{}; // shape of the inner dense section of the computed invariant coefficients std::array inner_invariants_shape{{0, 0}}; - bool normalize{}; - bool compute_gradients{}; - bool inversion_symmetry{false}; + bool normalize{true}; + bool compute_gradients{false}; + bool inversion_symmetry{true}; CalculatorSphericalExpansion rep_expansion; diff --git a/src/rascal/representations/cutoff_functions.hh b/src/rascal/representations/cutoff_functions.hh index 32eff4062..47c8c9c3e 100644 --- a/src/rascal/representations/cutoff_functions.hh +++ b/src/rascal/representations/cutoff_functions.hh @@ -29,6 +29,18 @@ #ifndef SRC_RASCAL_REPRESENTATIONS_CUTOFF_FUNCTIONS_HH_ #define SRC_RASCAL_REPRESENTATIONS_CUTOFF_FUNCTIONS_HH_ +// This variable stores the length of the absolute path of this file up to the +// librascal directory It is set in the CMakeLists.txt, but for clang > 4 it is +// not stored, so we set it here to zero in this case +#ifndef SOURCE_PATH_SIZE +#define SOURCE_PATH_SIZE 0 +#endif + +// relative path of file with respect to librascal directory +#ifndef __FILENAME__ +#define __FILENAME__ ((const char *)&__FILE__[SOURCE_PATH_SIZE]) +#endif + #include "rascal/math/utils.hh" #include "rascal/representations/calculator_base.hh" #include "rascal/utils/utils.hh" @@ -152,9 +164,10 @@ namespace rascal { } void set_hyperparameters(const Hypers_t & hypers) override { - this->cutoff = hypers.at("cutoff").at("value").get(); - this->smooth_width = - hypers.at("smooth_width").at("value").get(); + this->cutoff = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); + this->smooth_width = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_smooth_width"); } double f_c(double distance) { @@ -217,15 +230,25 @@ namespace rascal { } void set_hyperparameters(const Hypers_t & hypers) override { - this->cutoff = hypers.at("cutoff").at("value").get(); - this->smooth_width = - hypers.at("smooth_width").at("value").get(); - this->rate = hypers.at("rate").at("value").get(); + this->cutoff = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "interaction_cutoff"); + this->smooth_width = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_smooth_width"); + json cutoff_function_parameters = json_io::read_hyperparameter( + __FILENAME__, __LINE__, hypers, "cutoff_function_parameters"); + + this->rate = json_io::read_hyperparameter( + __FILENAME__, __LINE__, cutoff_function_parameters, "rate"); if (this->rate < 0) { - throw std::runtime_error("RadialScaling's rate should be positive"); + std::stringstream error{}; + error << "RadialScaling's 'rate' should be positive " + << "at file " << __FILENAME__ << ":" << __LINE__ << std::endl; + throw std::runtime_error(error.str()); } - this->exponent = hypers.at("exponent").at("value").get(); - this->scale = hypers.at("scale").at("value").get(); + this->exponent = json_io::read_hyperparameter( + __FILENAME__, __LINE__, cutoff_function_parameters, "exponent"); + this->scale = json_io::read_hyperparameter( + __FILENAME__, __LINE__, cutoff_function_parameters, "scale"); } double value(double distance) { diff --git a/src/rascal/utils/json_io.cc b/src/rascal/utils/json_io.cc index 32c4a7391..1aa4da0b7 100644 --- a/src/rascal/utils/json_io.cc +++ b/src/rascal/utils/json_io.cc @@ -28,6 +28,7 @@ #include "rascal/utils/json_io.hh" namespace rascal { + namespace json_io { json load(const std::string & filename) { diff --git a/src/rascal/utils/json_io.hh b/src/rascal/utils/json_io.hh index 7f8786048..eb4e83860 100644 --- a/src/rascal/utils/json_io.hh +++ b/src/rascal/utils/json_io.hh @@ -48,6 +48,7 @@ #include #include +#include // For convenience using json = nlohmann::json; @@ -301,6 +302,14 @@ namespace Eigen { namespace rascal { namespace json_io { + //! find if element appears in vector. + template + bool is_element_in(const T & element, const Vec & vector) { + Vec s_element{{element}}; + return not(std::search(vector.begin(), vector.end(), s_element.begin(), + s_element.end()) == vector.end()); + } + //! load a json file json load(const std::string & filename); @@ -356,6 +365,92 @@ namespace rascal { */ double check_units(const std::string & expected_unit, const json & parameter); + + /** + * Reads a hyperparameter from a json and handles the error properly in case + * of failure + * + * @tparam Hyperparameter_T the type of the returned hyperparameter. The + * type must support the << operator. + * + * @param filename the name of the file where the function is invoked + * @param line the line number where the function is invoked + * @param hypers the json with all hyperparameters + * @param key the name of the hyperparameter in the json `hypers` + * + * @throw runtime_error when the hyperparameter could not be read, because + * e.g. the `Hyperparameters_T` does not match hyperparameter type + * @throw runtime_error when the hyperparameter was not found in the json + * `hypers` + * + * @return the hyperparamater described by the `key` in the json `hypers` + */ + template + Hyperparameter_T read_hyperparameter(const char * filename, int line, + const json & hypers, std::string key) { + if (hypers.count(key)) { + try { + return hypers.at(key).get(); + } catch (const std::exception & e) { + std::stringstream error{}; + error << "Reading '" << key << "' from json hypers failed " + << "at file " << filename << ":" << line << "\n" + << " what(): " << e.what() << std::endl; + throw std::runtime_error(error.str()); + } + } else { + std::stringstream error{}; + error << "Reading '" << key << "' from json hypers failed. No '" << key + << "' was found in json hypers " + << "at file " << filename << ":" << line << std::endl; + throw std::runtime_error(error.str()); + } + } + + /** + * Reads a hyperparameter from a json and handles the error properly in case + * of failure + * + * @tparam Hyperparameter_T the type of the returned hyperparameter. The + * type must support the << operator. + * + * @param filename the name of the file where the function is invoked + * @param line the line number where the function is invoked + * @param hypers the json with all hyperparameters + * @param key the name of the hyperparameter in the json `hypers` + * @param default_hyperparameter this value is not used when the + * hyperparameter was not found in the json `hypers` + * + * @throw runtime_error when the hyperparameter could not be read, because + * e.g. the `Hyperparameters_T` does not match hyperparameter type + * @throw warning when key was not found in the json and + * `default_hyperparameter` is used + * + * @return the hyperparamater described by the `key` in the json `hypers` + */ + template + Hyperparameter_T + read_hyperparameter(const char * filename, int line, const json & hypers, + std::string key, + Hyperparameter_T default_hyperparameter) { + if (hypers.count(key)) { + try { + return hypers.at(key).get(); + } catch (const std::exception & e) { + std::stringstream error{}; + error << "Reading '" << key << "' from json hypers failed " + << "at file " << filename << ":" << line << "\n" + << " what(): " << e.what() << std::endl; + throw std::runtime_error(error.str()); + } + } else { + std::cout << "WARNING: Reading '" << key + << "' from json hypers failed. Setting " << key << " to '" + << default_hyperparameter << "' " + << "at file " << filename << ":" << line << std::endl; + return default_hyperparameter; + } + } } // namespace json_io } // namespace rascal diff --git a/tests/python/python_models_test.py b/tests/python/python_models_test.py index 05d852ce2..370e34e8b 100644 --- a/tests/python/python_models_test.py +++ b/tests/python/python_models_test.py @@ -132,21 +132,7 @@ def test_numerical_kernel_stress(self): # a general function transformation c++ parameters to # python would be more suitable here # future work - calculator = SphericalInvariants( - soap_type=hypers["soap_type"], - radial_basis=hypers["radial_contribution"]["type"], - max_radial=hypers["max_radial"], - max_angular=hypers["max_angular"], - cutoff_function_type=hypers["cutoff_function"]["type"], - interaction_cutoff=hypers["cutoff_function"]["cutoff"]["value"], - cutoff_smooth_width=hypers["cutoff_function"]["smooth_width"]["value"], - gaussian_sigma_type=hypers["gaussian_density"]["type"], - gaussian_sigma_constant=hypers["gaussian_density"]["gaussian_sigma"][ - "value" - ], - compute_gradients=hypers["compute_gradients"], - normalize=hypers["normalize"], - ) + calculator = SphericalInvariants(**hypers) kernel = Kernel(calculator, kernel_type="Sparse", **kernel_input["kernel"]) for j in range(len(frames)): # we do this frame by frame to be able to use the function diff --git a/tests/test_calculator.cc b/tests/test_calculator.cc index 10ef11c5b..97b998ae7 100644 --- a/tests/test_calculator.cc +++ b/tests/test_calculator.cc @@ -473,6 +473,7 @@ namespace rascal { for (const auto & rep_info : rep_infos.at(manager_i)) { const auto & representation_hypers = rep_info.at("hypers").template get(); + const auto & ref_representation = rep_info.at("feature_matrix").template get(); @@ -793,17 +794,22 @@ namespace rascal { this->factory_args.emplace_back(parameters); this->representation_hypers.emplace_back(); - auto fc_hyper = json::object( - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", cutoffs[i_filename]}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}); + json fc_hyp = json::object({{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", cutoffs[i_filename]}, + {"cutoff_smooth_width", 0.5}}); for (auto & ri_hyp : this->radial_contribution_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyper; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.back().push_back(rep_hyp); } } @@ -827,20 +833,22 @@ namespace rascal { std::vector> representation_hypers{}; std::vector fc_hypers{}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 2}, - {"max_angular", 2}, - {"normalize", true}, - {"soap_type", "PowerSpectrum"}, - {"compute_gradients", true}}, - {{"max_radial", 3}, - {"max_angular", 0}, - {"normalize", true}, - {"soap_type", "RadialSpectrum"}, - {"compute_gradients", true}}}; + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 2}, + {"max_angular", 2}, + {"normalize", true}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}, + {{"max_radial", 3}, + {"max_angular", 0}, + {"normalize", true}, + {"soap_type", "RadialSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}}; }; /** Contains some simple periodic structures for testing complicated things diff --git a/tests/test_calculator.hh b/tests/test_calculator.hh index d48bf549c..6a00634b6 100644 --- a/tests/test_calculator.hh +++ b/tests/test_calculator.hh @@ -29,6 +29,18 @@ #ifndef TESTS_TEST_CALCULATOR_HH_ #define TESTS_TEST_CALCULATOR_HH_ +// This variable stores the length of the absolute path of this file up to the +// librascal directory It is set in the CMakeLists.txt, but for clang > 4 it is +// not stored, so we set it here to zero in this case +#ifndef SOURCE_PATH_SIZE +#define SOURCE_PATH_SIZE 0 +#endif + +// relative path of file with respect to librascal directory +#ifndef __FILENAME__ +#define __FILENAME__ ((const char *)&__FILE__[SOURCE_PATH_SIZE]) +#endif + #include "test_adaptor.hh" #include "test_math.hh" #include "test_structure.hh" @@ -103,9 +115,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -117,42 +139,53 @@ namespace rascal { std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 3.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}}}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.2}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - - std::vector rep_hypers{{{"max_radial", 3}, - {"max_angular", 0}, - {"soap_type", "RadialSpectrum"}, - {"normalize", true}}, - {{"max_radial", 3}, - {"max_angular", 0}, - {"soap_type", "RadialSpectrum"}, - {"normalize", true}}, - {{"max_radial", 3}, - {"max_angular", 3}, - {"soap_type", "PowerSpectrum"}, - {"normalize", true}}, - {{"max_radial", 4}, - {"max_angular", 3}, - {"soap_type", "PowerSpectrum"}, - {"normalize", true}}, - {{"max_radial", 3}, - {"max_angular", 1}, - {"soap_type", "BiSpectrum"}, - {"inversion_symmetry", true}, - {"normalize", true}}, - {{"max_radial", 3}, - {"max_angular", 1}, - {"soap_type", "BiSpectrum"}, - {"inversion_symmetry", false}, - {"normalize", true}}}; + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.2}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + + std::vector rep_hypers{ + {{"max_radial", 3}, + {"max_angular", 0}, + {"soap_type", "RadialSpectrum"}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 3}, + {"max_angular", 0}, + {"soap_type", "RadialSpectrum"}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 3}, + {"max_angular", 3}, + {"soap_type", "PowerSpectrum"}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 4}, + {"max_angular", 3}, + {"soap_type", "PowerSpectrum"}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 3}, + {"max_angular", 1}, + {"soap_type", "BiSpectrum"}, + {"inversion_symmetry", true}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 3}, + {"max_angular", 1}, + {"soap_type", "BiSpectrum"}, + {"inversion_symmetry", false}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}}; }; template @@ -166,9 +199,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -180,29 +223,32 @@ namespace rascal { std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 1.0}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.0}, + {"cutoff_smooth_width", 1.0}}}; std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.2}, {"unit", "AA"}}}}, - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 1}, - {"max_angular", 2}, - {"soap_type", "LambdaSpectrum"}, - {"covariant_lambda", 2}, - {"inversion_symmetry", true}, - {"normalize", true}}, - {{"max_radial", 2}, - {"max_angular", 2}, - {"soap_type", "LambdaSpectrum"}, - {"covariant_lambda", 2}, - {"inversion_symmetry", false}, - {"normalize", true}}}; + {{"gaussian_sigma_type", "Constant"}, {"gaussian_sigma_constant", 0.2}}, + {{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 1}, + {"max_angular", 2}, + {"soap_type", "LambdaSpectrum"}, + {"covariant_lambda", 2}, + {"inversion_symmetry", true}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}, + {{"max_radial", 2}, + {"max_angular", 2}, + {"soap_type", "LambdaSpectrum"}, + {"covariant_lambda", 2}, + {"inversion_symmetry", false}, + {"normalize", true}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}}}; }; struct SphericalInvariantsTestData : TestData { @@ -242,9 +288,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -256,33 +312,34 @@ namespace rascal { std::vector representation_hypers{}; std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 3.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}, - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 1.0}, {"unit", "AA"}}}}, - {{"type", "RadialScaling"}, - {"cutoff", {{"value", 4.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}, - {"rate", {{"value", .0}, {"unit", "AA"}}}, - {"exponent", {{"value", 4}, {"unit", ""}}}, - {"scale", {{"value", 2.5}, {"unit", "AA"}}}}, - {{"type", "RadialScaling"}, - {"cutoff", {{"value", 4.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}, - {"rate", {{"value", 1.}, {"unit", "AA"}}}, - {"exponent", {{"value", 3}, {"unit", ""}}}, - {"scale", {{"value", 2.}, {"unit", "AA"}}}}}; - - std::vector radial_contribution_hypers{{{"type", "GTO"}}, - {{"type", "DVR"}}}; + {{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}}, + {{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.0}, + {"cutoff_smooth_width", 1.0}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 4.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", .0}, {"exponent", 4}, {"scale", 2.5}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 4.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 1.}, {"exponent", 3}, {"scale", 2.}}}}}; + + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}, + {{"radial_basis", "DVR"}}}; + + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.5}}}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.5}, {"unit", "AA"}}}}}; - - std::vector rep_hypers{{{"max_radial", 2}, {"max_angular", 2}}}; + std::vector rep_hypers{ + {{"max_radial", 2}, + {"max_angular", 2}, + {"compute_gradients", false}, + {"expansion_by_species_method", "environment wise"}}}; }; /** @@ -343,9 +400,22 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; + if (ri_hyp.count("optimization")) { + rep_hyp["optimization"] = ri_hyp["optimization"]; + } this->representation_hypers.push_back(rep_hyp); } } @@ -356,19 +426,17 @@ namespace rascal { ~MultipleHypersSphericalExpansion() = default; std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 3.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}, - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 1.0}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}}, + {{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.0}, + {"cutoff_smooth_width", 1.0}}}; std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.2}, {"unit", "AA"}}}}, - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; + {{"gaussian_sigma_type", "Constant"}, {"gaussian_sigma_constant", 0.2}}, + {{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; // nested json definitions dont work properly in this case: // if not defined seperately, radial dimension reduction_hypers // are interpreted as arrays @@ -389,16 +457,22 @@ namespace rascal { {"Spline", {{"accuracy", 1e-8}}}, {"RadialDimReduction", projection_matrices_hypers}}; std::vector radial_contribution_hypers{ - {{"type", "GTO"}, {"optimization", {}}}, - {{"type", "DVR"}, {"optimization", {}}}, - {{"type", "GTO"}, + {{"radial_basis", "GTO"}, {"optimization", {}}}, + {{"radial_basis", "DVR"}, {"optimization", {}}}, + {{"radial_basis", "GTO"}, {"optimization", {{"Spline", {{"accuracy", 1e-12}}}}}}, - {{"type", "DVR"}, {"optimization", {{"Spline", {{"accuracy", 1e-5}}}}}}, - {{"type", "GTO"}, {"optimization", radial_dim_reduction_spline_hypers}}, - {{"type", "DVR"}, {"optimization", radial_dim_reduction_spline_hypers}}, + {{"radial_basis", "DVR"}, + {"optimization", {{"Spline", {{"accuracy", 1e-5}}}}}}, + {{"radial_basis", "GTO"}, + {"optimization", radial_dim_reduction_spline_hypers}}, + {{"radial_basis", "DVR"}, + {"optimization", radial_dim_reduction_spline_hypers}}, }; std::vector rep_hypers{ - {{"max_radial", 3}, {"max_angular", 3}, {"compute_gradients", true}}}; + {{"max_radial", 3}, + {"max_angular", 3}, + {"compute_gradients", true}, + {"expansion_by_species_method", "environment wise"}}}; }; /** Contains some simple periodic structures for testing complicated things @@ -564,9 +638,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -577,14 +661,12 @@ namespace rascal { ~SingleHypersSphericalExpansion() = default; std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.5}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.5}, + {"cutoff_smooth_width", 0.5}}}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; std::map>>> projection_matrices{ {"1", @@ -648,16 +730,20 @@ namespace rascal { {"Spline", {{"accuracy", 1e-8}}}, {"RadialDimReduction", projection_matrices_hypers}}; std::vector radial_contribution_hypers{ - {{"type", "GTO"}, {"optimization", {}}}, - {{"type", "DVR"}, {"optimization", {}}}, - {{"type", "GTO"}, {"optimization", {{"Spline", {{"accuracy", 1e-8}}}}}}, - {{"type", "GTO"}, + {{"radial_basis", "GTO"}, {"optimization", {}}}, + {{"radial_basis", "DVR"}, {"optimization", {}}}, + {{"radial_basis", "GTO"}, + {"optimization", {{"Spline", {{"accuracy", 1e-8}}}}}}, + {{"radial_basis", "GTO"}, {"optimization", radial_dim_reduction_spline_hypers}}}; // if new hypers are added or current ones changed there will be problems // with the projection_matrices defined above since their size depend on // max_radial and max_angular std::vector rep_hypers{ - {{"max_radial", 2}, {"max_angular", 2}, {"compute_gradients", true}}}; + {{"max_radial", 2}, + {"max_angular", 2}, + {"compute_gradients", true}, + {"expansion_by_species_method", "environment wise"}}}; }; template @@ -671,9 +757,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -685,25 +781,26 @@ namespace rascal { std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.5}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.5}, + {"cutoff_smooth_width", 0.5}}}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 2}, - {"max_angular", 2}, - {"normalize", true}, - {"soap_type", "PowerSpectrum"}, - {"compute_gradients", true}}, - {{"max_radial", 2}, - {"max_angular", 0}, - {"normalize", true}, - {"soap_type", "RadialSpectrum"}, - {"compute_gradients", true}}}; + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 2}, + {"max_angular", 2}, + {"normalize", true}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}, + {{"max_radial", 2}, + {"max_angular", 0}, + {"normalize", true}, + {"soap_type", "RadialSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}}; }; struct ComplexHypersSphericalInvariants : ComplexPeriodicNLCCStrictFixture { @@ -716,9 +813,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -729,25 +836,26 @@ namespace rascal { ~ComplexHypersSphericalInvariants() = default; std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 3.5}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 1.0}, {"unit", "AA"}}}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 3.5}, + {"cutoff_smooth_width", 1.0}}}; - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 2}, - {"max_angular", 2}, - {"normalize", false}, - {"soap_type", "PowerSpectrum"}, - {"compute_gradients", true}}, - {{"max_radial", 2}, - {"max_angular", 2}, - {"normalize", true}, - {"soap_type", "PowerSpectrum"}, - {"compute_gradients", true}}}; + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 2}, + {"max_angular", 2}, + {"normalize", false}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}, + {{"max_radial", 2}, + {"max_angular", 2}, + {"normalize", true}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", true}}}; }; struct SphericalExpansionTestData : TestData { @@ -822,15 +930,16 @@ namespace rascal { // the fixture. This way we do not have to create a fixture for each // type while not using the wrong templated RadialIntegralHandler // constructor - auto radial_contribution_hypers = - hyper.at("radial_contribution").template get(); - auto radial_contribution_type_name = - radial_contribution_hypers.at("type").template get(); - auto density_hypers = hyper.at("gaussian_density").template get(); + // auto radial_contribution_type_name = + // radial_contribution_hypers.at("radial_basis").template + // get(); + std::string radial_contribution_type_name = + json_io::read_hyperparameter(__FILENAME__, __LINE__, + hyper, "radial_basis"); auto smearing_type_name = - density_hypers.at("type").template get(); + hyper.at("gaussian_sigma_type").template get(); auto optimization_hypers = - radial_contribution_hypers.at("optimization").template get(); + hyper.at("optimization").template get(); bool using_spline = optimization_hypers.count("Spline"); bool using_radial_dim_reduction = optimization_hypers.count("RadialDimReduction"); diff --git a/tests/test_cutoff_functions.cc b/tests/test_cutoff_functions.cc index c45d8318a..c48d89917 100644 --- a/tests/test_cutoff_functions.cc +++ b/tests/test_cutoff_functions.cc @@ -41,13 +41,9 @@ namespace rascal { */ BOOST_AUTO_TEST_CASE(shifted_cosine_gradient_test) { static const bool verbose{false}; - std::vector fc_hypers{ - R"({ - "type": "ShiftedCosine", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"} - })"_json, - }; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}}}; using Cutoff_t = internal::CutoffFunction; @@ -73,31 +69,22 @@ namespace rascal { BOOST_AUTO_TEST_CASE(radial_scaling_gradient_test) { static const bool verbose{false}; std::vector fc_hypers{ - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 1, "unit": "AA"}, - "scale": {"value": 2, "unit": "AA"} , - "exponent": {"value": 3, "unit": ""} - })"_json, - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 0, "unit": "AA"}, - "scale": {"value": 2, "unit": "AA"} , - "exponent": {"value": 3, "unit": ""} - })"_json, - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 0, "unit": "AA"}, - "scale": {"value": 2, "unit": "AA"} , - "exponent": {"value": 0, "unit": ""} - })"_json, - }; + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 1}, {"scale", 2}, {"exponent", 3}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 0}, {"scale", 2}, {"exponent", 3}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 0}, {"scale", 2}, {"exponent", 0}}}}}; + using Cutoff_t = internal::CutoffFunction; @@ -124,39 +111,26 @@ namespace rascal { BOOST_AUTO_TEST_CASE(radial_scaling_exponent_zero_test) { static const bool verbose{false}; std::vector fc_hypers{ - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 1, "unit": "AA"}, - "scale": {"value": 1, "unit": "AA"} , - "exponent": {"value": 0, "unit": ""} - })"_json, - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 0, "unit": "AA"}, - "scale": {"value": 0, "unit": "AA"} , - "exponent": {"value": 0, "unit": ""} - })"_json, - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 0, "unit": "AA"}, - "scale": {"value": 1, "unit": "AA"} , - "exponent": {"value": 0, "unit": ""} - })"_json, - R"({ - "type": "RadialScaling", - "cutoff": {"value": 3, "unit": "AA"}, - "smooth_width": {"value": 0.5, "unit": "AA"}, - "rate": {"value": 1, "unit": "AA"}, - "scale": {"value": 0, "unit": "AA"} , - "exponent": {"value": 0, "unit": ""} - })"_json, - }; + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 1}, {"scale", 1}, {"exponent", 0}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 0}, {"scale", 0}, {"exponent", 0}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 0}, {"scale", 1}, {"exponent", 0}}}}, + {{"cutoff_function_type", "RadialScaling"}, + {"interaction_cutoff", 3.0}, + {"cutoff_smooth_width", 0.5}, + {"cutoff_function_parameters", + {{"rate", 1}, {"scale", 0}, {"exponent", 0}}}}}; using Cutoff_t = internal::CutoffFunction; diff --git a/tests/test_kernels.hh b/tests/test_kernels.hh index 8abf7910e..a99f12bcb 100644 --- a/tests/test_kernels.hh +++ b/tests/test_kernels.hh @@ -48,9 +48,22 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; + if (ri_hyp.count("optimization")) { + rep_hyp["optimization"] = ri_hyp["optimization"]; + } this->representation_hypers.push_back(rep_hyp); } } @@ -61,23 +74,26 @@ namespace rascal { std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}}; - - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 3}, - {"max_angular", 0}, - {"soap_type", "RadialSpectrum"}, - {"normalize", true}}, - {{"max_radial", 2}, - {"max_angular", 2}, - {"soap_type", "PowerSpectrum"}, - {"normalize", true}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.0}, + {"cutoff_smooth_width", 0.5}}}; + + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 3}, + {"max_angular", 0}, + {"soap_type", "RadialSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}, + {"normalize", true}}, + {{"max_radial", 2}, + {"max_angular", 2}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}, + {"normalize", true}}}; std::vector kernel_hypers{ {{"zeta", 2}, {"target_type", "Structure"}, {"name", "Cosine"}}, diff --git a/tests/test_math_interpolator.hh b/tests/test_math_interpolator.hh index 2b17510e2..59637e857 100644 --- a/tests/test_math_interpolator.hh +++ b/tests/test_math_interpolator.hh @@ -204,13 +204,12 @@ namespace rascal { math::Hyp1f1 hyp1f1{0.5 * (5 + 4 + 3), 4 + 1.5, 200, 1e-15}; const int max_radial{3}; const int max_angular{max_radial - 1}; - const json fc_hypers{{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.5}, {"unit", "AA"}}}}; - const json hypers{ - {"gaussian_density", fc_hypers}, - {"max_radial", max_radial}, - {"max_angular", max_angular}, - {"cutoff_function", {{"cutoff", {{"value", 2.0}, {"unit", "AA"}}}}}}; + + const json hypers{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.5}, + {"max_radial", max_radial}, + {"max_angular", max_angular}, + {"interaction_cutoff", 3.0}}; // a=0.5*(n+l+3), b = l+1.5, mmax, tolerance RadialIntegral_t radial_contr{hypers}; diff --git a/tests/test_pseudo_points.cc b/tests/test_pseudo_points.cc index 911e8b3a5..277ea8064 100644 --- a/tests/test_pseudo_points.cc +++ b/tests/test_pseudo_points.cc @@ -52,9 +52,19 @@ namespace rascal { using SparsePoints_t = SparsePoints; SparsePointsFixture() { - hypers["cutoff_function"] = fc_hypers; - hypers["gaussian_density"] = sigma_hypers; - hypers["radial_contribution"] = {{"type", "GTO"}}; + hypers["cutoff_function_type"] = fc_hypers["cutoff_function_type"]; + hypers["interaction_cutoff"] = fc_hypers["interaction_cutoff"]; + hypers["cutoff_smooth_width"] = fc_hypers["cutoff_smooth_width"]; + if (fc_hypers.count("cutoff_function_parameters")) { + hypers["cutoff_function_parameters"] = + fc_hypers["cutoff_function_parameters"]; + } + + hypers["gaussian_sigma_type"] = sigma_hypers["gaussian_sigma_type"]; + hypers["gaussian_sigma_constant"] = + sigma_hypers["gaussian_sigma_constant"]; + + hypers["radial_contribution"] = {{"radial_basis", "GTO"}}; json ad1a{{"name", "AdaptorNeighbourList"}, {"initialization_arguments", {{"cutoff", cutoff}}}}; @@ -81,11 +91,11 @@ namespace rascal { {"normalize", true}, {"expansion_by_species_method", "environment wise"}}; - json fc_hypers{{"type", "ShiftedCosine"}, - {"cutoff", {{"value", cutoff}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}; - json sigma_hypers{{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}; + json fc_hypers{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", cutoff}, + {"cutoff_smooth_width", 0.5}}; + json sigma_hypers{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}; }; using multiple_fixtures = boost::mpl::list< diff --git a/tests/test_sparse_kernels.hh b/tests/test_sparse_kernels.hh index c9fe85783..4efb9f89e 100644 --- a/tests/test_sparse_kernels.hh +++ b/tests/test_sparse_kernels.hh @@ -51,9 +51,19 @@ namespace rascal { for (auto & fc_hyp : this->fc_hypers) { for (auto & sig_hyp : this->density_hypers) { for (auto & rep_hyp : this->rep_hypers) { - rep_hyp["cutoff_function"] = fc_hyp; - rep_hyp["gaussian_density"] = sig_hyp; - rep_hyp["radial_contribution"] = ri_hyp; + rep_hyp["cutoff_function_type"] = fc_hyp["cutoff_function_type"]; + rep_hyp["interaction_cutoff"] = fc_hyp["interaction_cutoff"]; + rep_hyp["cutoff_smooth_width"] = fc_hyp["cutoff_smooth_width"]; + if (fc_hyp.count("cutoff_function_parameters")) { + rep_hyp["cutoff_function_parameters"] = + fc_hyp["cutoff_function_parameters"]; + } + + rep_hyp["gaussian_sigma_type"] = sig_hyp["gaussian_sigma_type"]; + rep_hyp["gaussian_sigma_constant"] = + sig_hyp["gaussian_sigma_constant"]; + + rep_hyp["radial_basis"] = ri_hyp["radial_basis"]; this->representation_hypers.push_back(rep_hyp); } } @@ -64,23 +74,26 @@ namespace rascal { std::vector representation_hypers{}; - std::vector fc_hypers{ - {{"type", "ShiftedCosine"}, - {"cutoff", {{"value", 2.0}, {"unit", "AA"}}}, - {"smooth_width", {{"value", 0.5}, {"unit", "AA"}}}}}; - - std::vector density_hypers{ - {{"type", "Constant"}, - {"gaussian_sigma", {{"value", 0.4}, {"unit", "AA"}}}}}; - std::vector radial_contribution_hypers{{{"type", "GTO"}}}; - std::vector rep_hypers{{{"max_radial", 3}, - {"max_angular", 0}, - {"soap_type", "RadialSpectrum"}, - {"normalize", true}}, - {{"max_radial", 2}, - {"max_angular", 2}, - {"soap_type", "PowerSpectrum"}, - {"normalize", true}}}; + std::vector fc_hypers{{{"cutoff_function_type", "ShiftedCosine"}, + {"interaction_cutoff", 2.0}, + {"cutoff_smooth_width", 0.5}}}; + + std::vector density_hypers{{{"gaussian_sigma_type", "Constant"}, + {"gaussian_sigma_constant", 0.4}}}; + std::vector radial_contribution_hypers{{{"radial_basis", "GTO"}}}; + std::vector rep_hypers{ + {{"max_radial", 3}, + {"max_angular", 0}, + {"soap_type", "RadialSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}, + {"normalize", true}}, + {{"max_radial", 2}, + {"max_angular", 2}, + {"soap_type", "PowerSpectrum"}, + {"expansion_by_species_method", "environment wise"}, + {"compute_gradients", false}, + {"normalize", true}}}; std::vector kernel_hypers{ {{"zeta", 2}, {"target_type", "Structure"}, {"name", "GAP"}},