Skip to content

Commit

Permalink
Merge pull request #333 from amnp95/master
Browse files Browse the repository at this point in the history
Updating M9 to V3 and adding DRM model backends
  • Loading branch information
fmckenna authored Oct 1, 2024
2 parents 1766b8a + 3f8c184 commit bdf212a
Show file tree
Hide file tree
Showing 14 changed files with 3,517 additions and 157 deletions.
194 changes: 98 additions & 96 deletions modules/createEVENT/M9/M9App2.py
Original file line number Diff line number Diff line change
@@ -1,126 +1,128 @@
# %% # noqa: INP001, D100
#%%
import os
import json
import os
import time
from datetime import datetime
import time
from tapipy.tapis import Tapis

from agavepy.agave import Agave

# change the directory to the current directory
os.chdir(os.path.dirname(os.path.realpath(__file__))) # noqa: PTH120
os.chdir(os.path.dirname(os.path.realpath(__file__)))

def Submit_tapis_job(username, password):
t = Tapis(
base_url="https://designsafe.tapis.io",
username=username,
password=password)

t.get_tokens()

t.authenticator.list_profiles()

def Submit_tapis_job(): # noqa: N802, D103
ag = Agave.restore()
with open('TapisFiles/information.json') as file: # noqa: PTH123
information = json.load(file)
with open("TapisFiles/information.json", "r") as file:
information = json.load(file)
file.close()

# %%
profile = ag.profiles.get()
username = profile['username']
savingDirectory = information['directory'] # noqa: N806
if not os.path.exists(savingDirectory): # noqa: PTH110
os.makedirs(savingDirectory) # noqa: PTH103

print('Uploading files to designsafe storage') # noqa: T201
ag.files.manage(
systemId='designsafe.storage.default',
filePath=f'{username}/',
body={'action': 'mkdir', 'path': 'physics_based'},
)
ag.files.manage(
systemId='designsafe.storage.default',
filePath=f'{username}/physics_based',
body={'action': 'mkdir', 'path': 'M9'},
)
# ag.files_mkdir(systemId="designsafe.storage.default", filePath=f"{username}/physics_based/Istanbul2")
with open('TapisFiles/M9.py', 'rb') as file: # noqa: PTH123
result = ag.files.importData(
filePath=f'{username}/physics_based/M9/',
fileToUpload=file,
systemId='designsafe.storage.default',
)
with open('TapisFiles/information.json', 'rb') as file: # noqa: PTH123
result = ag.files.importData(
filePath=f'{username}/physics_based/M9/',
fileToUpload=file,
systemId='designsafe.storage.default',
)
with open('TapisFiles/selectedSites.csv', 'rb') as file: # noqa: PTH123
result = ag.files.importData( # noqa: F841
filePath=f'{username}/physics_based/M9/',
fileToUpload=file,
systemId='designsafe.storage.default',
)
savingDirectory = information["directory"]
if not os.path.exists(savingDirectory):
os.makedirs(savingDirectory)


#print("Uploading files to designsafe storage")
t.files.mkdir(systemId="designsafe.storage.default", path=f"{t.username}/physics_based")
t.files.mkdir(systemId="designsafe.storage.default", path=f"{t.username}/physics_based/M9")
with open("TapisFiles/M9.py", 'rb') as file:
contents = file.read()
result = t.files.insert(systemId="designsafe.storage.default", path=f"{t.username}/physics_based/M9/M9.py", file=contents)
with open("TapisFiles/information.json", 'rb') as file:
contents = file.read()
result = t.files.insert(systemId="designsafe.storage.default", path=f"{t.username}/physics_based/M9/information.json", file=contents)
with open("TapisFiles/selectedSites.csv", 'rb') as file:
contents = file.read()
result = t.files.insert(systemId="designsafe.storage.default", path=f"{t.username}/physics_based/M9/selectedSites.csv", file=contents)

# %%
# -------------------------------------------------------------------------------
# Define Inputs
input_Directory = f"tapis://designsafe.storage.default/{t.username}/physics_based/M9"
fileInputs = [{"name":"Input Directory","sourceUrl":input_Directory}]

# -------------------------------------------------------------------------------
# Define parameterSet
input_filename = "M9.py"
input_uri = f"tapis://designsafe.storage.default/{t.username}/physics_based/M9/"
# parameterSet = {"envVariables": [{"key": "inputScript", "value": input_filename},
# {"key": "dataDirectory", "value": input_uri}]}

parameterSet = {"envVariables": [{"key": "inputScript", "value": input_filename},
{"key": "_UserProjects", "value": "4127798437512801810-242ac118-0001-012,PRJ-4603"}]}


jobdict = {
'name': '',
'appId': 'physicsBasedMotionApp-0.0.1',
'appId': 'SimCenter-DesignSafeVM',
'appVersion': '0.0.1',
'execSystemId': 'wma-exec-01',
'nodeCount': 1,
'processorsPerNode': 1,
'archive': True,
'coresPerNode': 1,
'maxMinutes': 30,
'archiveOnAppError': True,
'inputs': {'inputDirectory': ''},
'parameters': {'inputScript': 'M9.py'},
'maxRunTime': '00:01:00',
'memoryPerNode': '1GB',
'archiveSystem': 'designsafe.storage.default',
'archiveSystemId': 'designsafe.storage.default',
'archiveSystemDir': f"{t.username}/physics_based/M9/",
'fileInputs': fileInputs,
'parameterSet': parameterSet,
'tags': ['portalName: DESIGNSAFE']
}


# Generate a timestamp to append to the job name an
timestamp = datetime.now().strftime('%Y%m%d%H%M%S') # noqa: DTZ005
jobname = f'PhysicsBasedMotion_M9_{username}_{timestamp}'
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
jobname = f"PhysicsBasedMotion_M9_{t.username}_{timestamp}"

print('Submitting job') # noqa: T201
# submit the job
print("Submitting job")
# submit the job
jobdict['name'] = jobname
jobdict['inputs']['inputDirectory'] = (
f'agave://designsafe.storage.default/{username}/physics_based/M9/'
)

# %%
res = ag.jobs.submit(body=jobdict)
jobid = res['id']
status = ''
last_status = ''
count = 0
while status != 'FINISHED':
status = ag.jobs.getStatus(jobId=jobid)['status']
if count == 0:
last_status = status
print('Job status: ', status) # noqa: T201
count += 1
if last_status != status:
print('Job status: ', status) # noqa: T201
last_status = status
if status == 'FAILED':
print('Job failed') # noqa: T201
break
res = t.jobs.submitJob(**jobdict)
mjobUuid=res.uuid

time.sleep(10)
tlapse = 1
status =t.jobs.getJobStatus(jobUuid=mjobUuid).status
previous = status
while True:
if status in ["FINISHED","FAILED","STOPPED"]:
break
status = t.jobs.getJobStatus(jobUuid=mjobUuid).status
if status == previous:
continue
else :
previous = status
print(f"\tStatus: {status}")
time.sleep(tlapse)

# %%
print('Downloading extracted motions') # noqa: T201
archivePath = ag.jobs.get(jobId=jobid)['archivePath'] # noqa: N806
archivePath = f'{archivePath}/M9/Events/' # noqa: N806
print("Downloading extracted motions")
archivePath = t.jobs.getJob(jobUuid=mjobUuid).archiveSystemDir
archivePath = f"{archivePath}/M9/Events"
files = t.files.listFiles(systemId="designsafe.storage.default", path=archivePath)

files = ag.files.list(
filePath=archivePath, systemId='designsafe.storage.default'
)
# %%
# %%
if len(files) <= 1:
print('No files in the archive') # noqa: T201
else:
print("No files in the archive")
else :
for file in files:
filename = file['name']
if filename == '.':
filename = file.name
if filename == ".":
continue
path = f'{archivePath}/{filename}'
res = ag.files.download(
filePath=path, systemId='designsafe.storage.default'
)
with open(f'{savingDirectory}/{filename}', 'wb') as f: # noqa: PTH123
f.write(res.content)
# %%
path = f"{archivePath}/{filename}"
res = t.files.getContents(systemId="designsafe.storage.default", path=path)

with open(f"{savingDirectory}/{filename}", "w") as f:
f.write(res.decode("utf-8"))
f.close()
print("Files downloaded")
print("Please check the directory for the extracted motions")

# %%
5 changes: 4 additions & 1 deletion modules/createEVENT/M9/M9Run.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
'-o', '--output', help='number of realizations', required=False
)
parser.add_argument('-a', '--API', help='API FLAG', required=False)
parser.add_argument("--username", help="username", required=False)
parser.add_argument("--password", help="password", required=False)
args = parser.parse_args()

if args.lat:
Expand All @@ -53,6 +55,7 @@
if args.API == 'true':
information['APIFLAG'] = True


#
# go get the motions
#
Expand All @@ -64,5 +67,5 @@
M9API.M9(information)
else:
M9Stations.getStations(information, plot=False, show=False)
M9App2.Submit_tapis_job()
M9App2.Submit_tapis_job(args.username, args.password)
exit() # noqa: PLR1722
Loading

0 comments on commit bdf212a

Please sign in to comment.