Changeset 24275


Ignore:
Timestamp:
10/25/19 02:44:52 (5 years ago)
Author:
bdef
Message:

NEW: adding some user required options to eportvtk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/defleurian/paraview/exportVTK.py

    r24269 r24275  
    44
    55
    6 def exportVTK(filename, md, *args, enveloppe=False):
     6def exportVTK(filename, md, *args, enveloppe=False, **kwargs):
    77    '''
    88    vtk export
     
    2020    enveloppe is an option keeping only the enveloppe of the md (it is False by default)
    2121
     22    Options:
     23        - clipping : allows to reduce your domain (cliping=[Xmin, Xmax, Ymin, Ymax])
     24        - coarsetime : output one timestep every X (coarsetime=X, with X an integer)
     25        - singletime : output only timestep X (singletime=X, with X an integer or -1 for last)
     26
    2227    TODO: - make time easily accessible
    2328
    2429    Basile de Fleurian:
    2530    '''
     31
     32    for key in kwargs.keys():
     33        if key not in ['clipping', 'coarsetime', 'singletime']:
     34            raise BadOption('Provided option "{}" is not supported possibilities are : {}'.format(key, ['cliping', 'coarsetime']))
     35
     36    if 'coarsetime' in kwargs.keys() and 'singletime' in kwargs.keys():
     37        raise BadOption("You can't specify both 'coarsetime' and 'singletime'")
    2638
    2739    # File checking and creation {{{
     
    113125                                        3 : md.geometry.bed
    114126                                        4 : 0\n''')
    115             if mesh_alti == 1:
     127            if mesh_alti == '1':
    116128                points = np.column_stack((md.mesh.x, md.mesh.y, md.geometry.surface))
    117             elif mesh_alti == 2:
     129            elif mesh_alti == '2':
    118130                points = np.column_stack((md.mesh.x, md.mesh.y, md.geometry.base))
    119             elif mesh_alti == 3:
     131            elif mesh_alti == '3':
    120132                points = np.column_stack((md.mesh.x, md.mesh.y, md.geometry.bed))
    121             elif mesh_alti == 4:
     133            elif mesh_alti == '4':
    122134                points = np.column_stack((md.mesh.x, md.mesh.y, 0. * md.mesh.x))
    123135            else:
    124136                points = np.column_stack((md.mesh.x, md.mesh.y, md.geometry.surface))
    125137        elif dim == 3:
    126             mesh_alti = 1
     138            mesh_alti = '1'
    127139            points = np.column_stack((md.mesh.x, md.mesh.y, md.mesh.z))
    128140        else:
    129141            raise BadDimension('exportVTK does not support dimension {}'.format(dim))
     142
     143    if 'clipping' in kwargs.keys():
     144        # first get the boundaries and check them
     145        [Xmin, Xmax, Ymin, Ymax] = kwargs['clipping']
     146        if Xmin > Xmax:
     147            raise ClipError('Xmax ({}) should be larger than Xmin ({})'.format(Xmax, Xmin))
     148        if Ymin > Ymax:
     149            raise ClipError('Ymax ({}) should be larger than Ymin ({})'.format(Ymax, Ymin))
     150        if Xmin > np.nanmax(points[:, 0]) or Xmax < np.nanmin(points[:, 0]):
     151            raise ClipError('Your X boundaries [{}, {}] are outside of the model domain [{},{}]'.format(Xmin, Xmax, np.nanmin(points[:, 0]), np.nanmax(points[:, 0])))
     152        if Ymin > np.nanmax(points[:, 1]) or Ymax < np.nanmin(points[:, 1]):
     153            raise ClipError('Your Y boundaries [{}, {}] are outside of the model domain [{},{}]'.format(Ymin, Ymax, np.nanmin(points[:, 1]), np.nanmax(points[:, 1])))
     154
     155        #boundaries should be fine lets do stuff
     156        InX = np.where(np.logical_and(points[:, 0] >= Xmin, points[:, 0] <= Xmax))
     157        InY = np.where(np.logical_and(points[:, 1] >= Ymin, points[:, 1] <= Ymax))
     158
     159        Isinside = np.zeros(np.shape(points)[0], dtype=bool)
     160        clip_convert_index = np.nan * np.ones(np.shape(points)[0])
     161
     162        Inclipping = np.intersect1d(InX, InY)
     163        Isinside[Inclipping] = True
     164        points = points[Inclipping, :]
     165        num_of_points = np.shape(points)[0]
     166
     167        clipconnect = np.asarray([], dtype=int)
     168        for elt in connect:
     169            if set(elt).issubset(Inclipping):
     170                clipconnect = np.append(clipconnect, elt, axis=0)
     171
     172        num_of_elt = int(np.size(clipconnect) / 3)
     173        connect = clipconnect.reshape(num_of_elt, 3)
     174
     175        clip_convert_index = np.asarray([[i, np.where(Inclipping == i)[0][0]] for i, val in enumerate(clip_convert_index) if any(Inclipping == i)])
     176        enveloppe_index = enveloppe_index[clip_convert_index[:, 0]]
     177        for elt in range(0, num_of_elt):
     178            try:
     179                connect[elt, 0] = clip_convert_index[np.where(clip_convert_index == connect[elt, 0])[0], 1][0]
     180            except IndexError:
     181                connect[elt, 0] = -1
     182            try:
     183                connect[elt, 1] = clip_convert_index[np.where(clip_convert_index == connect[elt, 1])[0], 1][0]
     184            except IndexError:
     185                connect[elt, 1] = -1
     186            try:
     187                connect[elt, 2] = clip_convert_index[np.where(clip_convert_index == connect[elt, 2])[0], 1][0]
     188            except IndexError:
     189                connect[elt, 2] = -1
     190
     191        connect = connect[np.where(connect != -1)[0], :]
     192        num_of_elt = np.shape(connect)[0]
     193
    130194    # }}}
    131195    # write header and mesh {{{
    132196    print('Now starting to write stuff')
    133     for step in range(0, num_of_timesteps):
     197
     198    if 'coarsetime' in kwargs.keys():
     199        steplist = range(0, num_of_timesteps, kwargs['coarsetime'])
     200    elif 'singletime' in kwargs.keys():
     201        steplist = [kwargs['singletime']]
     202    else:
     203        steplist = range(0, num_of_timesteps)
     204
     205    for step in steplist:
    134206        print('Writing for step {}'.format(step))
    135207        saved_cells = {}
     
    142214        fid.write('POINTS {:d} float\n'.format(num_of_points))
    143215    #updating z for mesh evolution
    144         if moving_mesh and mesh_alti in [1, 2]:
     216        if moving_mesh and mesh_alti in ['1', '2']:
    145217            base = np.squeeze(res_struct.__dict__['TransientSolution'][step].__dict__['Base'][enveloppe_index])
    146218            thick_change_ratio = (np.squeeze(res_struct.__dict__['TransientSolution'][step].__dict__['Thickness'][enveloppe_index]) / md.geometry.thickness[enveloppe_index])
     
    344416class BadDimension(Exception):
    345417    """The required dimension is not supported yet."""
     418
     419
     420class BadOption(Exception):
     421    """The given option does not exist."""
     422
     423
     424class ClipError(Exception):
     425    """Error while trying to clip the domain."""
Note: See TracChangeset for help on using the changeset viewer.