-
Notifications
You must be signed in to change notification settings - Fork 4
/
plantfem.py
executable file
·147 lines (114 loc) · 5.11 KB
/
plantfem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# prior to this,
# please install plantfem
import os
import sys
class plantfem:
# Is plantFEM installed?
#ret = os.system("plantfem hello")
#if int(ret) != 0:
# print("Library package of plantFEM is not installed.")
# ans = input("Can I install the library? [y/N]")
# if ans == "y" or ans == "Y":
# # install
# os.system("git clone https://github.com/kazulagi/plantFEM.git && cd plantFEM && python3 install.py")
# create a Light object
def Light(self,config):
return Light(light_angle=90.0,light_direction=180.0)
# create a Soybean object
def Soybean(self,config):
return Soybean(config)
def Soybean(self,config):
return Soil(config)
# define operatable soybean object
#
import os
import uuid
import numpy as np
class Soybean():
def __init__(self,config):
self.__config = config
self.__uuid = str(uuid.uuid4() )
self.__fortran_script_name = str(self.__uuid)+".f90"
self.__fortran_script = open(self.__fortran_script_name,"w")
self.__fortran_script.write("use plantfem \n")
self.__fortran_script.write("implicit none\n")
self.__fortran_script.write("type(Soybean_) :: soybean\n")
self.__fortran_script.write("type(Light_) :: light\n")
self.__fortran_script.write("real(real64),allocatable :: ppfd(:)\n")
self.__fortran_script.write("type(IO_) :: f\n")
self.__fortran_script.write("call light%"+"init()\n")
self.__fortran_script.write("call soybean % " + "init(config='"+self.__config +"')\n")
# Soybean methods
def ppfd(self,light,transparency=0.10):
self.__fortran_script.write("light%"+"angles(1)="+str(light.direction())+"d0\n")
self.__fortran_script.write("light%"+"angles(2)="+str(light.angle())+"d0\n")
self.__fortran_script.write("ppfd = soybean % " + "getPPFD(light=light,transparency="+str(transparency)+"d0)\n")
self.__fortran_script.write("call f%"+"open('ppfd_"+str(self.__uuid)+".txt', 'w')\n")
self.__fortran_script.write("call f%"+"write(ppfd)\n")
self.__fortran_script.write("call f%"+"close()\n")
return 'ppfd_'+str(self.__uuid)+'.txt'
def vtk(self,name="untitled",single_file=True,scalar_field="None"):
self.__name = name
if single_file:
export_as_single_file="True"
else:
export_as_single_file="False"
if scalar_field == "None":
# export only geometry
self.__fortran_script.write("call soybean % vtk('"+name+"',single_file="+str(export_as_single_file)+")\n")
else :
field_type = scalar_field.split("_")
field_type = field_type[0]
field_type = str(field_type)
self.__fortran_script.write("call soybean % vtk('"+name+"',single_file="+str(export_as_single_file)+"&\n")
self.__fortran_script.write(" ,scalar_field="+field_type+")\n")
def run(self,num_process=1):
self.__fortran_script.write("end")
self.__fortran_script.close()
self.__num_process = num_process
command = 'mv '+str(self.__fortran_script_name)+' server.f90'
print(command)
os.system('mv '+str(self.__fortran_script_name)+' server.f90')
os.system('plantfem build')
if num_process==1:
os.system("./server.out")
else:
os.system('mpirun -np '+str(self.__num_process)+" ./server.out")
def __del__(self):
self.run()
class Light():
def __init__(self,angle=90.0,direction=180.0):
self.__light_angle = angle
self.__light_direction = direction
def angle(self):
return self.__light_angle
def direction(self):
return self.__light_direction
def create_soybean(in_name,out_name):
if os.path.exists("./create_soybean.out"):
os.system("./create_soybean.out "+in_name+" "+out_name)
else:
os.system("plantfem build Tutorial/obj/create_soybean.f90")
os.system("mv Tutorial/obj/create_soybean.out ./")
os.system("./create_soybean.out "+in_name+" "+out_name)
def create_maize(in_name,out_name):
if os.path.exists("./create_maize.out"):
os.system("./create_maize.out "+in_name+" "+out_name)
else:
os.system("plantfem build Tutorial/obj/create_maize.f90")
os.system("mv Tutorial/obj/create_maize.out ./")
os.system("./create_maize.out "+in_name+" "+out_name)
def create_rice(in_name,out_name):
if os.path.exists("./create_rice.out"):
os.system("./create_rice.out "+in_name+" "+out_name)
else:
os.system("plantfem build Tutorial/obj/create_rice.f90")
os.system("mv Tutorial/obj/create_rice.out ./")
os.system("./create_rice.out "+in_name+" "+out_name)
def create_wheat(in_name,out_name):
if os.path.exists("./create_wheat.out"):
os.system("./create_wheat.out "+in_name+" "+out_name)
else:
os.system("plantfem build Tutorial/obj/create_wheat.f90")
os.system("mv Tutorial/obj/create_wheat.out ./")
os.system("./create_wheat.out "+in_name+" "+out_name)