Damping Formulation#

Open a time-domain project, read damping options (Damping Formulation tab), optionally apply recommended Rayleigh frequencies, and fetch preview tables as pandas DataFrames. Preview plots check your setup; they do not replace Run Compute.

  1"""
  2Damping Formulation scripting: read settings, optional recommended frequencies, preview tables.
  3
  4Requires a running RSSeismic scripting server and a time-domain project file.
  5
  6  python examples/example_damping_formulation.py path\\to\\project.rsseismicfile
  7  python examples/example_damping_formulation.py --motion EQ1 --apply-recommended project.rsseismicfile
  8  python examples/example_damping_formulation.py --no-preview project.rsseismicfile
  9"""
 10
 11from __future__ import annotations
 12
 13import argparse
 14import os
 15import sys
 16
 17from rsseismic import RSSeismicApplication
 18from rsseismic._client import DEFAULT_SCRIPTING_PORT
 19from rsseismic.exceptions import ScriptingError
 20from rsseismic.types import DampingMatrixType, RayleighDampingType
 21
 22
 23def _select_motion_name(model, requested: str) -> str:
 24    if requested.strip():
 25        return requested.strip()
 26    model.Motions.refreshMotionsList()
 27    motions = model.Motions.listMotions()
 28    if not motions:
 29        raise RuntimeError(
 30            "No motions in the project. Add motions in RSSeismic or pass --motion."
 31        )
 32    selected = [motion.name for motion in motions if motion.isSelected]
 33    return selected[0] if selected else motions[0].name
 34
 35
 36def run_damping_formulation_example(
 37    project_path: str,
 38    *,
 39    port: int = DEFAULT_SCRIPTING_PORT,
 40    motion_name: str = "",
 41    apply_recommended: bool = False,
 42    run_preview: bool = True,
 43) -> None:
 44    project_path = os.path.abspath(project_path.strip())
 45    if not os.path.isfile(project_path):
 46        raise FileNotFoundError(f"Project not found: {project_path}")
 47
 48    app = RSSeismicApplication(port=port)
 49    model = None
 50    try:
 51        app.ping()
 52        model = app.openFile(project_path)
 53        damping = model.DampingFormulation
 54
 55        settings = damping.getSettings()
 56        print("Current damping options:")
 57        print(f"  Matrix type     {settings.dampingMatrixType.name}")
 58        print(f"  Rayleigh type   {settings.rayleighDampingType.name}")
 59        print(
 60            f"  Frequencies Hz  {settings.modeFreq1}, {settings.modeFreq2}, "
 61            f"{settings.modeFreq3}, {settings.modeFreq4}"
 62        )
 63
 64        if apply_recommended:
 65            if settings.dampingMatrixType is not DampingMatrixType.RAYLEIGH:
 66                damping.setRayleigh(useFrequencies=True)
 67            elif settings.rayleighDampingType is not RayleighDampingType.FREQUENCIES:
 68                damping.setRayleigh(useFrequencies=True)
 69            settings = damping.applyRecommendedFrequencies()
 70            print("Applied recommended Rayleigh frequencies:")
 71            print(f"  f1 = {settings.modeFreq1} Hz, f2 = {settings.modeFreq2} Hz")
 72            model.save()
 73
 74        if not run_preview:
 75            return
 76
 77        if settings.dampingMatrixType is DampingMatrixType.RAYLEIGH:
 78            curve = damping.getDampingCurveDataFrame()
 79            print(f"Damping curve table: {len(curve)} rows, columns {list(curve.columns)}")
 80
 81        motion = _select_motion_name(model, motion_name)
 82        print(f"Preview motion: {motion}")
 83
 84        ftf, sa = damping.getFrequencyDomainSolutionDataFrames(motion)
 85        print(
 86            f"Frequency-domain preview: FTF {len(ftf)} rows, "
 87            f"spectral acceleration {len(sa)} rows"
 88        )
 89
 90        time_ftf, time_sa = damping.getTimeDomainSolutionDataFrames(motion)
 91        print(
 92            f"Time-domain preview: FTF {len(time_ftf)} rows, "
 93            f"spectral acceleration {len(time_sa)} rows"
 94        )
 95    except ScriptingError as ex:
 96        raise RuntimeError(str(ex)) from ex
 97    finally:
 98        if model is not None:
 99            model.close(saveProject=False)
100        app.close()
101
102
103def parse_args() -> argparse.Namespace:
104    parser = argparse.ArgumentParser(
105        description="Damping Formulation settings and preview tables for a project."
106    )
107    parser.add_argument(
108        "project",
109        help="Path to a .rsseismicfile (time-domain project).",
110    )
111    parser.add_argument(
112        "--motion",
113        "-m",
114        default="",
115        help="Motion name for preview plots (file name without extension).",
116    )
117    parser.add_argument(
118        "--apply-recommended",
119        action="store_true",
120        help="Apply recommended Rayleigh frequencies from the active profile, then save.",
121    )
122    parser.add_argument(
123        "--no-preview",
124        action="store_true",
125        help="Only read (and optionally apply recommended) settings; skip preview tables.",
126    )
127    parser.add_argument(
128        "--port",
129        "-p",
130        type=int,
131        default=int(os.environ.get("RSSEISMIC_SCRIPTING_PORT", DEFAULT_SCRIPTING_PORT)),
132        help=f"gRPC port (default: {DEFAULT_SCRIPTING_PORT}, or RSSEISMIC_SCRIPTING_PORT)",
133    )
134    return parser.parse_args()
135
136
137def main() -> int:
138    args = parse_args()
139    try:
140        run_damping_formulation_example(
141            args.project,
142            port=args.port,
143            motion_name=args.motion,
144            apply_recommended=args.apply_recommended,
145            run_preview=not args.no_preview,
146        )
147    except (FileNotFoundError, RuntimeError, ValueError) as ex:
148        print(ex, file=sys.stderr)
149        return 1
150    return 0
151
152
153if __name__ == "__main__":
154    raise SystemExit(main())