ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
428 stars 120 forks source link

animate_nodal_solution_set for Stresses, Strains #302

Open jeremymoran opened 3 years ago

jeremymoran commented 3 years ago

Hi,

I currently have a sim with one load step and several substeps. Just curious as to whether I can animate the nodal stresses, strains using the animate_nodal_solution_set function?

akaszynski commented 3 years ago

At the moment, no, but I can add these features in the near future.

jeremymoran commented 3 years ago

No problem. Perhaps a simpler alternative in the interim would be to write a sequence of .vtu files, i.e. results001.vtu, results.002.vtu. This can get read by Paraview as an animated sequence. I modified the save_to_vtk() function to remove the "Nodal Solution X" label. This way every .vtu file simply has "Nodal Solution" without numbering, allowing for proper playback.

I have attached my modified function here if it is of any help. I simply use this in a loop.

def save_as_vtk2(resultf, filename, rsets=None, result_types=['ENS','EEL','EPL']):

    # Copy grid as to not write results to original object
    grid = resultf.grid.copy()

    if rsets is None:
        rsets = range(resultf.nsets)
    elif isinstance(rsets, int):
        rsets = [rsets]
    elif not isinstance(rsets, Iterable):
        raise TypeError('rsets must be an iterable like [0, 1, 2] or range(3)')

    if result_types is None:
        result_types = ELEMENT_INDEX_TABLE_KEYS
    elif not isinstance(result_types, list):
        raise TypeError('result_types must be a list of solution types')
    else:
        for item in result_types:
            if item not in ELEMENT_INDEX_TABLE_KEYS:
                raise ValueError('Invalid result type "%s" % item')

    try:
        from tqdm import tqdm
        pbar = tqdm(total=len(rsets), desc='Saving to file')
    except ImportError:
        pbar = None

    for i in rsets:
        # Nodal results
        _, val = resultf.nodal_solution(i)
        grid.point_arrays['Nodal Solution'] = val   #Removed Nodal Solution XX label

        # Nodal results
        for rtype in resultf.available_results:
            if rtype in result_types:
                _, values = resultf._nodal_result(i, rtype)
                desc = element_index_table_info[rtype]
                grid.point_arrays['{:s}'.format(desc)] = values #Removed Nodal Solution XX label

        if pbar is not None:
            pbar.update(1)

    grid.save(filename)
    if pbar is not None:
        pbar.close()

def APDL_VTK():
    rst = pyansys.read_binary('apdl/file.rst')
    rsets = range(0, rst.nsets, 5)

    for i in rsets:
        save_as_vtk2(rst,'output/vtk/ansys_vtk_%d.vtk'%i, i)
germa89 commented 2 years ago

Linking #362

Clearly there is a need for improving plotting capabilities. We probably need to think about the long term plan and the big picture.

akaszynski commented 2 years ago

We might want to consider pulling off the plotting components of pymapdl into a separate library as pyfluent did within pyfluent-visualization.

germa89 commented 2 years ago

I think that is way to go forward. If we share the data model with pyfluent we might be able to reuse many of their functions. But I guess it is not possible.