@@ -140,101 +140,126 @@ def update_physicell_files(DIR, project_loaded=False):
140
140
if project_loaded :
141
141
makefile = f'{ DIR } /Makefile'
142
142
makefile_temp = append_suffix (f'{ DIR } /Makefile-TEMP' )
143
+ add_to_makefile = True
143
144
with open (makefile , 'r' ) as f :
144
145
lines = f .readlines ()
145
146
for line_number , line in enumerate (lines ):
146
- if "PhysiCell_custom_module_OBJECTS :=" in line :
147
- lines .insert (line_number ,"PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o\n \n " )
147
+ if "PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o" in line :
148
+ print (f"WARNING: Found PhysiPKPD_OBJECTS already defined in { makefile } . Skipping the rest of Makefile additions." )
149
+ print (f"\t If compilation errors occur, make sure PhysiPKPD_PK.o and PhysiPKPD_PD.o are defined" )
150
+ print (f"\t And make sure that $(PhysiPKPD_OBJECTS) is added to PhysiCell_OBJECTS." )
151
+ add_to_makefile = False
148
152
break
153
+ if add_to_makefile :
154
+ for line_number , line in enumerate (lines ):
155
+ if "PhysiCell_custom_module_OBJECTS :=" in line :
156
+ lines .insert (line_number ,"PhysiPKPD_OBJECTS := PhysiPKPD_PK.o PhysiPKPD_PD.o\n \n " )
157
+ break
158
+
159
+ if add_to_makefile :
160
+ source_file = f'{ DIR } /addons/PhysiPKPD/Makefile-PhysiPKPD-Objects.txt'
161
+ with open (source_file , 'r' ) as sf :
162
+ new_lines = sf .readlines ()
149
163
150
- source_file = f'{ DIR } /addons/PhysiPKPD/Makefile-PhysiPKPD-Objects.txt'
151
- with open (source_file , 'r' ) as sf :
152
- new_lines = sf .readlines ()
153
-
154
- for line_number , line in enumerate (lines ):
155
- if "custom_modules/custom.cpp" in line :
156
- break
157
-
158
- with open (makefile_temp ,'w' ) as f :
159
- for i in range (line_number ):
160
- print (lines [i ].rstrip ('\n ' ), file = f )
161
- for new_line in new_lines :
162
- print (new_line .rstrip ('\n ' ), file = f )
163
- print ("\n " , file = f )
164
- for i in range (i + 1 ,len (lines )):
165
- print (lines [i ].rstrip ('\n ' ), file = f )
166
-
167
- with open (makefile_temp ,'r+' ) as f :
168
- lines = f .readlines ()
164
+ for line_number , line in enumerate (lines ):
165
+ if "custom_modules/custom.cpp" in line :
166
+ break
169
167
170
- for i , line in enumerate (lines ):
171
- if "PhysiCell_OBJECTS :=" in line :
172
- line = line .rstrip ('\n ' ) + ' $(PhysiPKPD_OBJECTS)\n '
173
- break
174
- lines [i ] = line
168
+ with open (makefile_temp ,'w' ) as f :
169
+ for i in range (line_number ):
170
+ print (lines [i ].rstrip ('\n ' ), file = f )
171
+ for new_line in new_lines :
172
+ print (new_line .rstrip ('\n ' ), file = f )
173
+ print ("\n " , file = f )
174
+ for i in range (i + 1 ,len (lines )):
175
+ print (lines [i ].rstrip ('\n ' ), file = f )
176
+
177
+ with open (makefile_temp ,'r+' ) as f :
178
+ lines = f .readlines ()
179
+
180
+ for i , line in enumerate (lines ):
181
+ if "PhysiCell_OBJECTS :=" in line :
182
+ line = line .rstrip ('\n ' ) + ' $(PhysiPKPD_OBJECTS)\n '
183
+ break
184
+ lines [i ] = line
175
185
176
- with open (makefile_temp ,'w' ) as f :
177
- f .writelines (lines )
186
+ with open (makefile_temp ,'w' ) as f :
187
+ f .writelines (lines )
178
188
179
- print (f"{ makefile_temp } is ready for PhysiPKPD. Editing other files first before overwriting { makefile } " )
189
+ print (f"{ makefile_temp } is ready for PhysiPKPD. Editing other files first before overwriting { makefile } " )
180
190
181
- def get_line_number (s , lines ):
191
+ def get_line_number (s , lines , print_missing_message = True ):
182
192
for line_number , line in enumerate (lines ):
183
193
if s in line :
184
194
return line_number
185
- print (f"{ f .name } does not have a line containing { s } " )
195
+ if print_missing_message :
196
+ print (f"{ f .name } does not have a line containing { s } " )
186
197
return None
187
198
199
+ add_to_main = True
188
200
main_temp = append_suffix (f'{ DIR } /main' ,'.cpp' )
189
201
with open (main_file , 'r+' ) as f :
190
202
lines = f .readlines ()
191
- line_number = get_line_number ("setup_tissue" , lines )
192
- if line_number is not None :
193
- lines .insert (line_number + 1 , "\n \t setup_pharmacodynamics();\n " ) # new_string should end in a newline
194
- with open (main_temp ,'w' ) as f_temp :
195
- f_temp .writelines (lines ) # No need to truncate as we are increasing filesize
203
+ if get_line_number ("setup_pharmacodynamics" , lines , print_missing_message = False ) is not None :
204
+ print (f"WARNING: Found setup_pharmacodynamics already defined in { main_file } . Skipping the rest of main.cpp additions." )
205
+ print (f"\t If errors occur, make sure PK_model(PhysiCell_globals.current_time); and PD_model(PhysiCell_globals.current_time); are on either side of microenvironment.simulate_diffusion_decay." )
206
+ add_to_main = False
196
207
else :
197
- print (f"{ DIR } /main.cpp does not include `setup_tissue();`???" )
198
- exit ()
208
+ line_number = get_line_number ("setup_tissue" , lines )
209
+ if line_number is not None :
210
+ lines .insert (line_number + 1 , "\n \t setup_pharmacodynamics();\n " ) # new_string should end in a newline
211
+ with open (main_temp ,'w' ) as f_temp :
212
+ f_temp .writelines (lines ) # No need to truncate as we are increasing filesize
213
+ else :
214
+ print (f"{ DIR } /main.cpp does not include `setup_tissue();`???" )
215
+ exit ()
199
216
200
- with open (main_temp , 'r+' ) as f :
201
- lines = f .readlines ()
202
- line_number = get_line_number ("microenvironment.simulate_diffusion_decay" , lines )
203
- if line_number is not None :
204
- lines .insert (line_number + 1 , "\n \t \t \t PD_model( PhysiCell_globals.current_time );\n " ) # new_string should end in a newline
205
- lines .insert (line_number , "\t \t \t PK_model( PhysiCell_globals.current_time );\n \n " ) # new_string should end in a newline
206
- f .seek (0 ) # readlines consumes the iterator, so we need to start over
207
- f .writelines (lines ) # No need to truncate as we are increasing filesize
208
- else :
209
- print (f"{ DIR } /main.cpp does not include `microenvironment.simulate_diffusion_decay(diffusion_dt);`???" )
210
- exit ()
211
-
212
- print (f"{ main_temp } is ready for PhysiPKPD. Editing other files first before overwriting { main_file } " )
217
+ if add_to_main :
218
+ with open (main_temp , 'r+' ) as f :
219
+ lines = f .readlines ()
220
+ line_number = get_line_number ("microenvironment.simulate_diffusion_decay" , lines )
221
+ if line_number is not None :
222
+ lines .insert (line_number + 1 , "\n \t \t \t PD_model( PhysiCell_globals.current_time );\n " ) # new_string should end in a newline
223
+ lines .insert (line_number , "\t \t \t PK_model( PhysiCell_globals.current_time );\n \n " ) # new_string should end in a newline
224
+ f .seek (0 ) # readlines consumes the iterator, so we need to start over
225
+ f .writelines (lines ) # No need to truncate as we are increasing filesize
226
+ else :
227
+ print (f"{ DIR } /main.cpp does not include `microenvironment.simulate_diffusion_decay(diffusion_dt);`???" )
228
+ exit ()
229
+
230
+ print (f"{ main_temp } is ready for PhysiPKPD. Editing other files first before overwriting { main_file } " )
213
231
214
232
with open (f"{ DIR } /custom_modules/custom.h" , "r+" ) as f :
215
233
lines = f .readlines ()
216
- line_number = get_line_number ("#include" , lines )
217
- if line_number is not None :
218
- lines .insert (line_number ,'#include "../addons/PhysiPKPD/src/PhysiPKPD.h"\n ' )
219
- f .seek (0 )
220
- f .writelines (lines )
234
+ if get_line_number ("addons/PhysiPKPD/src/PhysiPKPD.h" , lines , print_missing_message = False ):
235
+ print ("Found addons/PhysiPKPD/src/PhysiPKPD.h in {DIR}/custom_modules/custom.h. Not going to add it again here." )
221
236
else :
222
- print (f"{ DIR } /custom_modules/custom.h does not have any `#include` statements???" )
223
- exit ()
224
-
225
- with open (makefile , 'w' ) as f :
226
- with open (makefile_temp , 'r' ) as f_temp :
227
- lines = f_temp .readlines ()
228
- f .writelines (lines )
229
-
230
- with open (main_file , 'w' ) as f :
231
- with open (main_temp , 'r' ) as f_temp :
232
- lines = f_temp .readlines ()
233
- f .writelines (lines )
237
+ line_number = get_line_number ("#include" , lines )
238
+ if line_number is not None :
239
+ lines .insert (line_number ,'#include "../addons/PhysiPKPD/src/PhysiPKPD.h"\n ' )
240
+ f .seek (0 )
241
+ f .writelines (lines )
242
+ else :
243
+ print (f"{ DIR } /custom_modules/custom.h does not have any `#include` statements???" )
244
+ exit ()
245
+
246
+ if add_to_makefile :
247
+ with open (makefile , 'w' ) as f :
248
+ with open (makefile_temp , 'r' ) as f_temp :
249
+ lines = f_temp .readlines ()
250
+ f .writelines (lines )
251
+
252
+ if add_to_main :
253
+ with open (main_file , 'w' ) as f :
254
+ with open (main_temp , 'r' ) as f_temp :
255
+ lines = f_temp .readlines ()
256
+ f .writelines (lines )
234
257
235
258
print (f"All files (Makefile, main.cpp, and custom_modules/custom.h) updated and overwritten now. Removing temporary files..." )
236
- os .remove (makefile_temp )
237
- os .remove (main_temp )
259
+ if os .path .exists (makefile ):
260
+ os .remove (makefile_temp )
261
+ if os .path .exists (main_temp ):
262
+ os .remove (main_temp )
238
263
else :
239
264
with open (f'{ DIR } /addons/PhysiPKPD/Makefile-PhysiPKPD-Samples.txt' , "r" ) as f :
240
265
add_samples_to_makefile (f , f"{ DIR } /Makefile" )
0 commit comments