def test_Poisson_t3(self): from context import spyfe from spyfe.meshing.generators.triangles import t3_ablock from spyfe.meshing.modification import mesh_boundary from spyfe.meshing.selection import connected_nodes from numpy import array from spyfe.materials.mat_heatdiff import MatHeatDiff from spyfe.femms.femm_heatdiff import FEMMHeatDiff from spyfe.fields.nodal_field import NodalField from spyfe.integ_rules import TriRule from spyfe.force_intensity import ForceIntensity from scipy.sparse.linalg import spsolve import time from spyfe.meshing.exporters.vtkexporter import vtkexport start0 = time.time() # These are the constants in the problem, k is kappa boundaryf = lambda x, y: 1.0 + x**2 + 2 * y**2 Q = -6 # internal heat generation rate k = 1.0 # thermal conductivity m = MatHeatDiff(thermal_conductivity=array([[k, 0.0], [0.0, k]])) Dz = 1.0 # thickness of the slice start = time.time() N = 5 Length, Width, nL, nW = 1.0, 1.0, N, N fens, fes = t3_ablock(Length, Width, nL, nW) print('Mesh generation', time.time() - start) bfes = mesh_boundary(fes) cn = connected_nodes(bfes) geom = NodalField(fens=fens) temp = NodalField(nfens=fens.count(), dim=1) for index in cn: temp.set_ebc([index], val=boundaryf(fens.xyz[index, 0], fens.xyz[index, 1])) temp.apply_ebc() temp.numberdofs() femm = FEMMHeatDiff(material=m, fes=fes, integration_rule=TriRule(npts=1)) start = time.time() fi = ForceIntensity(magn=lambda x, J: Q) F = femm.distrib_loads(geom, temp, fi, 3) print('Heat generation load', time.time() - start) start = time.time() F += femm.nz_ebc_loads_conductivity(geom, temp) print('NZ EBC load', time.time() - start) start = time.time() K = femm.conductivity(geom, temp) print('Matrix assembly', time.time() - start) start = time.time() temp.scatter_sysvec(spsolve(K, F)) print('Solution', time.time() - start) print(temp.values) print('Done', time.time() - start0) from numpy.testing import assert_array_almost_equal assert_array_almost_equal(temp.values[-4:-1], array([[3.16], [3.36], [3.64]]))
k = 1.0 # thermal conductivity m = MatHeatDiff(thermal_conductivity=k * numpy.identity(3)) start = time.time() N = 4 xs = numpy.linspace(0.0, 1.0, N + 1) ys = numpy.linspace(0.0, 1.0, N + 1) zs = numpy.linspace(0.0, 1.0, N + 1) fens, fes = h8_blockx(xs, ys, zs) fens, fes = h8_to_h20(fens, fes) fes.gradbfunpar(numpy.array([0, 0, 0])) geom = NodalField(fens=fens) vtkexport("Poisson_fe_H20_mesh", fes, geom) print('Mesh generation', time.time() - start) bfes = mesh_boundary(fes) cn = connected_nodes(bfes) temp = NodalField(nfens=fens.count(), dim=1) for j in cn: temp.set_ebc([j], val=boundaryf(fens.xyz[j, 0], fens.xyz[j, 1], fens.xyz[j, 2])) temp.apply_ebc() femm = FEMMHeatDiff(material=m, fes=fes, integration_rule=GaussRule(dim=3, order=3)) S = femm.connection_matrix(geom) perm = reverse_cuthill_mckee(S, symmetric_mode=True) temp.numberdofs(node_perm=perm) # temp.numberdofs() start = time.time() fi = ForceIntensity(magn=lambda x, J: Q)
tolerance=0.99) top = fe_select(fens, bfes, facing=True, direction=lambda x: numpy.array([0.0, +1.0]), tolerance=0.99) topright = numpy.concatenate((top, right)) print('Mesh generation', time.time() - start) model_data = {} model_data['fens'] = fens model_data['regions'] \ = [{'femm': FEMMHeatDiff(material=m, fes=fes, integration_rule=TriRule(npts=1))} ] model_data['boundary_conditions'] \ = {'essential': [{'node_list': connected_nodes(bfes.subset(bottom)), 'temperature': lambda x: 100.0 }], 'surface_transfer': [{'femm': FEMMHeatDiffSurface(fes=bfes.subset(topright), integration_rule=GaussRule(dim=1, order=1), surface_transfer_coeff=lambda x: 750.0), 'ambient_temperature': lambda x: 0.0 }] } # Call the solver steady_state(model_data) print(model_data['timings']) nodeA = fenode_select(fens, box=bounding_box([Width, Heightb]), inflate=Width / 1e6)
k = 1.0 # thermal conductivity m = MatHeatDiff(thermal_conductivity=numpy.array([[k, 0.0], [0.0, k]])) start = time.time() N = 50 Length, Width, nL, nW = 1.0, 1.0, N, N fens, fes = t3_ablock(Length, Width, nL, nW) fes.other_dimension = lambda conn, N, x: 1.0 print('Mesh generation', time.time() - start) model_data = {} model_data['fens'] = fens model_data['regions'] = [{ 'femm': FEMMHeatDiff(material=m, fes=fes, integration_rule=TriRule(npts=1)), 'heat_generation': ForceIntensity(magn=lambda x, J: -6.0) }] bfes = mesh_boundary(fes) model_data['boundary_conditions'] = { 'essential': [{ 'node_list': connected_nodes(bfes), 'value': lambda x: 1.0 + x[0]**2 + 2 * x[1]**2 }] } # Call the solver steady_state(model_data) for action, time in model_data['timings']: print(action, time, ' sec') plot_temperature(model_data)
def steady_state(model_data): """Steady-state heat conduction solver. :param model_data: Model data dictionary. model_data['fens'] = finite element node set (mandatory) For each region (connected piece of the domain made of a particular material), mandatory: model_data['regions']= list of dictionaries, one for each region Each region: region['heat_generation'] = material internal heat generation rate (optional), function region['femm'] = finite element set that covers the region (mandatory) :return: Success? True or false. The model_data object is modified. model_data['geom'] =the nodal field that is the geometry model_data['temp'] =the nodal field that is the computed temperature model_data['timings'] = timing of the individual operations """ # For essential boundary conditions (optional): # model_data.boundary_conditions.essential = cell array of struct, # each piece of surface with essential boundary condition gets one # element of the array with a struct with the attributes # essential.temperature=fixed (prescribed) temperature (scalar), or # handle to a function with signature # function T =f(x) # essential.fes = finite element set on the boundary to which # the condition applies # or alternatively # essential.node_list = list of nodes on the boundary to which # the condition applies # Only one of essential.fes and essential.node_list needs a given. # # For convection boundary conditions (optional): # model_data.boundary_conditions.convection = cell array of struct, # each piece of surface with convection boundary condition gets one # element of the array with a struct with the attributes # convection.ambient_temperature=ambient temperature (scalar) # convection.surface_transfer_coefficient = surface heat transfer coefficient # convection.fes = finite element set on the boundary to which # the condition applies # convection.integration_rule= integration rule # # For flux boundary conditions (optional): # model_data.boundary_conditions.flux = cell array of struct, # each piece of surface with flux boundary condition gets one # element of the array with a struct with the attributes # flux.normal_flux= normal flux component, positive when outward-bound (scalar) # flux.fes = finite element set on the boundary to which # the condition applies # flux.integration_rule= integration rule # # Control parameters: # model_data.renumber = true or false flag (default is true) # model_data.renumbering_method = optionally choose the renumbering # method (symrcm or symamd) # renumber = ~true; # Should we renumber? # if (isfield(model_data,'renumber')) # renumber =model_data.renumber; # end # Renumbering_options =struct( [] ); # # # Should we renumber the nodes to minimize the cost of the solution of # # the coupled linear algebraic equations? # if (renumber) # renumbering_method = 'symamd'; # default choice # if ( isfield(model_data,'renumbering_method')) # renumbering_method =model_data.renumbering_method;; # end # # Run the renumbering algorithm # model_data =renumber_mesh(model_data, renumbering_method);; # # Save the renumbering (permutation of the nodes) # clear Renumbering_options; Renumbering_options.node_perm =model_data.node_perm; # end timings = [] task = 'Preliminaries' start = time.time() # Extract the nodes fens = model_data['fens'] # Construct the geometry field geom = NodalField(fens=fens) # Construct the temperature field temp = NodalField(dim=1, nfens=geom.nfens) # Apply the essential boundary conditions on the temperature field if 'boundary_conditions' in model_data: if 'essential' in model_data['boundary_conditions']: ebcs = model_data['boundary_conditions']['essential'] for ebc in ebcs: fenids = ebc['node_list'] if 'temperature' not in ebc: value = lambda xyz: 0.0 else: value = ebc['temperature'] for index in fenids: temp.set_ebc([index], val=value(fens.xyz[index, :])) temp.apply_ebc() # Number the equations temp.numberdofs() timings.append((task, time.time() - start)) # Initialize the heat loads vector F = numpy.zeros((temp.nfreedofs, )) # # Flux boundary condition: # if (isfield(model_data.boundary_conditions, 'flux' )) # for j=1:length(model_data.boundary_conditions.flux) # flux =model_data.boundary_conditions.flux{j}; # flux.femm = femm_heat_diffusion (struct (... # 'fes',flux.fes,... # 'integration_rule',flux.integration_rule)); # model_data.boundary_conditions.flux{j}=flux; # end # clear flux fi femm # end # task = 'Conductivity matrix' start = time.time() # Construct the system conductivity matrix K = csr_matrix( (temp.nfreedofs, temp.nfreedofs)) # (all zeros, for the moment) for region in model_data['regions']: # Add up all the conductivity matrices for all the regions K += region['femm'].conductivity(geom, temp) timings.append((task, time.time() - start)) task = 'Heat generation load' start = time.time() for region in model_data['regions']: Q = None # default is no internal heat generation rate if 'heat_generation' in region: # Was it defined? Q = region['heat_generation'] if Q is not None: # If it was supplied, and it is nonzero, compute its contribution. F = F + region['femm'].distrib_loads(geom, temp, Q, 3) timings.append((task, time.time() - start)) task = 'NZEBC load' start = time.time() if 'boundary_conditions' in model_data: if 'essential' in model_data['boundary_conditions']: for region in model_data['regions']: # Loads due to the essential boundary conditions on the temperature field F += region['femm'].nz_ebc_loads_conductivity(geom, temp) timings.append((task, time.time() - start)) # Convection boundary conditions: if 'boundary_conditions' in model_data: if 'surface_transfer' in model_data['boundary_conditions']: amb = NodalField( dim=1, nfens=geom.nfens) # create the ambient temperature field sbcs = model_data['boundary_conditions']['surface_transfer'] for sbc in sbcs: if 'ambient_temperature' not in sbc: ambient_temperature = lambda xyz: 0.0 else: ambient_temperature = sbc['ambient_temperature'] femm = sbc['femm'] for index in connected_nodes(femm.fes): amb.set_ebc([index], val=ambient_temperature(fens.xyz[index, :])) amb.apply_ebc() for sbc in sbcs: femm = sbc['femm'] K += femm.surface_transfer(geom, temp) F += femm.surface_transfer_loads(geom, temp, amb) F += femm.nz_ebc_loads_surface_transfer(geom, temp) # # Process the flux boundary condition # if (isfield(model_data.boundary_conditions, 'flux' )) # for j=1:length(model_data.boundary_conditions.flux) # flux =model_data.boundary_conditions.flux{j}; # fi= force_intensity(struct('magn',flux.normal_flux)); # # Note the sign which reflects the formula (negative sign # # in front of the integral) # F = F - distrib_loads(flux.femm, sysvec_assembler, geom, temp, fi, 2); # end # clear flux fi # end task = 'System solution' start = time.time() # Solve for the temperatures temp.scatter_sysvec(spsolve(K, F)) timings.append((task, time.time() - start)) # Update the model data model_data['geom'] = geom model_data['temp'] = temp model_data['timings'] = timings return True