def test_gmsh_1_0_import_gzipped():
    # Work out in which directory the data files are
    org_dir = os.getcwd()
    os.chdir(os.path.split(__file__)[0])

    os.system('make sphere2.nmesh >test_stdoutstderr3.log 2>&1')

    import nmesh
    mesh = nmesh.load('sphere2.nmesh')

    assert len(mesh.points) == 258, "Something went wrong with nmeshimport --gmesh of gzipped mesh"

    os.chdir(org_dir)
Exemple #2
0
plot "%s"\n """ % (
        filename,
        tmpfilename1,
    )
    f.write(gnuplot_header)
    f.close()

    os.system("gnuplot %s" % tmpfilename2)
    os.remove(tmpfilename1)
    os.remove(tmpfilename2)


import nmesh

print "About to load mesh from file", time.asctime()
mesh = nmesh.load("run_simple4d/simple4d.nmesh")


print "About to convert mesh to lists", time.asctime()
lists = mesh.tolists()

print "Finished converting mesh to lists", time.asctime()

points = lists[0][2]

# extract 2d subsets

print "About to extract projects into 2d planes"
x01 = map(lambda x: [x[0], x[1]], points)
x12 = map(lambda x: [x[1], x[2]], points)
x23 = map(lambda x: [x[2], x[3]], points)
Exemple #3
0
import mayavi, sys, Numeric, nmesh
filename = sys.argv[1]
if filename[-5:] != "nmesh" : raise ValueError("The file is not a nmesh file.")
sys.argv=["",""]     

# define a few globals, so that the interval function can use them
globals()['v'] = mayavi.mayavi()
globals()['intervals'] = Numeric.arange(0,1.05,0.05)
globals()['call_counter'] = 0

the_mesh = nmesh.load(filename)
mesh_info = the_mesh.tolists()

# to visualise without writing to disk, uncomment this section
vtkData, points, simplices, simplexIndicies, icradii, ccradii = nmesh.visual.mesh2vtk(mesh_info, VTKonly=False,body_numbers=True)

in2circ = nmesh.visual.findRatios(icradii, ccradii)#, factor=3) #2D mesh

# order the mesh by this metric
mesh_info= nmesh.visual.order_mesh(mesh_info, data=in2circ)

globals()['v'].close_all()
# display this information and open the ExtractUnstructuredGrid filter
nmesh.visual.solid_in2circ(mesh_info, myv = globals()['v'])
m2 = v.load_module('SurfaceMap',0)
m2.actor.GetProperty().SetRepresentationToWireframe()
m2.mapper.SetScalarVisibility(0)
m2.renwin.Render()
dvm = v.get_current_dvm()
mm = dvm.get_current_module_mgr()
luthandler = mm.get_scalar_lut_handler()
Exemple #4
0
def tic():
    global starttime
    starttime = time.time()

def toc():
    runtime = time.time()-starttime
    print "--> %5.2f seconds" % runtime,
    return runtime




if os.path.exists("test.nmesh"):
    print "%40s" % "loading mesh",; tic();
    mesh = nmesh.load("test.nmesh")
    toc()
else:
    mesh = create_mesh()







import ocaml

#total_time:
tt = 0.
def test_gmsh_1_0_import():
    #py.test will pass on any command line arguments to the code it tests.
    #This will confuse nmag (as it doesn't know how to handle py.test's '-k'
    #and '--verbose' switches, etc.
    #
    #We thus manually delete all other entries that the name of the executable
    #from sys.argv.
    import sys

    proper_sysargv = sys.argv
    sys.argv = sys.argv[0:1]

    # work out in which directory the data files are
    org_dir = os.getcwd()
    os.chdir(os.path.split(__file__)[0])

    # Compute new data (this can take a while, will delete old data files
    # automatically)
    os.system("rm -fv sphere.nmesh &> test_stdoutstderr1.log")

    # Remove this line as it depends on having gmsh installed
    # os.system("make sphere.msh >> test_make_run1.out")
    #
    f = open("sphere.msh", "r")
    assert f.readline()[0:4] == "$NOD", \
      "gmsh file 'sphere.msh' has not been written correctly"
    f.close()

    os.system("make sphere.nmesh 2>&1 >test_stdoutstderr2.log")
    import nmesh

    mesh = nmesh.load("sphere.nmesh")

    n, expect = (len(mesh.points), 258)
    assert n == expect, ("Number of nodes has changed. "
                         "Expected %d, but got %d" % (expect,n))

    n, expect = (len(mesh.simplices), 677)
    assert n == expect, ("Number of simplices has changed. "
                         "Expected %d, but got %d" % (expect, n))

    n, expect = (len(mesh.simplicesregions), 677)
    assert n == expect, ("Number of simplicesregions has changed. "
                         "Expected %d, but got %d" % (expect, n))


    # For the number of links, we have seen 1687 and 1689. This seems 
    # to be non-deterministic behaviour from parmetis. We thus allow
    # a range of links.
    n, expect = (len(mesh.links), "1670-1700 (1687 and 1689 have been seen)")
    assert 1670 <= n <= 1700, ("Number of links has changed. "
                               "Expected %s, but got %d" % (expect, n))

    n, expect = (mesh.dim, 3)
    assert n == expect, ("Number of dim has changed. "
                         "Expected %d, but got %d" % (expect, n))

    n, expect = (len(mesh.periodicpointindices), 0)
    assert n == expect, ("Number of periodicpointsindices has changed. "
                         "Expected %d, but got %d" % (expect, n))

    n, expect = (mesh.numregions, 2)
    assert n == expect, ("Number of regions has changed. "
                         "Expected %d, but got %d" % (expect, n))

    n, expect = (max(max(mesh.pointsregions)), 1)
    assert n == expect, ("max(max(pointregions)) has changed. "
                         "Expected %d, but got %d" % (expect, n))

    n, expect = (min(min(mesh.pointsregions)), -1)
    assert n == expect, ("min(min(pointregions)) has changed. "
                         "Expected %d, but got %d" % (expect, n))


    # This avoids getting relative paths in the checking progress output
    os.chdir(org_dir)
Exemple #6
0
                                   jacobi_plans=lam_jacobi.values(),
                                   programs=lam_programs.values())

    return (lam, master_mwes_and_fields_by_name)


### Parameters (note that this all is quite ad-hoc and preliminary here):

mesh_filename = "H.nmesh.h5"
#jacobi_same_nonzero_pattern=True # Does not work yet!
jacobi_same_nonzero_pattern = False
max_order = 2
krylov_max = 100
max_it = 1000000

my_mesh = nmesh.load(mesh_filename)
(lam, master_mwes_and_fields_by_name) = jT_lam(
    mesh=my_mesh,
    sigma_el=su.of(sigma),  # electrical conductivity
    sigma_th=su.of(k),  # thermal conductivity
    c_heat=su.of(c_heat),  # heat capacity
    T_initial=su.of(T_initial),
)

(mwe_T, field_T) = master_mwes_and_fields_by_name['T']

sundialsbuffer_initial = ocaml.raw_make_field(mwe_T, [], "",
                                              "sundials_initial")
sundialsbuffer_final = ocaml.raw_make_field(mwe_T, [], "", "sundials_final")
sundialsbuffer_starting = ocaml.raw_make_field(mwe_T, [], "", "sundials_work")
Exemple #7
0
def tic():
    global starttime
    starttime = time.time()


def toc():
    runtime = time.time() - starttime
    print "--> %5.2f seconds" % runtime,
    return runtime


if os.path.exists("test.nmesh"):
    print "%40s" % "loading mesh",
    tic()
    mesh = nmesh.load("test.nmesh")
    toc()
else:
    mesh = create_mesh()

import ocaml

#total_time:
tt = 0.

print "%40s" % "grow_bookkeeping_data",
tic()
ocaml.mesh_grow_bookkeeping_data_all
tt += toc()
print "(cum: %5.2f)" % tt
print "%40s" % "getting points",
sigma0_Py = 670.
sigma0_Au = 100000.
alpha = 0.035

global magn_dict

print
"""
** meshing a square ring & solving the laplace equation
   for a space-dependent resistivity that depends on outer parameters
**
"""

##### Loading the mesh #####
the_mesh = nmesh.load("corrected-square-ring.nmesh")
nfem2.set_default_mesh(the_mesh)

##### Loading the mesh node positions #####
meshinfo = the_mesh.tolists()

##print "#######################################"
##print "##           ALL NODES               ##"
##print "#######################################"
##for n in range(len(meshinfo[0][2])):
##    print n, meshinfo[0][2][n]

[
    meshinfo_ring, meshinfo_contact7, meshinfo_contact8, meshinfo_contact9,
    meshinfo_contact10, meshinfo_contact11, meshinfo_contact12
] = nmesh.visual.separate_parts(meshinfo,
half_voltage = 0.075
alpha = 0.035

global m

print
"""
** Example: meshing four rings & solving the laplace equation
   for a space-dependent resistivity that depends on outer parameters

   (Presumably, we will encounter bugs at our first try)
**
"""

##### Loading the mesh #####
the_mesh = nmesh.load("ring.nmesh")

meshinfo = nmesh.tolists(the_mesh)
tmp = meshinfo[2][2]
nfem2.set_default_mesh(the_mesh)

##### Loading the mesh node positions #####
meshinfo = the_mesh.tolists()
node_position = meshinfo[0][2]
#print initial_magn

##### Making the elements... #####

# conductivity (scalar)
element_sigma = nfem2.make_element("sigma", [])
element_drho_by_dt = nfem2.make_element("drho_by_dt", [])
half_voltage = 0.075
alpha = 0.035

global m

print
"""
** Example: meshing four rings & solving the laplace equation
   for a space-dependent resistivity that depends on outer parameters

   (Presumably, we will encounter bugs at our first try)
**
"""

##### Loading the mesh #####
the_mesh = nmesh.load("ring.nmesh")

meshinfo = nmesh.tolists(the_mesh)
tmp = meshinfo[2][2]
nfem2.set_default_mesh(the_mesh)

##### Loading the mesh node positions #####
meshinfo = the_mesh.tolists()
node_position = meshinfo[0][2]
# print initial_magn


##### Making the elements... #####

# conductivity (scalar)
element_sigma = nfem2.make_element("sigma", [])
Exemple #11
0
import nmesh

ellipsoid = nmesh.ellipsoid([1.25, 0.5])

bbox = [[-1.25, -0.75], [10.25, 0.75]]

#create a mesh to be saved and then reloaded with another name
mesh2save = nmesh.mesh(objects=[ellipsoid], bounding_box=bbox, a0=0.2)

# save the mesh
mesh2save.save("saved4hint.nmesh")

# reload the mesh with another name
loaded_mesh = nmesh.load("saved4hint.nmesh")

# define a new object
square = nmesh.box([-1.25, -0.75], [1.25, 0.75],
                   transform=[("shift", [5.0, 0.0])])

# first we create a mesh out of the simplices of the loaded mesh which satisfy the
# boundary conditions related to the ellipsoid object. If we
# define some other object, the mesher will triangulate only the
# simplices of the loaded mesh which are within the new boundary conditions.
# the square is meshed after all the loaded_objects are meshed
mesh2 = nmesh.mesh(objects=[square],
                   hints=[(loaded_mesh, ellipsoid)],
                   bounding_box=bbox,
                   a0=0.2)

mesh2.save("hint.nmesh")
Exemple #12
0
import nmesh

ellipsoid = nmesh.ellipsoid([1.25,0.5])

bbox = [[-1.25,-0.75],[10.25,0.75]]

#create a mesh to be saved and then reloaded with another name
mesh2save = nmesh.mesh(objects = [ellipsoid] ,bounding_box=bbox,a0=0.2)

# save the mesh 
mesh2save.save("saved4hint.nmesh")

# reload the mesh with another name
loaded_mesh=nmesh.load("saved4hint.nmesh")

# define a new object
square = nmesh.box([-1.25,-0.75],[1.25,0.75], transform=[("shift",[5.0,0.0])]) 

# first we create a mesh out of the simplices of the loaded mesh which satisfy the
# boundary conditions related to the ellipsoid object. If we
# define some other object, the mesher will triangulate only the
# simplices of the loaded mesh which are within the new boundary conditions.
# the square is meshed after all the loaded_objects are meshed
mesh2 = nmesh.mesh(objects = [square], hints=[(loaded_mesh,ellipsoid)] ,bounding_box=bbox,a0=0.2)

mesh2.save("hint.nmesh")
Exemple #13
0
import nmesh


# Need to run save_load.py first to create the data for this example

#load mesh from file
mesh_loaded = nmesh.load("save_load.nmesh")

#and save mesh again 
nmesh.save(mesh_loaded,"test.nmesh")



Exemple #14
0
sigma0_Py=670.
sigma0_Au=100000.
alpha=0.035

global magn_dict

print
"""
** meshing a square ring & solving the laplace equation
   for a space-dependent resistivity that depends on outer parameters
**
"""

##### Loading the mesh #####
the_mesh = nmesh.load("corrected-square-ring.nmesh")
nfem2.set_default_mesh(the_mesh)

##### Loading the mesh node positions #####
meshinfo = the_mesh.tolists()

##print "#######################################"
##print "##           ALL NODES               ##"
##print "#######################################"
##for n in range(len(meshinfo[0][2])):
##    print n, meshinfo[0][2][n]

[meshinfo_ring,
 meshinfo_contact7,
 meshinfo_contact8,
 meshinfo_contact9,
Exemple #15
0
        jacobi_plans=lam_jacobi.values(),
        programs=lam_programs.values()
        )

    return (lam,master_mwes_and_fields_by_name)

### Parameters (note that this all is quite ad-hoc and preliminary here):

mesh_filename="H.nmesh.h5"
#jacobi_same_nonzero_pattern=True # Does not work yet!
jacobi_same_nonzero_pattern=False
max_order=2
krylov_max=100
max_it=1000000

my_mesh=nmesh.load(mesh_filename)
(lam,master_mwes_and_fields_by_name) = jT_lam(mesh=my_mesh,
                                              sigma_el=su.of(sigma), # electrical conductivity
                                              sigma_th=su.of(k), # thermal conductivity
                                              c_heat=su.of(c_heat),# heat capacity
                                              T_initial=su.of(T_initial),
)

(mwe_T,field_T) = master_mwes_and_fields_by_name['T']

sundialsbuffer_initial = ocaml.raw_make_field(mwe_T,[],"","sundials_initial")
sundialsbuffer_final = ocaml.raw_make_field(mwe_T,[],"","sundials_final")
sundialsbuffer_starting = ocaml.raw_make_field(mwe_T,[],"","sundials_work")

ocaml.field_copy_into(field_T, sundialsbuffer_initial)
ocaml.field_copy_into(field_T, sundialsbuffer_starting)
Exemple #16
0
import nmesh

# create square
box = nmesh.box([0.0, 0.0], [1.0, 1.0])

# crete cone
cone = nmesh.conic([3.0, 0.0], 1.0, [3.0, 4.0], 0.0)

bbox = [[0, 0], [4, 4]]

# create mesh
mesh = nmesh.mesh(bounding_box=bbox, objects=[box, cone], a0=0.4)

# save mesh in current directory
mesh.save("save_load.nmesh", directory=".")

# load mesh from file
mesh_loaded = nmesh.load("save_load.nmesh")

# plot loaded mesh
nmesh.visual.plot2d_ps(mesh_loaded, "save_load.ps")
Exemple #17
0
%range i:3, j:3, k:3, p:3, q:3;
dmdt(i) <- (-gamma_GG/(1 + alpha*alpha))*(eps(i,j,k)*m(j)*H_tot(k) +
           alpha*eps(i,j,k)*m(j)*eps(k,p,q)*m(p)*H_tot(q)) +
           norm_coeff*(1.0 - m(j)*m(j))*m(i);""")
"""+
           zl1 * eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * dm_dcurrent(q) +
           zl2 * eps(i,j,k) * m(j) * dm_dcurrent(k)"""

# Equation for the Jacobian: we omit the third term on the RHS
llg_jacobi = Equation("llg", """
%range i:3, j:3, k:3, p:3, q:3;
dmdt(i) <- (-gamma_GG/(1 + alpha*alpha))*(eps(i,j,k)*m(j)*H_tot(k) +
           alpha*eps(i,j,k)*m(j)*eps(k,p,q)*m(p)*H_tot(q));""")

# Put everything together in a physical model
mesh = nmesh.load("mesh.nmesh.h5")
region_materials = [[], ["Py"]]
p = Model("mumag", mesh, 1e-9, region_materials)

p.add_quantity([alpha, gamma_GG, norm_coeff, m, M_sat, H_ext, H_tot, dmdt])
p.add_computation([llg, eq_H_tot])

op_exch = add_exchange(p)
add_demag(p)

# Timestepper
ts = Timestepper("ts_llg", x='m', dxdt='dmdt',
                 eq_for_jacobian=llg_jacobi, derivatives=[(H_tot, op_exch)])
p.add_timestepper(ts)
p.build()
Exemple #18
0
def test_nmeshmirror():
    os.chdir(os.path.split(__file__)[0])

    #define my non-periodic mesh

    points = [[0,0],[1,0],[0,1],[1,1]]

    simplices = [[0,2,3],[0,1,3]]

    regions=[1,1]

    import nmesh

    mesh = nmesh.mesh_from_points_and_simplices(points,simplices,regions,[],0,False)

    mesh.save('org.nmesh')

    import nmesh.visual
    nmesh.visual.plot2d_ps(mesh,'org.ps')

    # mirror along the x axis (horizontal direction)
    command = "%s org.nmesh 1e-6 1e-6 1,0 mirror1.nmesh" % nmeshmirror_exec

    status = os.system(command)

    assert status==0, "after command '%s' status is %s" % (command,status)

    periodicmesh = nmesh.load('mirror1.nmesh')

    nmesh.visual.plot2d_ps(periodicmesh,'mirror1.ps')

    for i in range(4):
        assert mesh.points[i] == periodicmesh.points[i], \
            "org/mirror1, point %i differs: %s %s" % \
            (i,mesh.points[i],periodicmesh.points[i])

    assert [2.0, 0.0] in periodicmesh.points," [2.0, 0.0] not in periodic points"
    assert [2.0, 1.0] in periodicmesh.points," [2.0, 1.0] not in periodic points"

    command = "%s org.nmesh 1e-6 1e-6 -1,0 mirror2.nmesh" % nmeshmirror_exec

    status = os.system(command)

    assert status==0, "after command '%s' status is %s" % (command,status)

    periodicmesh = nmesh.load('mirror2.nmesh')

    nmesh.visual.plot2d_ps(periodicmesh,'mirror2.ps')

    for i in range(4):
        assert mesh.points[i] == periodicmesh.points[i], \
            "org/mirror2, point %i differs: %s %s" % \
            (i,mesh.points[i],periodicmesh.points[i])

    assert [-1.0, 0.0] in periodicmesh.points," [-1.0, 0.0] not in periodic points"
    assert [-1.0, 1.0] in periodicmesh.points," [-1.0, 1.0] not in periodic points"

    # mirror along the y axis (vertical direction)
    command = "%s org.nmesh 1e-6 1e-6 0,1 mirror3.nmesh" % nmeshmirror_exec

    status = os.system(command)

    assert status==0, "after command '%s' status is %s" % (command,status)

    periodicmesh = nmesh.load('mirror3.nmesh')

    nmesh.visual.plot2d_ps(periodicmesh,'mirror3.ps')

    for i in range(4):
        assert mesh.points[i] == periodicmesh.points[i], \
            "org/mirror3, point %i differs: %s %s" % \
            (i,mesh.points[i],periodicmesh.points[i])

    assert [0.0, 2.0] in periodicmesh.points," [0.0, 2.0] not in periodic points"
    assert [1.0, 2.0] in periodicmesh.points," [1.0, 2.0] not in periodic points"


    # mirror along the x and y axis (horizontal and vertical directions)
    command = "%s org.nmesh 1e-6 1e-6 1,1 mirror4.nmesh" % nmeshmirror_exec

    status = os.system(command)

    assert status==0, "after command '%s' status is %s" % (command,status)

    periodicmesh = nmesh.load('mirror4.nmesh')

    nmesh.visual.plot2d_ps(periodicmesh,'mirror4.ps')

    for i in range(4):
        assert mesh.points[i] == periodicmesh.points[i], \
            "org/mirror4, point %i differs: %s %s" % \
            (i,mesh.points[i],periodicmesh.points[i])

    assert [2.0, 0.0] in periodicmesh.points," [2.0, 0.0] not in periodic points"
    assert [2.0, 1.0] in periodicmesh.points," [2.0, 1.0] not in periodic points"
    assert [0.0, 2.0] in periodicmesh.points," [0.0, 2.0] not in periodic points"
    assert [1.0, 2.0] in periodicmesh.points," [1.0, 2.0] not in periodic points"



    os.chdir(org_dir)
import nmesh

import os.path,sys

filename="ellipsoid_array3d.nmesh"
if not os.path.exists(filename):
    print "You need to run ellipsoid_array3d.py first to compute %s" % filename
    sys.exit(1)

mesh = nmesh.load(filename)

mymayavi = nmesh.visual.show_surfaces_mayavi(mesh)

nmesh.visual.export_visualisation(mymayavi,'ellipsoid_array3d.eps')

import time
print "Will sleep 3 seconds"
time.sleep(3)
Exemple #20
0
    f = open(tmpfilename2, 'w')
    gnuplot_header = """set terminal postscript eps noenhanced monochrome blacktext dashed dashlength 1.0 linewidth 1.0 defaultplex palfuncparam 2000,0.003 butt "Helvetica" 14
set output '%s'
plot "%s"\n """ % (filename, tmpfilename1)
    f.write(gnuplot_header)
    f.close()

    os.system("gnuplot %s" % tmpfilename2)
    os.remove(tmpfilename1)
    os.remove(tmpfilename2)


import nmesh

print "About to load mesh from file", time.asctime()
mesh = nmesh.load("run_simple4d/simple4d.nmesh")

print "About to convert mesh to lists", time.asctime()
lists = mesh.tolists()

print "Finished converting mesh to lists", time.asctime()

points = lists[0][2]

#extract 2d subsets

print "About to extract projects into 2d planes"
x01 = map(lambda x: [x[0], x[1]], points)
x12 = map(lambda x: [x[1], x[2]], points)
x23 = map(lambda x: [x[2], x[3]], points)
x30 = map(lambda x: [x[3], x[0]], points)