Writing input file for ROM with Python API not working

Provides a system for patient-specific cardiovascular modeling and simulation.
POST REPLY
User avatar
Ione Ianniruberto
Posts: 5
Joined: Thu Jun 13, 2024 5:43 am

Writing input file for ROM with Python API not working

Post by Ione Ianniruberto » Wed Nov 13, 2024 9:02 am

Hi,
I have this code that is a combination of two codes that i found in the git hub repository. First i want to extract the centerline of a given geometry, then i want to buid a 0d model. This is code:


import os
from pathlib import Path
import sv
import sys
import vtk

patient_id = 'AORTA-013'
## Set some directory paths.
script_path = Path(os.path.realpath(__file__)).parent
parent_path = Path(os.path.realpath(__file__)).parent.parent
data_path = "D:/Progetto NIH/Simvascular_Simulation/AORTA-013/mesh/lumen_mesh/mesh-surfaces"
mesh_complete_path = "D:/Progetto NIH/Simvascular_Simulation/AORTA-013/mesh/lumen_mesh/mesh-complete.exterior.vtp"


## Centerline extration

# Create a modeler.
kernel = sv.modeling.Kernel.POLYDATA
modeler = sv.modeling.Modeler(kernel)

model = modeler.read(mesh_complete_path)

model_polydata = model.get_polydata()
face_id = model_polydata.GetPointData().GetArray('ModelFaceID')

inlet_ids = [1]
outlet_ids = [4,3,2,5]

use_face_ids = False
centerlines = model.compute_centerlines(inlet_ids, outlet_ids, use_face_ids)
print('centerline extraction')
input_dir = "D:/Progetto NIH/Simvascular_Simulation/AORTA-013/0D"

file_name = os.path.join(input_dir, 'centerlines.vtp')
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(file_name)
writer.SetInputData(centerlines)
writer.Update()
writer.Write()

## Rom simulation
rom_simulation = sv.simulation.ROM()

## Create ROM simulation parameters.
params = sv.simulation.ROMParameters()

## Mesh parameters.
mesh_params = params.MeshParameters()

## Model parameters.
model_params = params.ModelParameters()
model_params.name = "demo"
model_params.inlet_face_names = ['inlet_aorta' ]
model_params.outlet_face_names = ['aorta_bct', 'aorta_car', 'aorta_suc', 'aorta_outlet']
model_params.centerlines_file_name = input_dir + 'centerlines.vtp'

## Fluid properties.
fluid_props = params.FluidProperties()

## Set wall properties.
print("Set wall properties ...")
material = params.WallProperties.OlufsenMaterial()
print("Material model: {0:s}".format(str(material)))

## Set boundary conditions.
#
inlet_path = "D:/Progetto NIH/Inlet bc/AORTA-013"
bcs = params.BoundaryConditions()
#bcs.add_resistance(face_name='outlet', resistance=1333)
bcs.add_velocities(face_name='inlet', file_name=inlet_path +'flow_inlet_0.flow')
bcs.add_rcr(face_name='aorta_bct', Rp=90.0, C=0.0008, Rd=1200)
bcs.add_rcr(face_name='aorta_car', Rp=100.0, C=0.0004, Rd=1100)
bcs.add_rcr(face_name='aorta_suc', Rp=90.0, C=0.0008, Rd=1200)
bcs.add_rcr(face_name='aorta_outlet', Rp=100.0, C=0.0004, Rd=1100)

## Set solution parameters.
#
solution_params = params.Solution()
solution_params.time_step = 0.001
solution_params.num_time_steps = 1000

## Write a 1D solver input file.
#
output_dir = "D:/Progetto NIH/Simvascular_Simulation/AORTA-013/0D/output"
rom_simulation.write_input_file(model_order=0, model=model_params, mesh=mesh_params, fluid=fluid_props,
material=material, boundary_conditions=bcs, solution=solution_params, directory=output_dir)


When i run it from the shell, it doesn't give an error but it doesn't write the input file either. Does somebody know how to fix this problem?
And has the run method been defined?
Thanks,
Ione

User avatar
David Parker
Posts: 1729
Joined: Tue Aug 23, 2005 2:43 pm

Re: Writing input file for ROM with Python API not working

Post by David Parker » Wed Nov 13, 2024 10:38 am

Hello Ione,

The Python code you posted looks fine.

You do need to run the script within SimVascular, something like

simvascular --python -- script.py

where simvascular is the SimVascular shell script, not sure where this is on Windows.

Are you running this script in a Power Shell? Windows may not want to run an external program.

Cheers,
Dave

User avatar
Ione Ianniruberto
Posts: 5
Joined: Thu Jun 13, 2024 5:43 am

Re: Writing input file for ROM with Python API not working

Post by Ione Ianniruberto » Thu Nov 14, 2024 2:03 am

Hi,
thanks for your answer. Yes I'm runnig the script in the Windows power shell using this command:
sv.bat --python -- script.py

But I'm not sure wheter this is a problem or not since my collegues are using the same command to run different scripts for the mesh generation and everthing works fine.

Thansk,
Ione

User avatar
Ione Ianniruberto
Posts: 5
Joined: Thu Jun 13, 2024 5:43 am

Re: Writing input file for ROM with Python API not working

Post by Ione Ianniruberto » Thu Nov 14, 2024 2:20 am

When I try to run the code inside SimVascular using the python console, Simvascular crashes and closes suddenly.

What can be the problem?
Thanks,
Ione

User avatar
David Parker
Posts: 1729
Joined: Tue Aug 23, 2005 2:43 pm

Re: Writing input file for ROM with Python API not working

Post by David Parker » Thu Nov 14, 2024 1:02 pm

Hi Ione,

Running a Python script in the SV Python panel needs to have an active project I think, not sure why it would crash though.

It is difficult to see any errors in the Python script because the SV window pops up and then disappears.

To test the script I started the SV Python interpreter and then ran the script in the interpreter using

Code: Select all

import os
from pathlib import Path
exec(Path("script.py").read_text())
When I ran your script in a Power Shell and discovered some problems (I'm not a Windows user so these were new to me!)

- the current working directory is set to the location of the sv.bat
- the Python __file__ variable is not defined
- Windows paths need to use raw strings: path = r'C:\Users\YourName\Documents\file.txt'

Fixing the path names and removing using __file__ I was able to create centerlines and the 1D input file.

Cheers,
Dave

POST REPLY