11
11
import sys
12
12
import threading
13
13
14
- from ansys .api .dyna .v0 import dynasolver_pb2 , dynasolver_pb2_grpc
14
+ from ansys .api .dyna .v0 import dynasolver_pb2
15
15
import grpc
16
16
17
17
from . import grpc_tags as tag
18
+ from .launcher import * # noqa : F403
18
19
19
20
#
20
21
# Define our own exceptions
@@ -60,12 +61,46 @@ class DynaSolver:
60
61
"""
61
62
62
63
# logger = None
63
- def __init__ (self , hostname , port ):
64
+ def __init__ (self , hostname , port , server_path = "" ):
64
65
"""Create a client instance connected to the host name (or IP address) and port."""
65
66
self .hostname = hostname
66
67
self .port = port
67
- self .channel = grpc .insecure_channel (hostname + ":" + port )
68
- self .stub = dynasolver_pb2_grpc .DynaSolverCommStub (self .channel )
68
+
69
+ # start server locally
70
+ if (hostname .lower () == "localhost" or hostname == LOCALHOST ) and not DynaSolver .grpc_local_server_on ():
71
+ # LOG.debug("Starting solver server")
72
+
73
+ if len (server_path ) == 0 :
74
+ server_path = os .getenv ("ANSYS_PYDYNA_SOLVER_SERVER_PATH" )
75
+ if server_path is None :
76
+ print ("Please set the environment variable for ANSYS_PYDYNA_SOLVER_SERVER_PATH" )
77
+ return
78
+ if os .path .isdir (server_path ):
79
+ threadserver = ServerThread (1 , port = port , ip = hostname , server_path = server_path )
80
+ # threadserver.setDaemon(True)
81
+ threadserver .start ()
82
+ waittime = 0
83
+ while not DynaSolver .grpc_local_server_on ():
84
+ sleep (5 )
85
+ waittime += 5
86
+ print (waittime )
87
+ if waittime > 60 :
88
+ print ("Failed to start pydyna solver server locally" )
89
+ break
90
+
91
+ else :
92
+ print ("Failed to start pydyna solver server locally,Invalid server path!" )
93
+
94
+ temp = hostname + ":" + str (port )
95
+ self .channel = grpc .insecure_channel (temp )
96
+ try :
97
+ grpc .channel_ready_future (self .channel ).result (timeout = 5 )
98
+ except grpc .FutureTimeoutError :
99
+ logging .critical ("Can not connect to Solver Server" )
100
+ sys .exit ()
101
+ logging .info ("Connected to Solver Server..." )
102
+ # self.stub = dynasolver_pb2_grpc.DynaSolverCommStub(self.channel)
103
+ self .stub = DynaSolverCommStub (self .channel )
69
104
# if DynaSolver.logger is None:
70
105
# DynaSolver.logger = logging.getLogger("DynaSolver")
71
106
# DynaSolver.logger.setLevel(logging.INFO)
@@ -78,6 +113,22 @@ def __init__(self, hostname, port):
78
113
# self.logger = DynaSolver.logger
79
114
self .logger = logging .getLogger ("DynaSolver" )
80
115
116
+ @staticmethod
117
+ def grpc_local_server_on () -> bool :
118
+ """Check if the server is launched locally.
119
+
120
+ Returns
121
+ -------
122
+ bool
123
+ ``True`` when successful, ``False`` when failed.
124
+ """
125
+ channel = grpc .insecure_channel ("localhost:5000" )
126
+ try :
127
+ grpc .channel_ready_future (channel ).result (timeout = 5 )
128
+ except :
129
+ return False
130
+ return True
131
+
81
132
def _argcheck (self , cmd , ngiven , nrequired ):
82
133
"""Internally used routine for checking command argument counts
83
134
when using the generic ``send`` method."""
@@ -247,12 +298,13 @@ def upload(self, fname):
247
298
#
248
299
# First packet contains the filename. The rest hold the contents.
249
300
#
250
- self .logger .debug ("upload: %s" % fname )
301
+ # self.logger.debug("upload: %s" % fname)
251
302
fsize = 0
252
303
253
304
def push_packets (fname ):
254
305
nonlocal fsize
255
- request = dynasolver_pb2 .DynaSolverFileData ()
306
+ # request = dynasolver_pb2.DynaSolverFileData()
307
+ request = DynaSolverFileData ()
256
308
# Only send the base file name, not the whole path!
257
309
bfname = os .path .split (fname )[1 ]
258
310
request .b = bytes (bfname , "utf-8" )
@@ -261,7 +313,8 @@ def push_packets(fname):
261
313
blocksize = 1000000
262
314
n = blocksize
263
315
while n == blocksize :
264
- request = dynasolver_pb2 .DynaSolverFileData ()
316
+ # request = dynasolver_pb2.DynaSolverFileData()
317
+ request = DynaSolverFileData ()
265
318
request .b = fp .read (blocksize )
266
319
n = len (request .b )
267
320
fsize = fsize + n
@@ -335,7 +388,8 @@ def run(self, args):
335
388
Command line to pass to LS-DYNA.
336
389
"""
337
390
self .logger .debug ("run: %s" % args )
338
- request = dynasolver_pb2 .DynaSolverRelay ()
391
+ # request = dynasolver_pb2.DynaSolverRelay()
392
+ request = DynaSolverRelay ()
339
393
request .tag = tag .RUN
340
394
request .b = bytes (args , "utf-8" )
341
395
response = self .stub .send_request (request )
@@ -369,7 +423,7 @@ def setlc(self, lc, value):
369
423
self ._check_return (response )
370
424
return
371
425
372
- def start (self , nproc ):
426
+ def start (self , nproc , solver_fname = "" ):
373
427
"""Start LS-DYNA.
374
428
375
429
The program starts and awaits further input. To begin a
@@ -385,14 +439,36 @@ def start(self, nproc):
385
439
Number of cores (MPI ranks) to run.
386
440
"""
387
441
self .logger .debug ("start: %d" % nproc )
388
- request = dynasolver_pb2 .DynaSolverStart ()
389
- request .exename = b"mppdyna"
442
+ # request = dynasolver_pb2.DynaSolverStart()
443
+ request = DynaSolverStart ()
444
+ # request.exename = b"mppdyna"
445
+ request .exename = bytes (solver_fname , "utf-8" )
390
446
request .nproc = nproc
391
447
response = self .stub .start_solver (request )
392
448
if response .status == tag .RUNNING :
393
449
raise RunningError ("LSDYNA is already running" )
394
450
return
395
451
452
+ def start_locally (self , preset = "MPP_DOUBLE" , input = "" , nproc = 1 , memory = 20 ):
453
+ """Begin execution with the given string as the command-line arguments.
454
+
455
+ Parameters
456
+ ----------
457
+ args : str
458
+ Command line to pass to LS-DYNA.
459
+ """
460
+
461
+ self .logger .debug ("start: %d" % nproc )
462
+ request = DynaSolverStartLocal ()
463
+ request .preset = bytes (preset , "utf-8" )
464
+ request .input = bytes (input , "utf-8" )
465
+ request .nproc = nproc
466
+ request .memory = memory
467
+ response = self .stub .start_solver_locally (request )
468
+ # if response.status == tag.RUNNING:
469
+ # raise RunningError("LSDYNA is already running")
470
+ return
471
+
396
472
def switch (self , args ):
397
473
"""Send a "sense switch" to LS-DYNA.
398
474
0 commit comments