Skip to content

Performance of get/set field data #2201

Closed
@FedericoNegri

Description

@FedericoNegri

Before submitting the issue

  • I have checked for Compatibility issues
  • I have searched among the existing issues
  • I am using a Python virtual environment

Description of the bug

Some recent changes (f7be394?) seem to have slowed down manipulating field data (by orders of magnitude!).

Steps To Reproduce

This is a minimal script to reproduce the issue. Using uv I could easily run it with different "versions" of PyDPF-Core and compare timings.

Using the latest released version (0.13.6) it takes ~20ms to run. With current main it instead takes >5s.

Note: I'm aware the operations tested here could be done in a different way, by setting the field data and scoping outside of the loop. But still, I feel like this could indicate some bigger problem.

# /// script
# requires-python = "==3.12.9"
# dependencies = [
#     "numpy==2.2.4",
#     "ansys-dpf-core==0.13.6", #fast
#     #"ansys-dpf-core @ git+https://[email protected]/ansys/pydpf-core.git@e2a1cf0caa01d3376e956cbea026590a40ccbf48", #fast
#     #"ansys-dpf-core @ git+https://[email protected]/ansys/pydpf-core.git@f7be394515266766b6208ef4f341a5fdfec558db", #slow
# ]
# ///
"""
Script to measure performance of creating a DPF field.

How to:

1. Install uv if not already installed:
    pip install uv
   See https://docs.astral.sh/uv/getting-started/installation/#standalone-installer

2. Save this code as, say, `create_field.py` and run it:
    uv run create_field.py

3. Change the ansys-dpf-core version in the script header and re-run the script:
    uv run create_field.py
"""

import time
import numpy as np
from ansys.dpf.core import fields_factory

from ansys.dpf import core as dpf

server = dpf.start_local_server(
    context=dpf.AvailableServerContexts.premium,
    config=dpf.AvailableServerConfigs.InProcessServer,
    as_global=True,
)

num_entities = int(1e+6)
field = fields_factory.create_scalar_field(
    num_entities=num_entities, location=dpf.locations.elemental, server=server
)
field.name = "my_field"
field.data = np.zeros(num_entities, dtype=np.int32)
field.scoping.ids = np.zeros(num_entities, dtype=np.int32)

all_indices = np.arange(num_entities)
chunks = np.array_split(all_indices, 200)

start_time = time.perf_counter()
for index, chunk in enumerate(chunks):

    field.data[chunk] = int(index)
    field.scoping.ids[chunk] = chunk

print(
    f"Filling DPF field done in {(time.perf_counter() - start_time):.3f} s"
)

Which Operating System causes the issue?

Windows

Which DPF/Ansys version are you using?

DPF Server 2025.2.pre0

Which Python version causes the issue?

3.12

Installed packages

See script above

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions