def _prepare_set_field(field, subfieldname, data, site=None, scale_factor=1.0, normalise=False, check_site=False): """ Internal function used to make some sanity checks before setting a field. """ # 1-2 check size and shape of field _check_field(field, subfieldname, data, site) # 3 is the data of the right type --> done by ocaml # 4 check that the site type is a list if check_site and type(site) != types.ListType: raise NmagUserError("'site' has to be a list of integers. For " "first order basis functions, there is only " "one integer in the list, the node id.") # Now we can actually set the data. # Do we need to normalise? if normalise: data = normalised_vector(data) # Do we need to apply the scale factor if type(data) in [types.IntType, types.FloatType, types.BooleanType]: data *= float(scale_factor) else: # FIXME: this doesn't work with tensors! data = map(lambda x : x*float(scale_factor), data) return data
def _prepare_set_field(field, subfieldname, data, site=None, scale_factor=1.0, normalise=False, check_site=False): """ Internal function used to make some sanity checks before setting a field. """ # We do a number of sanity checks for which we need some metadata list_of_subfield_and_shape_tuples = nfem.data_doftypes(field) # Gets all the shapes mapped by the subfield names shape_by_subfieldname = {} for sfname, shape in list_of_subfield_and_shape_tuples: shape_by_subfieldname[sfname] = shape # 1 Does the field have this subfield: if not subfieldname in shape_by_subfieldname.keys(): subfields = shape_by_subfieldname.keys() msg = ("You want to set %s but I have only found subfield(s) '%s' " "in the field. By the way, your data is '%s'." % (_set_what_msg(subfieldname, site), subfields, data)) raise NmagUserError(msg) # 2 Is the shape of the data right? shape = list(numpy.array(data).shape) # quite a silly way to get the # shape, but it works! if shape != shape_by_subfieldname[subfieldname]: msg = ("When trying to set %s we have a shape mismatch. The shape of " "the subfield data is '%s' but the shape of your data is '%s'." "By the way, your data is '%s'." % (_set_what_msg(subfieldname, site), shape_by_subfieldname[subfieldname], shape, data)) raise NmagUserError(msg) # 3 is the data of the right type --> done by ocaml # 4 check that the site type is a list if check_site and type(site) != types.ListType: raise NmagUserError("'site' has to be a list of integers. For " "first order basis functions, there is only " "one integer in the list, the node id.") # Now we can actually set the data. # Do we need to normalise? if normalise: data = normalised_vector(data) # Do we need to apply the scale factor if type(data) in [types.IntType, types.FloatType, types.BooleanType]: data *= float(scale_factor) else: # FIXME: this doesn't work with tensors! data = map(lambda x : x*float(scale_factor), data) return data
def _prepare_set_field(field, subfieldname, data, site=None, scale_factor=1.0, normalise=False, check_site=False): """ Internal function used to make some sanity checks before setting a field. """ # 1-2 check size and shape of field _check_field(field, subfieldname, data, site) # 3 is the data of the right type --> done by ocaml # 4 check that the site type is a list if check_site and type(site) != types.ListType: raise NmagUserError("'site' has to be a list of integers. For " "first order basis functions, there is only " "one integer in the list, the node id.") # Now we can actually set the data. # Do we need to normalise? if normalise: data = normalised_vector(data) # Do we need to apply the scale factor if type(data) in [types.IntType, types.FloatType, types.BooleanType]: data *= float(scale_factor) else: # FIXME: this doesn't work with tensors! data = map(lambda x: x * float(scale_factor), data) return data
def _prepare_set_field(field, subfieldname, data, site=None, scale_factor=1.0, normalise=False, check_site=False): """ Internal function used to make some sanity checks before setting a field. """ # We do a number of sanity checks for which we need some metadata list_of_subfield_and_shape_tuples = nfem.data_doftypes(field) # Gets all the shapes mapped by the subfield names shape_by_subfieldname = {} for sfname, shape in list_of_subfield_and_shape_tuples: shape_by_subfieldname[sfname] = shape # 1 Does the field have this subfield: if not subfieldname in shape_by_subfieldname.keys(): subfields = shape_by_subfieldname.keys() msg = ("You want to set %s but I have only found subfield(s) '%s' " "in the field. By the way, your data is '%s'." % (_set_what_msg(subfieldname, site), subfields, data)) raise NmagUserError(msg) # 2 Is the shape of the data right? shape = list(numpy.array(data).shape) # quite a silly way to get the # shape, but it works! if shape != shape_by_subfieldname[subfieldname]: msg = ("When trying to set %s we have a shape mismatch. The shape of " "the subfield data is '%s' but the shape of your data is '%s'." "By the way, your data is '%s'." % (_set_what_msg(subfieldname, site), shape_by_subfieldname[subfieldname], shape, data)) raise NmagUserError(msg) # 3 is the data of the right type --> done by ocaml # 4 check that the site type is a list if check_site and type(site) != types.ListType: raise NmagUserError("'site' has to be a list of integers. For " "first order basis functions, there is only " "one integer in the list, the node id.") # Now we can actually set the data. # Do we need to normalise? if normalise: data = normalised_vector(data) # Do we need to apply the scale factor if type(data) in [types.IntType, types.FloatType, types.BooleanType]: data *= float(scale_factor) else: # FIXME: this doesn't work with tensors! data = map(lambda x: x * float(scale_factor), data) return data