def set_field_data(self, field_name, subfield_name=None, row=0): full_field_name = build_full_field_name(field_name, subfield_name) if self.fields.has_key(full_field_name): logmsg("found already generated field '%s': setting this" % full_field_name) field = self.fields[full_field_name] f = self.open_handler() ps, vs, sites = \ self.get_field_array(field_name, subfield_name, row) timer1.start("x") ff.set_fielddata_from_numpyarray(field, full_field_name, vs) timer1.stop("x") return field else: return self._create_field(field_name, subfield_name=subfield_name, row=row)
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