def set_fielddata_from_function(field, subfieldname, function, pos_unit_length, normalise=False, scale_factor=1.0): if type(function) != types.FunctionType: raise NmagUserError("Expect function-object but got type '%s'" % type(function)) log.log( 15, "set_fielddata_from_function: Entering, populate " "subfield '%s' from function '%s'" % (subfieldname, function.__name__)) site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field, subfieldname) for pos, site in zip(site_pos, site_ids): # convert position from mesh coordinates to meter pos = [(x * pos_unit_length).in_units_of(SI("m")) for x in pos] # sample function and set field at current site value = function(pos) set_field_at_site(field, subfieldname, site, value, normalise=normalise, scale_factor=scale_factor) log.debug("set_fielddata_from_function: Done.")
def set_fielddata_from_numpyarray_old(field, subfieldname, data, normalise=False, scale_factor=1.0): if type(data) != numpy.ndarray: raise NmagUserError, "Expect numpy array but got '%s'" % str( type(data)) # check that length of data agrees with subfield site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field,subfieldname) # check whether we have the same number of sites if len(site_ids) != len(data): raise NmagUserError("Subfield '%s' has %d sites but the data you " "provided as a numpy array has %d site entries." % (subfieldname, len(site_ids), len(data))) # check whether the 'tensor' for each site has the correct shape for i in range(len(data)): site = site_ids[i] tensor_value = data[i].tolist() set_field_at_site(field, subfieldname, site, tensor_value, scale_factor=scale_factor, normalise=normalise)
def set_fielddata_from_vector(field, subfieldname, data, scale_factor=1.0, normalise=False): # First check that shape of data and shape of this subfield agree: log.debug("set_fielddata_from_vector: setting %s to '%s'." % (_set_what_msg(subfieldname), data)) if True: data = _prepare_set_field(field, subfieldname, data, None, # <- site scale_factor, normalise) exit_status = ocaml.set_field_uniformly(field, subfieldname, data) if exit_status != 0: raise NmagUserError("set_fielddata_from_vector: couldn't set " "subfield '%s'." % subfieldname) else: site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field, subfieldname) for site in site_ids: set_field_at_site(field, subfieldname, site, data, normalise=normalise, scale_factor=scale_factor) log.debug("set_fielddata_from_vector: Done.")
def set_fielddata_from_vector(field, subfieldname, data, scale_factor=1.0, normalise=False): # First check that shape of data and shape of this subfield agree: log.debug("set_fielddata_from_vector: setting %s to '%s'." % (_set_what_msg(subfieldname), data)) if True: data = _prepare_set_field( field, subfieldname, data, None, # <- site scale_factor, normalise) if type(data) == list: pass elif hasattr(data, "__iter__"): data = list(data) else: data = [data] exit_status = ocaml.set_field_uniformly(field, subfieldname, data) if exit_status != 0: raise NmagUserError("set_fielddata_from_vector: couldn't set " "subfield '%s'." % subfieldname) else: site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field, subfieldname) for site in site_ids: set_field_at_site(field, subfieldname, site, data, normalise=normalise, scale_factor=scale_factor) log.debug("set_fielddata_from_vector: Done.")
def set_fielddata_from_numpyarray_old(field, subfieldname, data, normalise=False, scale_factor=1.0): if type(data) != numpy.ndarray: raise NmagUserError,"Expect numpy array but got '%s'" % str(type(data)) # check that length of data agrees with subfield site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field,subfieldname) # check whether we have the same number of sites if len(site_ids) != len(data): raise NmagUserError("Subfield '%s' has %d sites but the data you " "provided as a numpy array has %d site entries." % (subfieldname, len(site_ids), len(data))) # check whether the 'tensor' for each site has the correct shape for i in range(len(data)): site = site_ids[i] tensor_value = data[i].tolist() set_field_at_site(field, subfieldname, site, tensor_value, scale_factor=scale_factor, normalise=normalise)
def set_fielddata_from_function(field, subfieldname, function, pos_unit_length, normalise=False, scale_factor=1.0): if type(function) != types.FunctionType: raise NmagUserError("Expect function-object but got type '%s'" % type(function)) log.log(15, "set_fielddata_from_function: Entering, populate " "subfield '%s' from function '%s'" % (subfieldname, function.__name__)) site_ids, site_pos, shape, site_vols = \ ocaml.mwe_subfield_metadata(field, subfieldname) for pos, site in zip(site_pos, site_ids): # convert position from mesh coordinates to meter pos = [(x*pos_unit_length).in_units_of(SI("m")) for x in pos] # sample function and set field at current site value = function(pos) set_field_at_site(field, subfieldname, site, value, normalise=normalise, scale_factor=scale_factor) log.debug("set_fielddata_from_function: Done.")
def _create_field(self, field_name, subfield_name=None, row=0): # This is the real subfield name full_field_name = build_full_field_name(field_name, subfield_name) # Now we get the data f = self.open_handler() root_data_fields = self.get_root_data_fields() dim = self.mesh.dim field_stuff = f.getNode(root_data_fields, field_name) field_shape = list(field_stuff.row[full_field_name].shape)[1:] # Get the sites where the subfield is defined (sites) and the # corresponding coordinates (ps) and values (vs) ps, vs, sites = self.get_field_array(field_name, subfield_name, row) # sites now contains the dof allocation for the subfield, i.e. in # which sites the subfield is defined. # We now want to build a numarray of one boolean value per each site # which says whether for that site the field is defined or not pointsregions = self.mesh.pointsregions num_sites = len(pointsregions) defined = numpy.ndarray(num_sites, dtype=numpy.bool) defined.fill(False) for site_where_defined in sites: defined[site_where_defined] = True # Run over all the sites and build a list of regions where the field # is undefined all_regions = range(1, self.mesh.numregions) regions_where_defined = range(1, self.mesh.numregions) for site in range(num_sites): if not defined[site]: regions_owning_this_site = pointsregions[site] for region in regions_owning_this_site: if region in regions_where_defined: regions_where_defined.remove(region) # Now we know in which regions of the mesh the field is defined logmsg("'%s' has been found in regions %s" % (full_field_name, regions_where_defined)) # Consistency check: there musn't be a region where the field # is partially defined! logmsg("Now checking that there the field is always present...") for site in range(num_sites): field_should_be_defined_here = \ (True in [rwd in pointsregions[site] for rwd in regions_where_defined]) # ^^^ True when this site belongs to one region which is listed # in 'regions_where_defined' if field_should_be_defined_here != defined[site]: logmsg("Inconsistency while checking the field definition " "regions: site %d belongs to region %s, but the field " "is defined in regions %s." % (site, pointsregions[site], regions_where_defined)) raise NmagUserError("The given file seems to be corrupt!" "Cannot proceed!") logmsg("Check successful: definition regions are %s" % regions_where_defined) logmsg("Creating a new element '%s' for the field" % full_field_name) element = ocaml.make_element(full_field_name, field_shape, dim, self.field_order) logmsg("Creating MWE") element_assoc = zip(regions_where_defined, [element] * len(regions_where_defined)) properties = zip(regions_where_defined, [[full_field_name]] * len(regions_where_defined)) mwe = ocaml.make_mwe(field_name, self.mesh.raw_mesh, element_assoc, [], properties) logmsg("Creating the field") field = ocaml.raw_make_field(mwe, [], "", "") # We now try to understand how to map the data from file into the # newly created field metadata = ocaml.mwe_subfield_metadata(field, full_field_name) new_site_ids, new_pos, new_shape, new_site_vols = metadata num_new_sites = len(new_site_ids) if num_new_sites != len(sites): raise NmagUserError("The re-created field seems inconsistent " "with the one saved to file. Number of " "sites is %d for saved and %d for new field. " "Cannot proceed!" % (num_sites, num_new_sites)) # Check that the new field is binary compatible with the old one # this is really what we expect and allows us to set the field # by just passing a numarray as retrieved from the file need_map = False for i in range(num_new_sites): if new_site_ids[i] != sites[i]: need_map = True if need_map: raise NmagUserError("need_map=True, not implemented because this" "was not expected to happen, anyway! Contact" "Matteo ([email protected])") self.fields[full_field_name] = field timer1.start("x") ff.set_fielddata_from_numpyarray(field, full_field_name, vs) timer1.stop("x") return field
def _create_field(self, field_name, subfield_name=None, row=0): # This is the real subfield name full_field_name = build_full_field_name(field_name, subfield_name) # Now we get the data f = self.open_handler() root_data_fields = self.get_root_data_fields() dim = self.mesh.dim field_stuff = f.getNode(root_data_fields, field_name) field_shape = list(field_stuff.row[full_field_name].shape)[1:] # Get the sites where the subfield is defined (sites) and the # corresponding coordinates (ps) and values (vs) ps, vs, sites = self.get_field_array(field_name, subfield_name, row) # sites now contains the dof allocation for the subfield, i.e. in # which sites the subfield is defined. # We now want to build a numarray of one boolean value per each site # which says whether for that site the field is defined or not pointsregions = self.mesh.pointsregions num_sites = len(pointsregions) defined = numpy.ndarray(num_sites, dtype=numpy.bool) defined.fill(False) for site_where_defined in sites: defined[site_where_defined] = True # Run over all the sites and build a list of regions where the field # is undefined all_regions = range(1, self.mesh.numregions) regions_where_defined = range(1, self.mesh.numregions) for site in range(num_sites): if not defined[site]: regions_owning_this_site = pointsregions[site] for region in regions_owning_this_site: if region in regions_where_defined: regions_where_defined.remove(region) # Now we know in which regions of the mesh the field is defined logmsg("'%s' has been found in regions %s" % (full_field_name, regions_where_defined)) # Consistency check: there musn't be a region where the field # is partially defined! logmsg("Now checking that there the field is always present...") for site in range(num_sites): field_should_be_defined_here = \ (True in [rwd in pointsregions[site] for rwd in regions_where_defined]) # ^^^ True when this site belongs to one region which is listed # in 'regions_where_defined' if field_should_be_defined_here != defined[site]: logmsg("Inconsistency while checking the field definition " "regions: site %d belongs to region %s, but the field " "is defined in regions %s." % (site, pointsregions[site], regions_where_defined)) raise NmagUserError("The given file seems to be corrupt!" "Cannot proceed!") logmsg("Check successful: definition regions are %s" % regions_where_defined) logmsg("Creating a new element '%s' for the field" % full_field_name) element = ocaml.make_element(full_field_name, field_shape, dim, self.field_order) logmsg("Creating MWE") element_assoc = zip(regions_where_defined, [element]*len(regions_where_defined)) properties = zip(regions_where_defined, [[full_field_name]]*len(regions_where_defined)) mwe = ocaml.make_mwe(field_name, self.mesh.raw_mesh, element_assoc, [], properties) logmsg("Creating the field") field = ocaml.raw_make_field(mwe, [], "", "") # We now try to understand how to map the data from file into the # newly created field metadata = ocaml.mwe_subfield_metadata(field, full_field_name) new_site_ids, new_pos, new_shape, new_site_vols = metadata num_new_sites = len(new_site_ids) if num_new_sites != len(sites): raise NmagUserError("The re-created field seems inconsistent " "with the one saved to file. Number of " "sites is %d for saved and %d for new field. " "Cannot proceed!" % (num_sites,num_new_sites)) # Check that the new field is binary compatible with the old one # this is really what we expect and allows us to set the field # by just passing a numarray as retrieved from the file need_map = False for i in range(num_new_sites): if new_site_ids[i] != sites[i]: need_map = True if need_map: raise NmagUserError("need_map=True, not implemented because this" "was not expected to happen, anyway! Contact" "Matteo ([email protected])") self.fields[full_field_name] = field timer1.start("x") ff.set_fielddata_from_numpyarray(field, full_field_name, vs) timer1.stop("x") return field